【问题标题】:Combine and minify all bower libraries with gruntjs使用 gruntjs 合并和缩小所有 bower 库
【发布时间】:2014-10-08 22:44:30
【问题描述】:

有没有办法自动将所有 Bower 安装的库合并并缩小到一个文件中?

首先我尝试了最基本的方法:合并所有子目录中的所有.js文件:

uglify: {
    options: {compress: true},
    my_target: { files: {
        'vendor.js': ['bower_components/**/*.js'],
}   }   }

但这显然是一种不好的方法。由于错误太多,它也不起作用。

我手动删除了所有文件,只保留了每个库拥有的 1 个(主)文件,并且它有效。

有没有办法自动完成这一切?

另外,这样做是否可取? (即将所有供应商库合并到一个文件中)

【问题讨论】:

    标签: gruntjs minify bower grunt-contrib-uglify wiredep


    【解决方案1】:

    我推荐 2 个不错的 grunt 库,Wiredep 和 Usemin 的组合:

    Wiredep:自动加载在你的 html 中的 bower.json 中标识的 bower 的所有依赖项

    Usemin:检测两个 cmets 标签内的所有 src,并且所有源都在 dist 文件夹中被缩小和连接,下面是使用这个包的 grunt 文件的一个小例子,基于 angular 到 yeoman 的生成器,这只是一个简短的咕哝声

    咕噜声

        wiredep: {
            options: {
                cwd: 'appFolder'
            },
            app: {
                src: ['htmlCollections'],
                ignorePath:  /\.\.\//
            }
        },
    
        useminPrepare: {
            html: 'htmlCollections',
            options: {
                dest: 'distributionFolder',
                flow: {
                    html: {
                        steps: {
                            js: ['concat', 'uglifyjs'],
                            css: ['cssmin']
                        },
                        post: {}
                    }
                }
            }
        },
    
      usemin: {
            html: ['distributionFolder+HtmlFiles'],
            css: ['distributionFolder+cssFiles'],
            js: ['distributionFolder+javascriptFiles']
      }
    

    HTML

    <!doctype html>
    <html lang="en" ng-app="MobileDev" id="ng-app">
    <head>
        <!-- build:css(app) styles/vendor.css -->
        <!-- bower:css -->
                //This gonna be generated for the grunt by dependencies in bower
        <!-- endbower -->
        <!-- endbuild -->
    
        <!-- build:css(.tmp) styles/main.css -->
            //All the script inside this gonna be concatened and minified in 
            //the dist folder by the name of main.css
            <link type="text/css" rel="stylesheet" href="styles/app.css"/>
        <!-- endbuild -->
    </head>
    
    <body>
        <!-- build:js(app) scripts/vendor.js -->
        <!-- bower:js -->
            //This gonna be generated for the grunt by dependencies in bower
            //And in distribution all bower components added gonna be minified by usemin in
            //vendor.js
        <!-- endbower -->
        <!-- endbuild -->
    
        <!-- build:js({.tmp,app}) scripts/scripts.js -->
            //All the script inside this gonna be concatened and minified in the dist 
            //folder by the name of scripts.js
            <script type="text/javascript" src="scripts/numero1"></script>
            <script type="text/javascript" src="scripts/numero2"></script>
        <!-- endbuild -->
    
    </body>
    

    【讨论】:

      【解决方案2】:

      刚好需要wiredep

      uglify: {
          options: { compress: true },
          my_target: { 
              files: { 'public/vendor.js': require('wiredep')().js
      }   }   },
      cssmin: {
          minify: {
              files: { 'public/vendor.css': require('wiredep')().css
      }   }   },
      

      【讨论】:

        猜你喜欢
        • 2012-06-12
        • 1970-01-01
        • 2014-04-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多