node.js 如何忽略文件 grunt uglify

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18453974/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:25:53  来源:igfitidea点击:

How to ignore files grunt uglify

javascriptnode.jsgruntjsuglifyjs

提问by Jamie Hutber

Background

背景

I've just started using grunt as of about 30mins ago. So bear with me.

大约 30 分钟前,我刚刚开始使用 grunt。所以请耐心等待。

But I have a rather simple script going that will look at my js and then compress it all into one file for me.

但是我有一个相当简单的脚本,它会查看我的 js,然后将其全部压缩到一个文件中。

Code

代码

"use strict";
module.exports = function (grunt) {

    // load all grunt tasks
    require('matchdep').filterDev('grunt-*').forEach(grunt.loadNpmTasks);

    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        uglify: {
            options: {
                beautify: true,
                report: 'gzip'
            },
            build: {
                src: ['docroot/js/*.js', 'docroot/components/pages/*.js', 'docroot/components/plugins/*.js'],
                dest: 'docroot/js/main.min.js'
            }
        },
        watch: {
            options: {
                dateFormat: function(time) {
                    grunt.log.writeln('The watch finished in ' + time + 'ms at' + (new Date()).toString());
                    grunt.log.writeln('Waiting for more changes...');
                }
            },
            js: {
                files: '<%= uglify.build.src %>',
                tasks: ['uglify']
            }
        }
    });

    grunt.registerTask('default', 'watch');

}

Question

My main.min.js is getting included in the compile each time. Meaning my min.js is getting 2x, 4x, 8x, 16x etc etc. Is best way around this is to add an exception and ignore main.min.js?

我的 main.min.js 每次都包含在编译中。这意味着我的 min.js 得到了 2x、4x、8x、16x 等。最好的方法是添加一个异常并忽略main.min.js

回答by Martin Hansen

To the endof the src array, add

在 src 数组的末尾,添加

'!docroot/js/main.min.js'

This will exclude it. The ! turns it into an exclude.

这将排除它。这 !把它变成一个排除。

http://gruntjs.com/api/grunt.file#grunt.file.expand

http://gruntjs.com/api/grunt.file#grunt.file.expand

Paths matching patterns that begin with ! will be excluded from the returned array. Patterns are processed in order, so inclusion and exclusion order is significant.

匹配以 ! 开头的模式的路径 将从返回的数组中排除。模式按顺序处理,因此包含和排除顺序很重要。

This is not specific to grunt uglify, but any task that uses grunt convention for specifying files will work this way.

这不是 grunt uglify 特有的,但任何使用 grunt 约定指定文件的任务都可以这样工作。

As a general advice though I would suggest putting built files somewhere else than your source files. Like in a root distfolder.

作为一般建议,我建议将构建的文件放在源文件之外的其他地方。就像在根dist文件夹中一样。