javascript - Grunt/Yeoman 用绝对路径连接 javascript 文件

标签 javascript gruntjs yeoman

当 javascript 文件 src 路径以“/”开头时,我在使用 Gruntfile.js 时遇到问题。

我关注这个用例 http://briantford.com/blog/angular-yeoman.html

默认情况下,JS文件包含为

<!-- build:js scripts/scripts.js -->
        <script src="scripts/app.js"></script>
        <script src="scripts/controllers/main.js"></script>
        <!-- endbuild -->

$ ls dist/scripts/
ab541b7e.app.js     afa864c8.scripts.js controllers         vendor
$ cat dist/scripts/afa864c8.scripts.js 
"use strict";var ...
....

App Engine 应用程序配置 require使用“/”作为前缀。

<!-- build:js scripts/scripts.js -->
        <script src="/scripts/app.js"></script>
        <script src="/scripts/controllers/main.js"></script>
        <!-- endbuild -->

Build results in the concatenated file,
$ ls dist/scripts/
9eecb7db.scripts.js ab541b7e.app.js     controllers         vendor
$ cat dist/scripts/9eecb7db.scripts.js

但是,连接后的脚本文件是空的。

完整的 Gruntfile 如下,未修改。

module.exports = function( grunt ) {
  'use strict';
  //
  // Grunt configuration:
  //
  // https://github.com/cowboy/grunt/blob/master/docs/getting_started.md
  //
  grunt.initConfig({

    // Project configuration
    // ---------------------

    // specify an alternate install location for Bower
    bower: {
      dir: 'app/components'
    },

    // Coffee to JS compilation
    coffee: {
      compile: {
        files: {
          'app/scripts/*.js': 'app/scripts/**/*.coffee',
          'test/spec/*.js': 'test/spec/**/*.coffee'
        }
      }
    },

    // compile .scss/.sass to .css using Compass
    compass: {
      dist: {
        // http://compass-style.org/help/tutorials/configuration-reference/#configuration-properties
        options: {
          css_dir: 'temp/styles',
          sass_dir: 'app/styles',
          images_dir: 'app/images',
          javascripts_dir: 'temp/scripts',
          force: true
        }
      }
    },

    // generate application cache manifest
    manifest:{
      dest: ''
    },

    // default watch configuration
    watch: {
      coffee: {
        files: 'app/scripts/**/*.coffee',
        tasks: 'coffee reload'
      },
      compass: {
        files: [
          'app/styles/**/*.{scss,sass}'
        ],
        tasks: 'compass reload'
      },
      reload: {
        files: [
          'app/*.html',
          'app/styles/**/*.css',
          'app/scripts/**/*.js',
          'app/views/**/*.html',
          'app/images/**/*'
        ],
        tasks: 'reload'
      }
    },

    // default lint configuration, change this to match your setup:
    // https://github.com/cowboy/grunt/blob/master/docs/task_lint.md#lint-built-in-task
    lint: {
      files: [
        'Gruntfile.js',
        'app/scripts/**/*.js',
        'spec/**/*.js'
      ]
    },

    // specifying JSHint options and globals
    // https://github.com/cowboy/grunt/blob/master/docs/task_lint.md#specifying-jshint-options-and-globals
    jshint: {
      options: {
        curly: true,
        eqeqeq: true,
        immed: true,
        latedef: true,
        newcap: true,
        noarg: true,
        sub: true,
        undef: true,
        boss: true,
        eqnull: true,
        browser: true
      },
      globals: {
        angular: true
      }
    },

    // Build configuration
    // -------------------

    // the staging directory used during the process
    staging: 'temp',
    // final build output
    output: 'dist',

    mkdirs: {
      staging: 'app/'
    },

    // Below, all paths are relative to the staging directory, which is a copy
    // of the app/ directory. Any .gitignore, .ignore and .buildignore file
    // that might appear in the app/ tree are used to ignore these values
    // during the copy process.

    // concat css/**/*.css files, inline @import, output a single minified css
    css: {
      'styles/main.css': ['styles/**/*.css']
    },

    // renames JS/CSS to prepend a hash of their contents for easier
    // versioning
    rev: {
      js: 'scripts/**/*.js',
      css: 'styles/**/*.css',
      img: 'images/**'
    },

    // usemin handler should point to the file containing
    // the usemin blocks to be parsed
    'usemin-handler': {
      html: 'index.html'
    },

    // update references in HTML/CSS to revved files
    usemin: {
      html: ['**/*.html'],
      css: ['**/*.css']
    },

    // HTML minification
    html: {
      files: ['**/*.html']
    },

    // Optimizes JPGs and PNGs (with jpegtran & optipng)
    img: {
      dist: '<config:rev.img>'
    },

    // rjs configuration. You don't necessarily need to specify the typical
    // `path` configuration, the rjs task will parse these values from your
    // main module, using http://requirejs.org/docs/optimization.html#mainConfigFile
    //
    // name / out / mainConfig file should be used. You can let it blank if
    // you're using usemin-handler to parse rjs config from markup (default
    // setup)
    rjs: {
      // no minification, is done by the min task
      optimize: 'none',
      baseUrl: './scripts',
      wrap: true
    }
  });

  // Alias the `test` task to run `testacular` instead
  grunt.registerTask('test', 'run the testacular test driver', function () {
    var done = this.async();
    require('child_process').exec('testacular start --single-run', function (err, stdout) {
      grunt.log.write(stdout);
      done(err);
    });
  });
};

编辑:

做一些研究导致下面的 repo,yeoman 使用它来替换标记。 https://github.com/yeoman/grunt-usemin

还需要搞清楚,前面加“/”有什么问题

最佳答案

实际上,您的用例应该得到支持。 但是,请注意:

  • 在当前 0.9.6 版本的 yeoman 上,我不确定它是否有效
  • yeoman 目前正在进行大量返工,很快就会使用 grunt-usemin。你的用例应该得到 grunt-usemin 的支持

关于javascript - Grunt/Yeoman 用绝对路径连接 javascript 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14076927/

相关文章:

javascript - 在 ajax 加载的页面上执行 JQuery 操作

javascript - 一个接一个地加载脚本

visual-studio-2013 - 如何将自定义事件绑定(bind)到 VS 2013 中的 grunt 任务运行器

gruntjs - grunt-processhtml 定义可变数量的文件

javascript - 使用 grunt 构建时,如何/将什么部署到我的 tomcat 服务器?

angularjs - 使用 grunt 时找不到模块 'karma'

javascript - meteor ccorcos/meteor-transitioner 包是否为移动设备提供滑动功能

javascript - 使用 JavaScript 从文本创建数组

linux - 为 ubuntu 14.04 grunt 命令配置 yeoman 不起作用

gruntjs - Yeoman & Grunt,如何仅包含在构建中