您好,我正在使用uglifyJs来最小化我的javascript文件,它一次只能处理一个文件,我想要的是将存在于名为JS的文件夹中的所有javascript文件最小化为名为JSM的文件夹。我的JS文件夹中有2个文件,分别称为test1.js和test2.js,我想对该文件夹运行uglify并在JSM文件夹中生成test1.min.js和test2.min.js,所以有一种方法可以这?像这样的命令:

uglifyjs -c -m JS/*.js JSM/*.min.js

或任何可以帮助我的想法。

谢谢。

最佳答案

我知道这似乎是一个巨大的步骤,但我真的建议您使用grunt
一旦掌握了它,这真的很简单。

这是速成类(class):

  • 安装NodeJS
  • 安装Grunt CLI(只需在控制台/终端中输入此命令):
    npm install -g grunt-cli
    
  • Create a simple package.json file in the root of your project:

    {
      "name": "my-project-name",
      "version": "1.0.0",
      "devDependencies": {
        "grunt": "~0.4.2",
        "grunt-contrib-uglify": "~0.2.4",
        "grunt-contrib-watch" : "~0.5.3"
      }
    }
    
  • Once you have that, just type: npm install to the console (in the root of your project).This will install the necessary grunt plugins/dependencies (from the package file above).

  • Now create a simple gruntfile.js in the root of your project (it's a kind of config for your project):

    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' ]);
    

    };
  • 完成后,您只需要构建它即可。在控制台中输入:

    咕unt

    或-更好-如果您键入下面的命令,-grunt将监视源文件的更改,如果您更改了其中的任何一个,它将自动生成它们:

    咕watch的 watch -力

  • 然后,您可以添加更多插件,例如:css缩小,css预处理程序(less,sass,手写笔),jshint等。

    09-10 11:36
    查看更多