gruntjs - Grunt usemin 任务没有正确更新嵌套的相对路径

标签 gruntjs yeoman grunt-usemin minimatch

我有一个带有“webapp-generator”脚手架的 Yeoman 项目,它包含一个带有嵌套 HTML 文件的静态网站,类似于此结构:

-root/
  index.html
    -directory/
       file1.html
    -directory2/
       file2.html
    -js/
       (revved .js files)
    -css
       (revved .css files)

我正在使用 useminfilerev更新 .html 文档上的 filerevved 文件路径的任务。它会正确更新 js/css/images 上的所有文件名,并且在根目录上可以正常工作 index.html . 但是,在嵌套的 HTML 文件中,它不会替换对正确嵌套路径的引用。

例如。

js/scripts.js重命名为 js/827385.scripts.js

index.html

<scripts src="js/scripts.js"></scripts>

解析为:<scripts src="js/827385.scripts.js"></scripts>

在目录/file1.html(或任何其他嵌套的 html 文件)

<scripts src="../js/scripts.js"></scripts>

也被转换为:<scripts src="js/827385.scripts.js"></scripts>

忽略 ../ 相对路径


有什么方法可以调整 Grunt 任务以了解文件在目录中的相对深度以保持相对指示符 ../在重命名的路径中?

下面是相关 Grunt 任务的代码片段。

PS:我已经在 Stack Overflow 问题中找到了一些可能的答案:How should I configure grunt-usemin to work with relative path无济于事。

     // Renames files for browser caching purposes
    rev: {
        dist: {
          files: {
            src: [
              '<%= config.dist %>/scripts/{,**/}*.js',
              '<%= config.dist %>/styles/{,**/}*.css',
              '<%= config.dist %>/images/{,**/}*.*',
              '<%= config.dist %>/styles/fonts/{,**/}*.*',
              '<%= config.dist %>/*.{ico,png}'
            ]
          }
        }
      },

      // Reads HTML for usemin blocks to enable smart builds that automatically
      // concat, minify and revision files. Creates configurations in memory so
      // additional tasks can operate on them
      // <%= config.dist %>
      useminPrepare: {
        options: {
          dest: 'out',
          // root: '<%= config.app %>'
        },
        html: '<%= config.app %>/index.html'
          // root: '<%= config.app %>'
      },

      // Performs rewrites based on rev and the useminPrepare configuration
      usemin: {
        options: {
          assetsDirs: [
            '<%= config.dist %>',
            '<%= config.dist %>/images',
            '<%= config.dist %>/styles'
          ]
        },
        html: ['<%= config.dist %>/**/*.html'],
        css: ['<%= config.dist %>/styles/**/*.css']
      },

最佳答案

在我的案例中,我发现问题出在 html 文件中的 usemin 配置:

如果您的 index.html 中有这个 usemin 指令:

<!-- build:js styles/jquery.js -->
<script src="bower_components/jquery/dist/jquery.js"></script>
<!-- endbuild -->

directory/file1.html 中,您应该尝试在其中使用完整路径:

<!-- build:js /scripts/jquery.js-->
<script src="../bower_components/jquery/dist/jquery.js"></script>
<!-- endbuild-->

这将被转换成类似 <script src="/scripts/jquery.34ad31c3.js"></script> 的形式.

关于gruntjs - Grunt usemin 任务没有正确更新嵌套的相对路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27868194/

相关文章:

gruntjs - Karma.conf.js 不存在

less - 使用 Compass 内联 Bootstrap SASS 图像

javascript - “Caller”, 'Callee',使用 Yeoman 部署时 Angular 应用程序出现严格模式错误

gulp - 构建 Liferay 主题失败并出现错误 "Cannot find module ' liferay-font-awesome'"

gruntjs - 如何使 grunt 不缩小某些 js 文件

用于在构建期间替换变量引用的 Javascript 预处理器

javascript - 当 grunt 构建用于产品时删除 dev/sit 代码

c# - .NET Core 控制台应用程序中的交互式、可选择和可滚动项目,如 Yeoman 控制台

gruntjs - cssmin给我 "Path must be a string. Received undefined"

javascript - 使用 grunt 和 dist 项目文件夹进行开发和部署时如何管理 Bower 依赖项?