我尝试使用bower_concat https://github.com/sapegin/grunt-bower-concat从我的bower_components编译所有css。 js编译得很好,但css永远不会被创建。以下是此部分的grunt文件代码:
bower_concat: {
all: {
dest: '<%= pkg.dist_dir %>/lib/_bower.js',
cssDest: '<%= pkg.dist_dir %>/lib/_bower.css',
dependencies: {
// 'angular': ''
},
exclude: [
'jquery'
],
bowerOptions: {
relative: false
},
includeDev: true
}
},
它永远不会创建“_bower.css”。为什么不能正常工作?
答案 0 :(得分:1)
grunt-bower-concat(以及grunt-wiredep)处理将相应包的bower.json的main
字段中提到的文件捆绑在一起的概念。
最初没有任何规范定义了应该包含在bower.json文件的main
字段中的内容。完全取决于包创建者做出这个选择。然后Define main as the entry-point files, one-per-filetype来了(这导致已知的库Bootstrap和Font Awesome从main
字段中移除CSS文件,渲染grunt-bower-concat这样的工具无需手动覆盖
mainFiles: {
package: [ 'path/to/its/file.css' ]
}
因此,您遇到的问题的可能原因与您尝试包含的bower包的main
字段不引用CSS文件有关。
答案 1 :(得分:1)
我根据页面底部的config example修复了它,而是在all参数中添加目标,创建dest参数并单独设置js / css目的地:
bower_concat: {
all: {
dest: {
'js': 'build/_bower.js',
'css': 'build/_bower.css'
}
}
}
答案 2 :(得分:1)
从1.0.0版开始,有一个新的API,cssDest已被删除:
Concatenation of any file types
The new API looks like this:
bower_concat: {
main: {
dest: {
js: 'build/_bower.js',
scss: 'build/_bower.scss',
coffee: 'build/_bower.coffee'
},
// ...
}
}
The old dest as a string ({dest: 'build/_bower.js'}) is still working but the cssDest was removed.
请参阅发行说明here。
答案 3 :(得分:0)
我的问题是我错过了css目录中的一个文件
这基本上包括我的grunt文件生成的include auto(auto_imports.less),它有一堆包含(你的app中可能有的每个.less文件) 和auto_imports.less
我还需要运行它:
bower: {
install: {
options: {
cleanTargetDir: true,
targetDir: '<%= pkg.dist_dir %>/lib'
}
}
},
在 bower_concat 之前,它可以获取所有第三方库,这就是为什么bower_concat至少对我来说不起作用的原因。我最终重写了整个Gruntfile,所以如果复制它,它应该可以正常工作。我为我未来的项目做了一个模板lol
这是完整的Gruntfile.js,希望看到它时有意义
module.exports = function (grunt) {
require('time-grunt')(grunt);
require('load-grunt-tasks')(grunt);
grunt.initConfig({
//define pkg object and point to package.json
pkg: grunt.file.readJSON('package.json'),
//define notifications
notify_hooks: {
options: {
enabled: true,
max_jshint_notifications: 5, // maximum number of notifications from jshint output
title: "<%= pkg.name %>", // defaults to the name in package.json, or will use project directory's name
success: false, // whether successful grunt executions should be notified automatically
duration: 3 // the duration of notification in seconds, for `notify-send only
}
},
notify: {
build: {
options: {
title: '<%= pkg.name %> Build',
message: 'Build Completed'
}
},
js: {
options: {
message: 'Completed JS Build'
}
},
css: {
options: {
message: 'Completed CSS Build'
}
},
bower: {
options: {
message: 'Completed Bower'
}
}
},
//define clean task
clean: {
bower: ["<%= bower.install.options.targetDir %>", "bower_components"]
},
//define bower task
bower: {
install: {
options: {
cleanTargetDir: true,
targetDir: '<%= pkg.dist_dir %>/lib'
}
}
},
bower_concat: {
all: {
dest: '<%= pkg.dist_dir %>/lib/_bower.js',
cssDest: '<%= pkg.dist_dir %>/lib/_bower.css',
bowerOptions: {
relative: false
},
dependencies: {
'angular': ['jquery', 'moment'],
'datePicker': ['moment']
},
mainFiles: {
'ng-flags': 'src/directives/ng-flags.js'
},
includeDev: true
}
},
//define concat task to concat all js files
concat: {
js: {
options: {
separator: '\n'
},
src: [
'js/app/app.js', 'js/**/*.js'
],
dest: '<%= pkg.dist_dir %>/<%= pkg.name %>.js'
}
},
uglify: {
options: {
banner: '/*! <%= pkg.name %> <%= grunt.template.today("dd-mm-yyyy") %> */\n',
mangle: false
},
js: {
files: {
'<%= pkg.dist_dir %>/<%= pkg.name %>.min.js': ['<%= concat.js.dest %>']
}
}
},
jshint: {
files: ['Gruntfile.js', 'js/**/*.js', '!js/lib/*.js'],
options: {
globals: {
jQuery: true,
console: true,
module: true
}
}
},
//define less task
less: {
all: {
options: {
paths: ["css"]
},
files: {
"<%= pkg.dist_dir %>/<%= pkg.name %>.css": "css/<%= pkg.name %>.less"
}
}
},
less_imports: {
options: {
inlineCSS: false
},
all: {
src: [ 'css/**/*.less', '!<%= less_imports.all.dest %>', '!css/<%= pkg.name %>.less'],
dest: 'css/auto_imports.less'
}
},
//define the watch task. (documented at https://github.com/gruntjs/grunt-contrib-watch)
watch: {
js: {
files: ['<%= concat.js.src %>'],
tasks: ['build_js']
},
css: {
files: ['css/**/*.less'],
tasks: ['build_css']
},
grunt_file: {
files: ['Gruntfile.js'],
tasks: ['build']
}
}
});
//bower tasks
grunt.registerTask('bower_install', [ 'clean:bower', 'bower', 'bower_concat', 'notify:bower']);
grunt.registerTask('build_css', ['less_imports', 'less', 'notify:css']);
grunt.registerTask('build_js', ['jshint', 'concat:js', 'uglify:js', 'notify:js']);
// the default task can be run just by typing "grunt" on the command line
grunt.registerTask('build', [
'bower_install', 'build_css', 'build_js'
]);
grunt.registerTask('default', ['build']);
// Start the notification task.
grunt.task.run('notify_hooks');
};