Grunt用户转换路径到资产

Grunt usemin transform path to assets

本文关键字:路径 用户 转换 Grunt      更新时间:2023-09-26

这是我的输入文件(使用yeoman-angular)

  <!-- build:js(./) scripts/vendor.js -->
  <!-- bower:js -->
  <script src="bower_components/angular/angular.js"></script>
  ...
  <script src="bower_components/pagedown-semantic-ui/pagedown.js"></script>
  <!-- endbower -->
  <!-- endbuild -->

和usemin将所有文件编译成一个单独的文件vendor.xxxx.js,并将html替换为:

<script src="scripts/vendor.f5d9f6b9.js"></script>

但是这里的路径是相对的,我想要一个绝对路径。此外,angular应用程序位于我的节点服务器的子目录中(在dashboard/中),所以我使用nignx并重定向必要的内容。我想:

<script src="/dashboard/scripts/vendor.f5d9f6b9.js"></script>

您的问题与usemin有关,请参阅此处的文档:https://github.com/yeoman/grunt-usemin

只需将index.html中的注释替换为:

<!-- build:js(./) /dashboard/scripts/vendor.js -->

这意味着:

<!-- build:PATH_WHERE_TO_FIND_FILES DESTINATION_PATH -->

将ngmin替换为ngAnnotate:

修改你的Gruntfile:

替换:

// ngmin tries to make the code safe for minification automatically by
// using the Angular long form for dependency injection. It doesn't work on
// things like resolve or inject so those have to be done manually.
ngmin: {
  dist: {
    files: [{
      expand: true,
      cwd: '.tmp/concat/scripts',
      src: '*.js',
      dest: '.tmp/concat/scripts'
    }]
  }
},

ngAnnotate: {
    options: {
        singleQuotes: true
    },
    dist: {
        files: [{
            expand: true,
            cwd: '.tmp/concat/scripts',
            src: '*.js',
            dest: '.tmp/concat/scripts'
        }]
    }
},

和替换:

grunt.registerTask('build', [
    'clean:dist',
    'wiredep',
    'useminPrepare',
    'concurrent:dist',
    'autoprefixer',
    'concat',
    'ngmin',
    'copy:dist',
    'cdnify',
    'cssmin',
    'uglify',
    'filerev',
    'usemin',
    'htmlmin'
]);
由:

grunt.registerTask('build', [
    'clean:dist',
    'wiredep',
    'useminPrepare',
    'concurrent:dist',
    'autoprefixer',
    'concat',
    'ngAnnotate',
    'copy:dist',
    'cdnify',
    'cssmin',
    'uglify',
    'filerev',
    'usemin',
    'htmlmin'
]);

运行以下两个命令修改package.json:1. NPM uninstall grunt-ngmin——save2. NPM install grunt-ng-annotate——save