【问题标题】:Grunt usemin and absolute (nonexistent) file pathsGrunt usemin 和绝对(不存在)文件路径
【发布时间】:2015-04-30 15:29:09
【问题描述】:

我的目录结构是这样的,我有一个绝对路径/static 映射到我的public dir

+public/
  +--app/
  +--dist/
  |--Gruntfile.js
  |..
  |..

目前,一切正常,我在dist 中得到了我的缩小/修订文件,但index.html 文件包含相对路径,例如:

<script src="some-file.56a75bad.js"></script>

当我需要它们时:

<script src="/static/dist/some-file.56a75bad.js"></script>

似乎无法弄清楚如何实现这一点,我尝试过的所有内容最终都给出了正确的文件路径,而不是 rev'd 文件,即:

<script src="/static/dist/some-file.js"></script>

【问题讨论】:

    标签: angularjs gruntjs grunt-usemin


    【解决方案1】:

    我的解决方案: copy 任务将所有脚本移动到 .tmp 文件夹。 uglify 任务然后运行并输出到.tmp 中的文件夹层次结构,该绝对路径要解析到。运行这 2 个任务后,我的文件夹结构如下所示(构建中):

    public/
      +--.tmp/
        +--static/
          +--dist/
            |--application.min.js
            |--dependencies.min.js
      +--app/
      +--bower_components/
      +--dist/
      |--Gruntfile.js
      |--index.html
    

    我的一点点index.html

    <!-- build:js /static/dist/dependencies.min.js -->
    <script src="/static/dist/dependencies.min.js"></script>
    <!-- endbuild -->
    
    <!-- build:js /static/dist/application.min.js -->
    <script src="/static/dist/application.min.js"></script>
    <!-- endbuild -->
    

    现在一切正常,useminPrepare filerevusemin 任务正在运行。

    还有我的 GruntFile:

    module.exports = function(grunt) {
    
        require('load-grunt-tasks')(grunt);
    
        grunt.config.init({
    
            useminPrepare: {
                html: 'dist/index.html',
                options: {
                    dest: './dist'
                }
            },
    
            usemin:{
                html:['dist/index.html'],
                options: {
                    assetsDirs: ['.tmp']
                }
            },
    
            copy:{
                html: {
                    src: './index.html',
                    dest: 'dist/index.html'
                },
                javascripts: {
                    cwd: '',
                    src: 'app/scripts/**',
                    dest: '.tmp',
                    expand: true
                }
            },
    
            uglify: {
                build: {
                    options: {
                        mangle: true
                    },
                    files: {
                        '.tmp/static/dist/application.min.js': ['.tmp/app/**/*.js'],
                        '.tmp/static/dist/dependencies.min.js': [
                            'bower_components/jquery/dist/jquery.js',
                            'bower_components/angular/angular.js'
                            // All my other 3rd party libs, I realized this can be done in index.html but there's environmental constraints making that not possible
                        ]
                    }
                }
            },
    
            filerev: {
                dist: {
                    src: ['.tmp/static/dist/application.min.js', '.tmp/static/dist/dependencies.min.js'],
                    dest: 'dist'
                }
            }
    
        });
    
        grunt.registerTask('build',[
            'copy:javascripts',
            'copy:html',
            'uglify',
            'useminPrepare',
            'filerev',
            'usemin'
        ]);
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多