Javascript Grunt-缩小/处理将多个文件合并为一个文件
我是格伦特的新手,我迷失在它的所有选择中。 我试着做两件事:Javascript Grunt-缩小/处理将多个文件合并为一个文件,javascript,gruntjs,Javascript,Gruntjs,我是格伦特的新手,我迷失在它的所有选择中。 我试着做两件事: 每次更改一个js文件时,将所有js文件缩小为一个 当我的某个scss文件发生更改时,处理特定的scss文件 以下是我得到的当前Gruntfile.js: module.exports = function (grunt) { grunt.initConfig({ // define source files and their destinations uglify: { files: {
- 每次更改一个js文件时,将所有js文件缩小为一个
- 当我的某个scss文件发生更改时,处理特定的scss文件
module.exports = function (grunt) {
grunt.initConfig({
// define source files and their destinations
uglify: {
files: {
src: 'js/*.js', // source files mask
dest: 'jsm/', // destination folder
expand: true, // allow dynamic building
flatten: true, // remove all unnecessary nesting
ext: '.min.js' // replace .js to .min.js
}
},
watch: {
js: { files: 'js/*.js', tasks: [ 'uglify' ] },
}
});
// load plugins
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-uglify');
// register at least this one task
grunt.registerTask('default', [ 'uglify' ]);
};
我如何才能做到这一点?使用
您应该将所有文件包装在一个匿名函数中,并使用var
定义变量,这样就不会在文件之间产生变量冲突
(function(){
// foo has function scope
var foo = 3;
console.log(foo);
// FOO is a global variable and
// can be accessed between files
window.FOO = 3;
console.log(FOO);
}).call()
(function(){
// foo has function scope
var foo = 3;
console.log(foo);
// FOO is a global variable and
// can be accessed between files
window.FOO = 3;
console.log(FOO);
}).call()