Combine and minify all bower libraries with gruntj

2019-04-07 11:44发布

问题:

Is there a way to combine and minify all bower installed libraries into 1 file automatically?

First I tried the most basic approach: combine all .js files from all subdirectories:

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

But this is obviously a bad approach. It also doesn't work because of too many errors.

I manually deleted all the files and kept only 1 (main) file that each library has, and it worked.

Is there a way to do this all automatically?

Also, is it advisable to do it? (i.e. combine all vendor libraries into 1 file)

回答1:

I recommend the combination of 2 nice grunt libraries, Wiredep and Usemin:

Wiredep: Load all dependencies of bower identified in bower.json inside your html automatically

Usemin: Detect all src inside two comments tags and all that source are minified and concatenated in dist folder, below are a little example of a grunt files using this packages, is based on the generator of angular to yeoman this is only a brief of that grunt

Grunt

    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:

Just needed wiredep

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