can*_*era 12 javascript gruntjs
如何访问Grunt配置属性site以project.json在config属性值指定的路径上读取文件?
grunt.registerTask('build', function(target) {
grunt.config('site', target);
grunt.task.run('foo:dist', 'bar:dist');
});
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
site: grunt.file.readJSON('./sites/' + grunt.config('site') + '/project.json')
});
Run Code Online (Sandbox Code Playgroud)
咕噜-CLI:
grunt build:sitename
>> Error: Unable to read "./sites/undefined/project.json"
Run Code Online (Sandbox Code Playgroud)
使用文档中的示例,我也试过这个:
grunt.registerTask('global', 'site', function(prop, value) {
global[prop] = val;
});
grunt.registerTask('build', ['foo:dist', 'bar:dist']);
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
site: grunt.file.readJSON('./sites/' + global.site + '/project.json')
});
Run Code Online (Sandbox Code Playgroud)
咕噜-CLI:
grunt global:site:sitename
>> Error: Unable to read "./sites/undefined/project.json"
Run Code Online (Sandbox Code Playgroud)
更新:
使用@FelixKling答案作为指导,我取得了一些进展:
grunt.registerTask('build', function(target) {
grunt.config.set('target', target);
grunt.config.set('site', grunt.file.readJSON('./sites/' + grunt.config.get('target') + '/project.json'));
grunt.task.run('watch');
});
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
site: grunt.config.get('site'),
watch: {
sass: {
files: ['<%= site.dev %>/scss/*.scss'],
tasks: ['sass:dist']
}
},
sass: {
dist: {
files: {
'<%= site.dist %>/style.css': '<%= site.dev %>/scss/style.scss'
}
}
},
});
Run Code Online (Sandbox Code Playgroud)
现在我能够project.json成功读取文件,并且(不知何故)它甚至能够识别何时对观看文件进行编辑.但由于某些原因,当它运行sass:dist任务时,我收到此错误:Warning: An error occurred while processing a template (Cannot read property 'dev' of undefined).
我不清楚watch任务如何能够获得正确的值site,但更重要的是,我需要找到一种方法来获得相同的sass任务值.
Fel*_*ing 17
initConfig并grunt.file.readJSON在任务运行之前运行.看起来你需要的是模板字符串,你只能grunt.file.readJSON在实际拥有目标名称时调用.
例如:
grunt.registerTask('build', function(target) {
grunt.config.set('target', target);
grunt.config.set('site', grunt.file.readJSON(grunt.config.get('path'));
grunt.task.run('foo:dist', 'bar:dist');
});
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
path: './sites/<%= target %>/project.json'
});
Run Code Online (Sandbox Code Playgroud)
更多信息:http://gruntjs.com/api/grunt.config
关于你的更新:你基本上犯了同样的错误,就像你在第一个例子中所做的那样.您正在尝试site在设置之前访问配置.
您必须了解初始化步骤,即在任何相关代码运行之前grunt.initConfig发生:
Initialize config -> Run task
Run Code Online (Sandbox Code Playgroud)
让我们grunt.initConfig孤立地看一下:
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
site: grunt.config.get('site'),
});
Run Code Online (Sandbox Code Playgroud)
这是初始化步骤,在其他所有步骤之前发生.首先计算传递给initConfig配置对象的参数.您在此处尝试执行的操作是site在创建配置之前访问配置选项.我希望你认识到这没有意义.
如果你grunt.initConfig在注册任何任务之前放在最顶端,也许它可以帮助你理解这个过程.
解决方案:
我认为您实际可能会遇到的是命令行参数,您可以使用它来控制要构建的站点.有关grunt.option更多信息,请参阅
例如:
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
watch: {
sass: {
files: ['<%= site.dev %>/scss/*.scss'],
tasks: ['sass:dist']
}
}
});
grunt.config.set('site', grunt.file.readJSON('./sites/' + grunt.option('site') + '/project.json'));
Run Code Online (Sandbox Code Playgroud)
然后你运行任务
grunt watch --site=somesite
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10561 次 |
| 最近记录: |