你的代碼有幾個問題。第一:
common_libs_js : this.common_libs + "lib/general/static/js/"
不幸的是this.common_libs
未定義(this
沒有指向,你認爲它),所以common_libs_js最終被'undefinedlib/general/static/js/'
,這是行不通的。
第二個問題是在同一行上連接路徑,但第一個路徑(來自屬性文件)似乎不以斜線結尾,並且如果它不是以前的路徑將變爲'/home/user/projects/commonlib/general/static/js/'
問題。
第三,你會得到你的dest路徑中的一大堆文件夾。當使用擴展選項時,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');
};
另外,如果你希望你的文件「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');
};
如果你不知道咕嚕如何看待您的文件,與grunt -v
運行它,它會告訴你。
您可能還想考慮GUN子模塊的非Grunt解決方案。
有關特定任務的常量:您可以使用任務(或目標的)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');
};
這是很少超過實際選擇其他任何事情,雖然。與真實選項衝突可能發生。您也可以直接使用目標命名空間,直接在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');
};
然後,你打電話給你的任務與。
編輯
基於更新後的問題,它似乎並不像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');
};
如果需要,您也可以將此文件與此一起使用。甚至可以將這些屬性(使用模板字符串)放入properties.json
文件中。最後,它只是讓Grunt用模板字符串看到一個對象。以某種方式提供這個由你決定。
謝謝@ simo-kinnunen。感謝您的詳細解釋和糾正我。我在看的是,有什麼我可以從'properties.common_libs'創建另一個簡單變量,以便我不需要在'cwd'中指定一個長名稱。我想'cwd'採取一個簡單的常量,像'properties.common_libs_js'。因爲我有許多其他文件需要從相同的'properties.common_libs_js'文件夾中複製。在Ant中,我們從現有的屬性創建一個新的屬性並使用該快捷方式。我正在尋找一個類似的。我們可以在GruntJS中實現嗎? –
更清楚地更新了問題。 –
感謝Simo Kinnunen。 –