【问题标题】:Grunt.js watch - node_modules always being watchedGrunt.js watch - node_modules 总是被监视
【发布时间】:2015-08-05 12:56:51
【问题描述】:

我仅将 Grunt 用于 livereload。它工作正常,但我注意到它的 CPU 很高,当我使用“--verbose”运行它时,我发现它正在监视整个“node_modules”文件夹。

所以,我做了一些研究,并试图忽略这一点。不幸的是,没有成功。

我对“gruntfile.js”的关注部分是:

    // the watch stuff ..
    watch: {
        all: {
            files: ['!**/node_modules/**', 'js/**/*.js', 'css/**/*.css', 'index.html'],
            options: {
                interval: 5007,
                livereload: true
            }
        }
    },

基本上我是说我想让 grunt 观看所有的 js、css 和 index.html 文件。显式添加了忽略“node_modules”的代码,但它仍然说它正在监视它并且 CPU 运行大约 30%。 (Mac OSx)

===================

我注意到了一件事:

当我在“gruntfile.js”中进行更改时——例如向“watch”任务的“files”属性中添加一个文件——然后它会重新启动 grunt,并且在控制台中我看到它开始观看只有我想要的文件,然后 CPU 低于 1%。 (我猜应该是原来的样子。)

我做错了什么?

=====================

编辑:不幸的是,当我更改 gruntfile 时,我只看到我想要的文件正在被观看 - 然后 livereload 的东西不再工作。

=====================

这是我开始的文章: http://thecrumb.com/2014/03/15/using-grunt-for-live-reload/

这是我的 package.json 文件:

{
  "name": "grunt-reload",
  "version": "1.0.0",
  "devDependencies": {
        "grunt": "~0.4.3",
        "matchdep": "~0.3.0",
        "grunt-express": "~1.2.1",
        "grunt-contrib-watch": "~0.6.0",
        "grunt-open": "~0.2.3"
  }
}

这是我的 Gruntfile.js:

module.exports = function(grunt) {

    require('matchdep')
        .filterDev('grunt-*')
        .forEach(grunt.loadNpmTasks);

    grunt.initConfig({

        // the web server ..
        express: {
            all: {
                options: {
                    bases: [__dirname],
                    port: 8888,
                    hostname: 'localhost',
                    livereload: true
                }
            }
        },

        // the watch stuff ..
        watch: {
            all: {
                files: ['js/**/*.js', 'css/**/*.css', 'index.html'],
                options: {
                    livereload: true
                }
            }
        },

        // the automatic opening stuff ..
        open: {
            all: {
                path: 'http://localhost:8888/index.html'
            }
        }
    });

    // create the server task ..
    grunt.registerTask(
        'server',
        ['express', 'open', 'watch']
    );

}; // end of "module.exports" ..

我从“grunt server”开始。

【问题讨论】:

    标签: gruntjs livereload node-modules


    【解决方案1】:

    编辑:在你分享你的 Gruntfile 之后,问题变得更清晰了。

    在您的grunt-express 配置中,您已将livereload 设置为true,并将bases 设置为__dirname,这是运行中的文件夹Gruntfile

    现在,让我们看看grunt-express 文档:

    基地

    类型:字符串|数组默认值:null

    将提供静态文件的基本(或根)目录。将为每个基础条目生成一个 connect.static()。 当 livereload 设置为 true(或设置为特定端口号)时,将创建一个监视任务(在运行时)以监视您的 basePath/**/*.*

    (强调我的)

    因此,在您的 grunt-express 配置中,您将 livereload 设置为监视基本路径中的所有文件,其中当然包括 node_modules

    你有几个选择:

    1. 删除您的其他watch 任务并相应地配置grunt-expresses 基本路径(基本上只需复制您的其他配置)。

    2. 保留两个监视任务,并忽略另一个 grunt-express -> bases 配置中的 node_modules

    3. 删除 bases 并在另一个 watch 任务中处理 livereloading


    node_modules 文件夹在哪里?如果它在 root 上,只要没有任何匹配的 glob 模式,您就可以完全删除 node_modules 参数:

    // the watch stuff ..
    watch: {
        all: {
            files: ['js/**/*.js', 'css/**/*.css', 'index.html'],
            options: {
                interval: 5007,
                livereload: true
            }
        }
    },
    

    现在您正在观看:js 文件夹下的所有.js 文件、根目录下的index.html 等等。

    但是,如果您在 js/node_modules 下有 node_modules,则可以显式忽略该文件夹及其中的文件:

     files: ['!js/node_modules/**', 'js/**/*.js', 'css/**/*.css', 'index.html'],
    

    无论如何,根据您的 Gruntfiles 和 node_module-folders 位置,您的配置应该可以正常工作。

    【讨论】:

    • 'node_modules' 在根目录中。目前我只指定了“js”和“css”文件夹中的文件,以及 index.html 文件 - 要被监视,但它仍然会监视所有文件并且 CPU 会变高。可能是“全部”这个词,这就是为什么它绝对在根目录中观察一切? ...
    • 不应该 与目标名称有任何关系,并且查看源我也找不到任何其他指向该方向的东西......你没有配置中碰巧有其他watch-targets而不是all?很奇怪。
    • 我添加了“package.json”和“Gruntfile.js”文件的内容,希望能提供更多相关信息。
    • 谢谢,这有助于理解问题:) ...(我刚刚从 express 配置中删除了“livereload:true”),现在一切正常 - 小型 CPU 和观看 + livereload 功能启动并运行:) .
    • (顺便说一句,原始文章中的 cmets 已禁用 :( ...而且我无法添加评论,因此其他人不会遇到同样的问题。)
    猜你喜欢
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-17
    • 2019-02-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多