简体   繁体   中英

Configure grunt.js to minify files one by one in bower folder

I have the dependencies of the application in bower_components , some of the dependencies don't have a minified version so I'd like to create a task creates a minified copy of the file version in the same place where the file is located like:

  • bower_components
    • lib1
      • lib1.js
      • lib1.min.js <- create this file if doesn't exist
    • lib2
      • lib2.js
      • lib2.min.js <- create this file in it's own lib2 folder
    • lib3
      • lib3.js
      • lib3.min.js <- and so on...

This is my grunt Config so far:

 uglify: {
        dev: {
            files:[
            {
                expand: true,
                src: 'bower_components/modernizr/modernizr.js',
                dest: '/',
                ext:'.min.js'
            }, {
                expand: true,
                src: 'bower_components/angular-facebook/lin/angular-facebook.js',
                dest: '/',
                ext: '.min.js'
            }]
            },
                main: {
                    src: 'temp/app.min.js',
                    dest:'dist/app.min.js'
                }
            }

the Grunt task says that copied modernizr to it's own folder but when I look at it, the file is not there and after the first file Grunt passes to the next task and ignores the 'second' file in the array.

I was just testing this obviously I'd like to implement a way that grunt scan all the dependencies in bower_components automatically.

btw, I don't mind to change the task to any other library.

the / in your dest -option means the root path (were your gruntfile resides). just delete the dest -option or put an empty string there.

important: this just works with the expand -option set!

{
  expand: true,
  src: 'bower_components/modernizr/modernizr.js',
  ext:'.min.js'
}

Edit:

for scanning all folders an minimizing all js files do it like this (note the second argument in src to not minify files which are already minified):

{
  expand: true,
  src: ['bower_components/**/*.js', '!bower_components/**/*.min.js'],
  ext:'.min.js'
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM