在gruntjs中使用常量

Sas*_*nga 1 gruntjs

我正在尝试编写我的第一个Grunt任务来复制我的公共libs文件夹中的一些文件,这些文件不在我的项目目录中.

  • 项目文件夹: /home/user/projects/bottle
  • Common Libs目录: /home/user/projects/common
  • 文件的源位于Common Libs目录中: lib/general/static/js/
  • 项目文件夹中的文件目标: lib

我有一个properties.json带有Common Libs目录路径的文件,如下所示

{
    "common_libs" : `/home/user/projects/common`
}
Run Code Online (Sandbox Code Playgroud)

现在我已经尝试过的是:

module.exports = function(grunt) {

    var properties = grunt.file.readJSON('properties.json'),
        paths = {
            common_libs : properties.common_libs,
            common_libs_js : this.common_libs + "lib/general/static/js/"
        };

    grunt.initConfig({
        copy: {
            main: {
                files: [
                    {
                        expand: true,
                        flatten : true,
                        src: [
                            paths.common_libs_js + "/*"
                        ],
                        dest: 'lib/',
                        filter: 'isFile'
                    }
                ]
            }
        }
    });

    grunt.loadNpmTasks('grunt-contrib-copy');
};
Run Code Online (Sandbox Code Playgroud)

我按照以下方式运行咕噜声 grunt copy

这样就没有文件被复制到目的地.

帮助我.

我也想知道

  1. 如何在GruntJS中使用Ant的属性标记类型常量?因为我从properties.json获取基本文件夹,我需要从基本文件夹下的不同文件夹中复制许多文件.
  2. 我们可以为每个任务提供这些类型的常量吗?

提前致谢.

Sim*_*nen 8

您的代码存在一些问题.第一:

common_libs_js : this.common_libs + "lib/general/static/js/"
Run Code Online (Sandbox Code Playgroud)

不幸的this.common_libs是未定义(this不指向你认为它的位置),所以common_libs_js最终存在'undefinedlib/general/static/js/',这是行不通的.

第二个问题是在同一行上你是连接路径,但是第一个路径(来自属性文件)似乎没有以斜杠结束,'/home/user/projects/commonlib/general/static/js/'如果它不是前一个问题就会变成.

第三,你会在你的dest路径中获得一大堆文件夹.使用expand选项时,Grunt使用src属性中给出的路径来创建文件夹结构.如果要/home/user/projects/common/lib/general/static/js/foo.js复制到lib/foo.js,则应将cwd选项设置为paths.common_libs_js和src '*.js'(或'**/*.js'任何级别的匹配).

人们通常在Grunt的配置中嵌入配置属性,然后使用模板字符串来访问它们.编写任务的一种非常常见的方式是这样的(通过一些更改,根据需要进行调整):

module.exports = function(grunt) {

    grunt.initConfig({
        properties: grunt.file.readJSON('properties.json'),
        copy: {
            main: {
                expand: true,
                cwd: '<%= properties.common_libs %>/lib/general/static/js',
                src: '**/*.js',
                dest: 'lib'
            }
        }
    });

    grunt.loadNpmTasks('grunt-contrib-copy');
};
Run Code Online (Sandbox Code Playgroud)

或者,如果您希望文件lib/general/static/js/也在目标路径的' '中结束:

module.exports = function(grunt) {

    grunt.initConfig({
        properties: grunt.file.readJSON('properties.json'),
        copy: {
            main: {
                expand: true,
                cwd: '<%= properties.common_libs %>',
                src: 'lib/general/static/js/**/*.js',
                dest: '.' // because src includes 'lib'
            }
        }
    });

    grunt.loadNpmTasks('grunt-contrib-copy');
};
Run Code Online (Sandbox Code Playgroud)

如果您不确定Grunt如何看待您的文件,请运行它grunt -v并告诉您.

您可能还想考虑非Grunt解决方案的git子模块.

对于特定于任务的常量:您可以使用任务的(或目标)options哈希,并使用模板字符串访问它:

module.exports = function(grunt) {

    grunt.initConfig({
        properties: grunt.file.readJSON('properties.json'),
        copy: {
            options: {
                foo: 'lib'
            },
            main: {
                options: {
                    bar: '**/*.js'
                },
                expand: true,
                cwd: '<%= properties.common_libs %>/<%= copy.options.foo %>/general/static/js',
                src: '<%= copy.options.main.bar %>',
                dest: 'lib'
            }
        }
    });

    grunt.loadNpmTasks('grunt-contrib-copy');
};
Run Code Online (Sandbox Code Playgroud)

但是,对于除实际选项之外的任何其他内容,很少这样做.可能会发生与实际期权的冲突.您也可以直接使用目标命名空间,直接在里面设置属性main.但同样,有一些属性名称可能会发生冲突.

如果要覆盖属性(例如,对于发布版本),可以执行以下操作:

module.exports = function(grunt) {

    grunt.registerTask('release', function() {
        grunt.config.set('properties.common_libs', '/usr/lib/shared');
    });

    grunt.initConfig({
        properties: grunt.file.readJSON('properties.json'),
        copy: {
            main: {
                expand: true,
                cwd: '<%= properties.common_libs %>/lib/general/static/js',
                src: '**/*.js',
                dest: 'lib'
            }
        }
    });

    grunt.loadNpmTasks('grunt-contrib-copy');
};
Run Code Online (Sandbox Code Playgroud)

那你就叫你的任务了grunt release copy.

编辑

根据您更新的问题,该properties.json文件似乎对您没有多大用处.为什么不在Gruntfile中指定属性?

module.exports = function(grunt) {

    grunt.initConfig({
        properties: {
            base_dir: '../common',
            base_js_dir: '<%= properties.base_dir %>/lib/general/static/js',
            base_css_dir: '<%= properties.base_dir %>/lib/general/static/css'
        },
        copy: {
            main: {
                expand: true,
                cwd: '<%= properties.base_js_dir %>',
                src: '**/*.js',
                dest: 'lib'
            }
        }
    });

    grunt.loadNpmTasks('grunt-contrib-copy');
};
Run Code Online (Sandbox Code Playgroud)

如果需要,您也可以将此文件与此一起使用.或者甚至将这些属性(带有模板字符串)放在properties.json文件中.最后,只需要让Grunt看到一个带有模板字符串的对象.这取决于你以某种方式提供.