如何在yeoman/grunt项目中自动包含脚本?

26

我有一个可用的Yeoman项目。我正在使用grunt-usemin。

为了包含JavaScript,我在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 -->

这段文字很啰嗦。我希望能够像这样做:

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 -->
1个回答

41

我使用了grunt-include-source

安装它:

npm install grunt-include-source --save-dev

在 Gruntfile 文件中:

在 initConfig 之前加载它:

module.exports = function (grunt) {
  ...
  grunt.loadNpmTasks('grunt-include-source');

在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'
        }
      }
    }

将此任务添加到您想要的位置(这里,我将其添加到构建任务中):


grunt.registerTask('build', [
    'clean:dist',
    'includeSource:dist',
    'useminPrepare',
    ...
将其添加到监视任务中:
watch: {
  ...,
  includeSource: {
    files: ['<%= yeoman.app %>/index.html'],
    tasks: ['includeSource:server']
  }

修改 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 %>'
  }
},

我曾经使用了两个子任务:dist和server来在不同的目录中生成index.html。


编辑:如何将你的javascript包含在index.html中:

<!-- 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 -->
如果您想包含所有脚本,请执行以下操作:
<!-- include: "type": "js", "files": "scripts/**/*.js" -->

你的HTML索引文件是什么样子的?我按照所有步骤操作了,但是因为不知道HTML注释标签,所以无法使其正常工作。谢谢! - DanFritz
1
我正在使用带有Angular和Ionic/Cordova的Yeoman,对于这个例子(它可以工作,谢谢!),你需要将/www视为分发文件夹。例如,includeSource:dist应该指向www/index.html。 - FugueWeb
1
另外,我的Grunt文件有require('load-grunt-tasks')(grunt);,所以我不需要使用loadNpmTasks()。 - FugueWeb
或者,您可以使用 grunt serve 在相同的目录中创建文件(覆盖现有文件),并将其配置为在“build”时加载到www。 - FugueWeb
3
谢谢。我不能不说这似乎过于复杂了,但这并不是你的错。 - Casey
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接