Gruntjs: grunt-usemin使用心得

grunt-usemin

Replaces references to non-optimized scripts or stylesheets into a set of HTML files

usemin exports 2 different tasks:

  • useminPrepare prepares the configuration to transform specific construction (blocks) in the scrutinized file into a single line, targeting an optimized version of the files 

  • usemin replaces the blocks by the file they reference, and replaces all references to assets by their revisioned version if it is found on the disk. This target modifies the files it is working on.

Usually, useminPrepare is launched first, then the steps of the transformation flow (for example, concatuglify, and cssmin), and then, in the end usemin is launched.

我的工作目录

workspace/

  ——app/

    ——js/

    ——tpl/

Gruntfile.js配置

 1 module.exports = function(grunt) {
 2 
 3     // Project configuration.
 4     grunt.initConfig({
 5 
 6         useminPrepare: {
 7             html: ['app/tpl/**/*.html'],
 8             options: {
 9                 // 测试发现这里指定的dest,是usemin引入资源的相对路径的开始
10                 // 在usemin中设置assetsDirs,不是指定的相对路径
11                 // List of directories where we should start to look for revved version of the assets referenced in the currently looked at file
12                 dest: 'build/tpl'               // string type                 
13             }
14         },
15         usemin: { 
16             html: ['build/tpl/**/*.html'],      // 注意此处是build/
17             options: {
18                 assetsDirs: ['build/js']
19             }
20         },
21         copy: {
22             html: {
23                 expand: true,                   // 需要该参数
24                 cwd: 'app/',
25                 src: ['tpl/**/*.html'],         // 会把tpl文件夹+文件复制过去
26                 dest: 'build'
27             }
28         } 
29 
30     });
31 
32   grunt.loadNpmTasks('grunt-contrib-uglify');
33   grunt.loadNpmTasks('grunt-contrib-copy');
34   grunt.loadNpmTasks('grunt-contrib-concat');
35   grunt.loadNpmTasks('grunt-usemin');
36 
37   // 最后就是顺序了,没错concat,uglify在这里哦!
38   grunt.registerTask('default', [
39       'copy:html',
40       'useminPrepare',
41       'concat:generated',
42       'uglify:generated',
43       'usemin'
44   ]);
45 
46 };

源html结构

<body>
    <p>this is a grunt usemin</p>

    <script src="../js/globle.js"></script>
    
    <!-- build:js ../js/page.js -->
    <script src="../js/libs.js"></script>
    <script src="../js/page.js"></script>
    <!-- endbuild -->
</body>

 

打包后的html

<body>
    <p>this is a grunt usemin</p>

    <script src="../js/globle.js"></script>
    
    <script src="../js/page.js"></script>
</body>

 

感谢gruntjs,yeoman

https://github.com/yeoman/grunt-usemin

转载于:https://www.cnblogs.com/mackxu/p/grunt-usemin.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Grunt 是一个 JavaScript 任务运行器,可以自动化执行一些重复性的开发任务,如压缩、合并、编译等等。其中,`grunt build` 是一个常用的命令,用于打包整个项目。在运行 `grunt build` 命令时,可能会遇到一些常见错误,下面是一些解决这些错误的方法。 1. Error: Cannot find module 'load-grunt-tasks' 该错误通常是由于缺少 `load-grunt-tasks` 模块导致的。可以通过在命令行中运行以下命令来安装该模块: ``` npm install --save-dev load-grunt-tasks ``` 2. Warning: Task "task-name" not found 该错误通常是由于缺少某个 Grunt 插件导致的。可以通过在命令行中运行以下命令来安装相应的插件: ``` npm install --save-dev grunt-plugin-name ``` 其中,`grunt-plugin-name` 为需要安装的插件名称。 3. Warning: Task "uglify" not found 如果遇到这个警告,可能是因为需要安装并加载 `grunt-contrib-uglify` 插件。可以通过运行以下命令来安装该插件: ``` npm install --save-dev grunt-contrib-uglify ``` 并在 `Gruntfile.js` 文件中添加以下代码: ```js grunt.loadNpmTasks('grunt-contrib-uglify'); ``` 4. Warning: Task "concat" not found 如果遇到这个警告,可能是因为需要安装并加载 `grunt-contrib-concat` 插件。可以通过运行以下命令来安装该插件: ``` npm install --save-dev grunt-contrib-concat ``` 并在 `Gruntfile.js` 文件中添加以下代码: ```js grunt.loadNpmTasks('grunt-contrib-concat'); ``` 5. Warning: Task "sass" not found 如果遇到这个警告,可能是因为需要安装并加载 `grunt-contrib-sass` 插件。可以通过运行以下命令来安装该插件: ``` npm install --save-dev grunt-contrib-sass ``` 并在 `Gruntfile.js` 文件中添加以下代码: ```js grunt.loadNpmTasks('grunt-contrib-sass'); ``` 除了上述错误,还有许多其他可能出现的错误,每个错误的解决方法都有所不同。因此,在使用 `grunt build` 命令时,需要注意查看控制台输出的错误信息,并根据错误信息来解决问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值