javascript 如何在 yeoman/grunt 项目中自动包含脚本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21232889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to include scripts automatically in a yeoman/grunt project?
提问by Benjamin Crouzier
I have a working yeoman project. I am using grunt-usemin.
我有一个工作自耕农项目。我正在使用 grunt-usemin。
To include javascripts, I do this in index.html
:
要包含 javascripts,我在index.html
:
<!-- build:js scripts/includes.js -->
<script src="/bower_components/jquery/jquery.min.js"></script>
<script src="/bower_components/underscore/underscore-min.js"></script>
<script src="/bower_components/angular/angular.js"></script>
<script src="/bower_components/angular-route/angular-route.min.js"></script>
<script src="/bower_components/angular-resource/angular-resource.min.js"></script>
<script src="/bower_components/angular-sanitize/angular-sanitize.min.js"></script>
<script src="/bower_components/angular-ui-date/src/date.js"></script>
<script src="/bower_components/angulartics/src/angulartics.js"></script>
<script src="/bower_components/angulartics/src/angulartics-ga.js"></script>
<script src="/bower_components/jquery-ui/ui/jquery-ui.js"></script>
<script src="/assets/vendor/bootstrap.2.3.1.min.js"></script>
... a few more libs like that
<script src="/scripts/config.processed.js"></script>
<script src="/scripts/app.js"></script>
<script src="/scripts/controllers/first_controller.js"></script>
<script src="/scripts/controllers/second_controller.js"></script>
<script src="/scripts/controllers/third_controller.js"></script>
<script src="/scripts/controllers/fourth_controller.js"></script>
<script src="/scripts/controllers/fith_controller.js"></script>
<script src="/scripts/controllers/sixth_controller.js"></script>
<script src="/scripts/controllers/seventh_controller.js"></script>
... 20 more like that
<script src="/scripts/directives/first_directive.js"></script>
<script src="/scripts/directives/second_directive.js"></script>
<script src="/scripts/directives/third_directive.js"></script>
... 10 more like that
<script src="/scripts/services/first_service.js"></script>
<script src="/scripts/services/second_service.js"></script>
...
<script src="/scripts/filters/first_filter.js"></script>
<script src="/scripts/filters/second_filter.js"></script>
...
<!-- endbuild -->
This is verbose. I would like to be able to do something like this:
这是冗长的。我希望能够做这样的事情:
In index.html
:
在index.html
:
<!-- build:js scripts/includes.js -->
<!-- library includes as before -->
<script src="/bower_components/jquery/jquery.min.js"></script>
<script src="/bower_components/underscore/underscore-min.js"></script>
...
<!-- Replace application includes with this: -->
<script src="<% '/scripts/**/*.js' %>"></script>
<!-- endbuild -->
回答by Benjamin Crouzier
I used grunt-include-source
Install it:
安装它:
npm install grunt-include-source --save-dev
In Gruntfile:
在 Gruntfile 中:
Load it before initConfig:
在 initConfig 之前加载它:
module.exports = function (grunt) {
...
grunt.loadNpmTasks('grunt-include-source');
Configure includeSource itself in initConfig:
在 initConfig 中配置 includeSource 本身:
grunt.initConfig({
...,
includeSource: {
options: {
basePath: 'app',
baseUrl: '/',
},
server: {
files: {
'.tmp/index.html': '<%= yeoman.app %>/index.html'
}
},
dist: {
files: {
'<%= yeoman.dist %>/index.html': '<%= yeoman.app %>/index.html'
}
}
}
Add this task where you want (here, I add it to the build task):
在你想要的地方添加这个任务(在这里,我将它添加到构建任务中):
grunt.registerTask('build', [
'clean:dist',
'includeSource:dist',
'useminPrepare',
...
Add it to the watch task:
将其添加到监视任务中:
watch: {
...,
includeSource: {
files: ['<%= yeoman.app %>/index.html'],
tasks: ['includeSource:server']
}
Change useminPrepare (if you use yeoman)
更改 useminPrepare(如果您使用 yeoman)
useminPrepare: {
// changed from app to dist, to take index.html processed by includeSource in dist
html: '<%= yeoman.dist %>/index.html',
options: {
dest: '<%= yeoman.dist %>'
}
},
I've used to subtasks: dist and server to generate the index.html in different directories.
我曾经使用子任务:dist 和 server 在不同的目录中生成 index.html。
Edit: How to include your javascripts in index.html:
编辑:如何在 index.html 中包含您的 javascripts:
<!-- build:js({.tmp,dist,app}) /scripts/application.js -->
<!-- vendor, external -->
<script src="/bower_components/jquery/jquery.min.js"></script>
<script src="/bower_components/underscore/underscore-min.js"></script>
<script src="/assets/vendor/underscore.extentions.js"></script>
<script src="/bower_components/angular/angular.js"></script>
<script src="/bower_components/select2/select2.js"></script>
<script src="/bower_components/angular-ui-select2/src/select2.js"></script>
<!-- entry point -->
<script src="/scripts/config.processed.js"></script>
<script src="/scripts/app.js"></script>
<!--include everything in scripts/ except files directly inside scripts (without subdirectory) -->
<!-- include: "type": "js", "files": ["scripts/**/*.js", "!scripts/*.js"] -->
<!-- endbuild -->
If you want to include everything scripts, do this:
如果要包含所有脚本,请执行以下操作:
<!-- include: "type": "js", "files": "scripts/**/*.js" -->