Terminal Grunt-从命令行传递文件名变量
我正在努力理解如何从grunt命令行传递部分文件名,以便在特定文件上运行任务(从安装的grunt模块) 我想能够做的是配置一系列任务,从命令行获取filename参数 我试着在这一页上重做最后一个例子,但我有点在暗中捅了一刀。我以为有人会很快回答 这是我的Grunfile文件:Terminal Grunt-从命令行传递文件名变量,terminal,gruntjs,command-line-arguments,Terminal,Gruntjs,Command Line Arguments,我正在努力理解如何从grunt命令行传递部分文件名,以便在特定文件上运行任务(从安装的grunt模块) 我想能够做的是配置一系列任务,从命令行获取filename参数 我试着在这一页上重做最后一个例子,但我有点在暗中捅了一刀。我以为有人会很快回答 这是我的Grunfile文件: module.exports = function (grunt) { grunt.initConfig({ globalConfig: globalConfig, uglify:
module.exports = function (grunt) {
grunt.initConfig({
globalConfig: globalConfig,
uglify: {
js: {
options: {
mangle: true
},
files: {
'js/<%= globalConfig.file %>.min.js': ['js/<%= globalConfig.file %>.js']
}
}
},
});
// Load tasks so we can use them
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.registerTask('go', 'Runs a task on a specified file', function (fileName){
globalConfig.file = fileName;
grunt.task.run('uglify:js');
});
};
以js/app.js为目标
我得到这个错误:
Aborted due to warnings.
roberts-mbp:150212 - Grunt Tasks robthwaites$ grunt go:app
Loading "Gruntfile.js" tasks...ERROR
>> ReferenceError: globalConfig is not defined
Warning: Task "go:app" not found. Use --force to continue.
谢谢您可以使用grunt.option 您的grunt注册任务将如下所示
> grunt.option('fileName'); grunt.registerTask('go', 'Runs a task on a
> specified file', function (){
> grunt.task.run('uglify:js');
> });
您的grunt配置将是
module.exports = function (grunt) {
var fileName=grunt.option('fileName');
grunt.initConfig({
uglify: {
js: {
options: {
mangle: true
},
files: {
'js/fileName.min.js': ['js/fileName.js']
}
}
},
});
从终端运行任务的命令:
$grunt go--fileName='xyzfile'
我最终能够像这样完成我想要的,但不确定这是否是一种标准的方式 我没有做到的是首先全局声明globalConfig变量,以便在运行grunt任务时从终端重新定义它 这里有一个例子。处理HTML电子邮件时,我需要:
grunt
,它将只运行sass任务,该任务处理所有sass文件-不需要terminal的文件参数
但是,如果我希望在单个html文件上运行一系列进程,则输入grunt process:fileName
,其中fileName是html文件的名称,扩展名不带.html
您会注意到,唯一需要文件名的任务实际上包括replace和premailer。但是,我仍然希望在定位所选文件之前运行所有其他CSS清理任务
关键是:
module.exports = function (grunt) {
var globalConfig = {
file: 'index' // this is the default value, for a single project.
}
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
// load the globalConfig variables
globalConfig: globalConfig,
sass: {
dev: {
files: [{
expand: true,
cwd: 'scss',
src: ['*.scss'],
dest: 'css',
ext: '.css'
}]
}
},
cssmin: {
options: {
keepSpecialComments: 0,
keepBreaks: true,
advanced: false
},
target: {
files: [{
expand: true,
cwd: 'css',
src: '*.css',
dest: 'css',
ext: '.css'
}]
}
},
autoprefixer: {
css: {
src: "css/*.css"
}
},
includereplace: {
your_target: {
options: {
prefix: '\\/\\* ',
suffix: ' \\*\\/',
},
files: {
'inline/<%= globalConfig.file %>-inline.html': ['<%= globalConfig.file %>.html']
}
}
},
premailer: {
main: {
options: {
verbose: true,
preserveStyles: true,
},
src: 'inline/<%= globalConfig.file %>-inline.html',
dest: 'inline/<%= globalConfig.file %>-inline.html'
}
},
});
grunt.loadNpmTasks('grunt-contrib-sass');
grunt.loadNpmTasks('grunt-autoprefixer');
grunt.loadNpmTasks('grunt-include-replace');
grunt.loadNpmTasks('grunt-premailer');
grunt.registerTask('default', 'sass');
grunt.registerTask('process', 'Runs all processing tasks on a specific file to produce inlined file', function (fileName) {
globalConfig.file = fileName;
grunt.task.run('sass', 'autoprefixer', 'cssmin', 'includereplace', 'premailer');
});
module.exports=函数(grunt){
变量globalConfig={
file:'index'//这是单个项目的默认值。
}
grunt.initConfig({
pkg:grunt.file.readJSON('package.json'),
//加载globalConfig变量
globalConfig:globalConfig,
sass:{
开发人员:{
档案:[{
是的,
cwd:‘scss’,
src:['*.scss'],
dest:'css',
分机:'.css'
}]
}
},
cssmin:{
选项:{
KeepsSpecialComments:0,
keepBreaks:没错,
高级:错误
},
目标:{
档案:[{
是的,
cwd:‘css’,
src:'*.css',
dest:'css',
分机:'.css'
}]
}
},
自动刷新器:{
css:{
src:“css/*.css”
}
},
包括地点:{
你的目标:{
选项:{
前缀:'\/\\*',
后缀:“\\*\\/”,
},
档案:{
'inline/-inline.html':['.html']
}
}
},
预卷机:{
主要内容:{
选项:{
没错,
是的,
},
src:'inline/-inline.html',
dest:'inline/-inline.html'
}
},
});
grunt.loadNpmTasks(“grunt-contrib-sass”);
grunt.loadNpmTasks('grunt-autoprefixer');
grunt.loadNpmTasks('grunt-include-replace');
grunt.loadNpmTasks(“grunt-premailer”);
registerTask('default','sass');
registerTask('process','运行特定文件上的所有处理任务以生成内联文件',函数(文件名){
globalConfig.file=文件名;
run('sass','autoprefixer','cssmin','includereplace','premailer');
});
}
EDIT:显然,目前它只接受我提供的一个参数。在其他用例中,上面的grunt.option版本可以提供更多功能,能够在一个命令中提交多个参数。如果我觉得有必要的话,我会继续尝试grunt.option。Hi我找到了一个语法稍微简单一点的解决方案,至少可以从终端运行命令。我想知道你对此有何看法?无论如何,这是另一种解决办法。
module.exports = function (grunt) {
var globalConfig = {
file: 'index' // this is the default value, for a single project.
}
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
// load the globalConfig variables
globalConfig: globalConfig,
sass: {
dev: {
files: [{
expand: true,
cwd: 'scss',
src: ['*.scss'],
dest: 'css',
ext: '.css'
}]
}
},
cssmin: {
options: {
keepSpecialComments: 0,
keepBreaks: true,
advanced: false
},
target: {
files: [{
expand: true,
cwd: 'css',
src: '*.css',
dest: 'css',
ext: '.css'
}]
}
},
autoprefixer: {
css: {
src: "css/*.css"
}
},
includereplace: {
your_target: {
options: {
prefix: '\\/\\* ',
suffix: ' \\*\\/',
},
files: {
'inline/<%= globalConfig.file %>-inline.html': ['<%= globalConfig.file %>.html']
}
}
},
premailer: {
main: {
options: {
verbose: true,
preserveStyles: true,
},
src: 'inline/<%= globalConfig.file %>-inline.html',
dest: 'inline/<%= globalConfig.file %>-inline.html'
}
},
});
grunt.loadNpmTasks('grunt-contrib-sass');
grunt.loadNpmTasks('grunt-autoprefixer');
grunt.loadNpmTasks('grunt-include-replace');
grunt.loadNpmTasks('grunt-premailer');
grunt.registerTask('default', 'sass');
grunt.registerTask('process', 'Runs all processing tasks on a specific file to produce inlined file', function (fileName) {
globalConfig.file = fileName;
grunt.task.run('sass', 'autoprefixer', 'cssmin', 'includereplace', 'premailer');
});