我使用Grunt和node.js,並試圖讓Grunt告訴我它在Gruntfile.js中發現錯誤的位置。這似乎很奇怪,它不會默認這樣做。Grunt:獲取行號錯誤
對於grunt watch
,我得到這個錯誤:
Loading "Gruntfile.js" tasks...ERROR
>> SyntaxError: Unexpected string
Warning: Task "watch" not found. Use --force to continue.
我很想知道,有意想不到的字符串是在哪一行,但我不能讓咕嚕地告訴我。
當我嘗試grunt watch --stack
時,它會顯示節點文件的整個node.js堆棧跟蹤,但不會顯示Gruntfile.js中的錯誤源。
將grunt.option('stack', true);
添加到Gruntfile.js的頂部,如this answer中所建議的,不起任何作用。
我Gruntfile.js:
var path_dev = 'app/assets/_dev';
var path_live = 'app/assets';
var url_dev = '//assets.domain.dev/_dev';
module.exports = function (grunt) {
grunt.option('stack', true);
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
banner: '/*!\n' +
'* <%= pkg.name %> - v<%= pkg.version %> - <%= grunt.template.today("yyyy-mm-dd") %> - Copyright <%= grunt.template.today("yyyy") %> \n' +
'* @author <%= pkg.author %>\n' +
'*/',
path_dev: path_dev,
path_live: path_live,
url_dev: url_dev,
clean: {
build: {
src: [
'<%= path_live %>/css/',
'<%= path_live %>/js/'
]
}
},
concat: {
options: {
separator: ';'
},
build: {
src: [
'<%= path_dev %>/js/core/jquery.min.js',
'<%= path_dev %>/js/core/bootstrap.min.js',
'<%= path_dev %>/js/core/jquery.slimscroll.min.js',
'<%= path_dev %>/js/core/jquery.scrollLock.min.js',
'<%= path_dev %>/js/core/jquery.appear.min.js',
'<%= path_dev %>/js/core/jquery.countTo.min.js',
'<%= path_dev %>/js/core/jquery.placeholder.min.js',
'<%= path_dev %>/js/core/js.cookie.min.js',
'<%= path_dev %>/js/main.js'
],
dest: '<%= path_live %>/js/global.js'
}
},
uglify: {
options: {
banner: '<%= banner %>'
},
build: {
files: {
'<%= path_live %>/js/global.js': ['<%= path_live %>/js/global.js']
}
}
},
copy: {
build: {
files: [
{
expand: true,
cwd: '<%= path_dev %>/css/',
src: 'bootstrap.min.css',
dest: '<%= path_live %>/css/'
},
{
expand: true,
cwd: '<%= path_dev %>/fonts/',
src: '**',
dest: '<%= path_live %>/fonts/'
},
{
expand: true,
cwd: '<%= path_dev %>/img/',
src: '**',
dest: '<%= path_live %>/img/'
},
{
expand: true,
cwd: '<%= path_dev %>/js/plugins/',
src: '**',
dest: '<%= path_live %>/js/plugins/'
}
]
}
}
});
var tasks_to_watch = [];
var less = {};
var watch = {};
//less:build
less['build'] = {
options: {
compress: true,
yuicompress: true,
optimization: 2,
},
files: {}
};
less['build']['files'][path_live+'/css/global.css'] = path_dev'/less/main.less';
less['build']['files'][path_live+'/css/apps/**/*.css'] = path_dev'/less/apps/**/*.less';
//less:dev_main
less['dev_main'] = {
options: {
sourceMap: true,
sourceMapURL: url_dev+'/css/global.css.map',
},
files: {}
};
less['dev_main']['files'][path_dev+'/css/global.css'] = path_dev'/less/main.less';
tasks_to_watch.push('less:dev_main');
//less:(dynamically find .less files)
grunt.file.recurse(path_dev+'/less/apps/', function(abspath, rootdir, subdir, filename) {
if(filename.match(/\.less$/g)){
var name = filename.substring(0, filename.lastIndexOf('.'));
var thisurl = url_dev+'/css/apps/app/'+name+'.css';
var thispath = path_dev+'/css/apps/app/'+name+'.css';
less[name] = {
options: {
sourceMap: true,
sourceMapURL: thisurl+'.map',
},
files: {}
};
less[name]['files'][thispath] = abspath;
tasks_to_watch.push('less:'+name);
}
}); //grunt.file.recurse()
watch = {
styles: {
files: [path_dev+'/less/**/*.less'],
tasks: tasks_to_watch,
options: {
nospawn: true
}
}
};
//now repurpose the array to contain all tasks, so we can register them all below
tasks_to_watch.push('less:build');
tasks_to_watch.push('watch');
//load tasks
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-less');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.config('less', less);
grunt.config('watch', watch);
//register tasks
grunt.registerTask('default', tasks_to_watch);
grunt.registerTask('build', ['clean:build', 'less:build', 'concat:build', 'uglify:build', 'copy:build']);
};
你可以發佈gruntfile嗎? –
試試'grunt --debug watch' –
感謝您的幫助。我在上面添加了Gruntfile.js。運行'grunt --debug watch'什麼也不做,不幸的是... – bobsoap