在gruntjs中使用常量 [英] Using constants in gruntjs
问题描述
- Common Libs目录:
/ home / user / projects / common
- 文件源位于Common Libs目录下:
lib / general / static / js / $
- 文件目标在项目文件夹内:
lib
- 文件目标在项目文件夹内:
我试图编写我的第一个Grunt任务来从我的公共libs文件夹中复制一些文件, li>工程文件夹: / home / user / projects / bottle
我有一个具有Common Libs目录路径的 properties.json
文件,如下所示:
< pre $
{
common_libs:`/ home / user / projects / common`
}
现在我已经尝试过了:
module.exports = function (grunt){
var properties = grunt.file.readJSON('properties.json'),
paths = {
common_libs:properties.common_libs,
common_libs_js :thi s.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');
};
我正在运行grunt,如下所示
grunt copy
$ b $ p
$ b
没有文件被复制到目的地。
帮助我。 / p>
另外我想知道如何使用Ant的属性标签类型常量在GruntJS?因为我从properties.json获取基础文件夹,我需要从基础文件夹下的不同文件夹复制许多文件。
在此先感谢。
是你的代码的几个问题。首先:
pre $ common $ lib $ common $ lib $ / $ c>
不幸 第二个问题是在同一行上连接路径,但第一个路径(来自属性文件)似乎不以斜线结尾,如果不是针对上一个问题,它将成为 第三,你会在你的dest路径中得到一大堆文件夹。当使用扩展选项时,Grunt使用src属性中给出的路径来创建文件夹结构。如果要将 人们通常在Grunt的配置中嵌入配置属性,然后使用模板字符串来访问它们。编写任务的一种非常常见的方式就是像这样(有一些变化,根据需要调整): 或者,如果您希望文件以' 如果您不确定Grunt如何看到您的文件,请使用 你可能还想考虑一个非Grunt解决方案的git子模块。 对于特定于任务的常量:您可以使用任务的(或目标的) 然而,除了实际选项以外,这种情况很少做。与真实选项冲突可能发生。您也可以直接使用目标命名空间,直接在 如果您想覆盖属性(例如发布版本),可以这样做: 然后你可以用 根据您更新的问题,看起来像 如果需要,您也可以将此文件与此一起使用。甚至可以将这些属性(使用模板字符串)放入 I am trying to write my first Grunt task to copy some files from my common libs folder, which is out of my project directory. I have a Now what I already tried is : I am running grunt as follows
With this no files are copied to the destination. Help me in this. Also I want to know Thanks in advance. There are a few issues with your code. First: Unfortunately The second problem is that on that same line you are concatenating paths, but the first path (from the properties file) doesn't seem to end with a slash, and would become Third, you'd get a whole bunch of folders inside your dest path. When the expand option is used, Grunt uses the paths as given in the src property to create the folder structure. If you want People usually embed configuration properties inside Grunt's config, and then use template strings to access them. A very common way to write your task would be something like this (with a few changes, adjust as needed): Alternatively, if you want your files to end up in ' If you're not sure how Grunt sees your files, run it with You might also want to consider git submodules for a non-Grunt solution. For task-specific constants: you could use the task's (or target's) This is rarely done for anything other than actual options, though. Conflicts with real options can occur. You could also use the target namespace directly, setting the property directly inside If you want to override properties (e.g. for a release build), you can do this: Then you'd call your task with EDIT Based on your updated question, it doesn't seem like the You can also use the file together with this, if you want. Or even put these properties (with the template strings) inside the 这篇关于在gruntjs中使用常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! this.common_libs
未定义( this
并不指向你认为它的位置),所以common_libs_js最终是
'undefinedlib / general / static / js /'
,它不会工作。
'/ home / user / projects / commonlib / general / static / js /'
。
/home/user/projects/common/lib/general/static/js/foo.js
复制到 lib / foo。 js
,您应该将 cwd
选项设置为 paths.common_libs_js
,并将src设置为'*。js'
(或者'** / *。js'
p>
模块。出口=功能(grunt){
grunt.initConfig({
属性:grunt.file.readJSON('properties.json'),
复制:{
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:'。'//因为src包含'lib'
}
}
});
grunt.loadNpmTasks('grunt-contrib-copy');
};
grunt -v
,它会告诉你。
选项
散列,并使用模板字符串访问它:
module.exports = function(grunt){
grunt.initConfig({
properties:grunt.file.readJSON('properties.json'),
copy:{
options:{
foo:'lib'
},
main :{
options:{
bar:'** / *。js'
},
展开:true,
cwd:'<%= properties。 common_libs%> /<%= copy.options.foo%> / general / static / js',
src:'<%= copy.options.main.bar%&g t;',
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');
};
grunt release copy $ c
$ b $ 编辑
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'
},
复制:{
main:{
expand:true,
cwd:'<%= properties.base_js_dir%>',
src:'** / *。js',
dest:'lib'
}
}
});
grunt.loadNpmTasks('grunt-contrib-copy');
};
properties.json
文件中。最后,它只是让Grunt用模板字符串看到一个对象。不管怎样,这取决于你。
/home/user/projects/bottle
/home/user/projects/common
lib/general/static/js/
lib
properties.json
file with Common Libs directory path as shown below{
"common_libs" : `/home/user/projects/common`
}
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');
};
grunt copy
common_libs_js : this.common_libs + "lib/general/static/js/"
this.common_libs
is undefined (this
doesn't point to where you think it does), so common_libs_js ends up being 'undefinedlib/general/static/js/'
, which doesn't work.'/home/user/projects/commonlib/general/static/js/'
if it wasn't for the previous issue./home/user/projects/common/lib/general/static/js/foo.js
to be copied to lib/foo.js
, you should set the cwd
option to paths.common_libs_js
and the src to '*.js'
(or '**/*.js'
for a match on any level).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/
' in the destination path too: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
and it will tell you.options
hash for that, and access it with template strings: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
. But again, there are a few property names that may conflict.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');
};
grunt release copy
.properties.json
file is of much use to you. Why not just specify the properties in your 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
file. In the end it's just a matter of making Grunt see an object with template strings. It's up to you to provide that somehow.