node.js - 如果启用了 Uglify 中的动态扩展,如何使用 Grunt.js 监视任务只对更改的文件进行 uglify?

标签 node.js gruntjs watch uglifyjs

我的 Gruntfile.js 中有以下配置: 问题是,当某些文件被更改时,'uglify' 任务对所有文件照常执行。我做错了什么?

module.exports = function(grunt) {
    grunt.initConfig({
        pkg     : grunt.file.readJSON('package.json'),
        watch: {
            scripts: {
                files: ['js/**/*.js'],
                tasks: ['uglify']
            }
        },
        uglify  : {
            options: {
                banner: '/*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> */\n',
                sourceMapRoot: 'www/js/sourcemap/'
            },
            build: {
                files: [
                    {
                        expand: true,
                        cwd: 'js/',
                        src: ['**/*.js', '!**/unused/**'],
                        dest: 'www/js/',
                        ext: '.js'
                    }
                ]
            }
        }
    });

    grunt.loadNpmTasks('grunt-contrib-watch');
    grunt.loadNpmTasks('grunt-contrib-uglify');

    grunt.event.on('watch', function(action, filepath) {
        grunt.config(['uglify', 'build', 'src'], filepath);
    });

    grunt.registerTask('default', ['uglify', 'watch']);
};

最佳答案

默认情况下,watch 任务会生成任务运行。因此它们处于不同的进程上下文中,因此在监视事件上设置配置将不起作用。您需要启用 nospawn 才能不生成任务运行并保持在同一上下文中:

watch: {
  options: { nospawn: true },
  scripts: {
    files: ['js/**/*.js'],
    tasks: ['uglify']
  }
},

关于node.js - 如果启用了 Uglify 中的动态扩展,如何使用 Grunt.js 监视任务只对更改的文件进行 uglify?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16836076/

相关文章:

json - 使用反斜杠解析 JSON Node.js

css - 如何用 grunt 递归编译 less/sass 文件

css - 我可以配置一个 grint-contrib-less 任务来编译成并行结构吗?

bash - gnu watch : justify on the lower left of the terminal

c - 警告:从不兼容的指针类型传递''的参数1 [默认启用]

bash - 如何将 `watch' 命令添加到 shell 脚本?

node.js - 为 Sequelize.js 中连接/包含实体的嵌套和/或表达式生成 SQL

javascript - 向特定的套接字 ID 发送私有(private)消息

gruntjs - JSX 转换器使用 React.createElement ("h1", null) 而不是 React.DOM.h1(null)

node.js - Express 验证器 - 如何允许可选字段