Search code examples
javascriptwebpackminify

Can I minify file separately with webpack?


I have a project with a lot of JavaScript files separate in several folders.

root
 | - index.html
 | - lib
 | -- jquery-ui.js
 | -- jquery.js
 | -- html2canvas.js
 |  
 | - js
 | -- main.js
 | -- app1.js
 | -- app2.js
 | -- app3.js

And O need to use webpack to minify these files separately in such a way that each file keep its path.

I don't want to have one single file with all code.

My current configuration:

const path = require('path');

module.exports = {
    entry:[
        './js/index.js',
        './pages/MCF/js/index.js',
        './pages/MCF/js/refresh.js',
        './pages/MCF/lang/index.js',
        './pages/MCF/lib/scriptJs/script.js',
        './pages/MCF/lib/scriptJs/load.js'
        ],
    output :{
        path :path.resolve('./prod/js'),
        filename: "app.min.js"
    },
    module:{
        rules:[
        {
            test:/\.js$/,
            exclude: /(node_modules|bower_components)/,
            use:['babel-loader']
        }
        ]
    }
}

Solution

  • You can take a reference from here for multiple-entry-points.

    This is the way you should go for separate file builds:

    module.exports = {
      entry:{
        main      : './js/index.js',
        main2     : './pages/MCF/js/index.js',
        refresh   : './pages/MCF/js/refresh.js',
        langindex : './pages/MCF/lang/index.js',
        script    : './pages/MCF/lib/scriptJs/script.js',
        load      : './pages/MCF/lib/scriptJs/load.js'
        },
        output :{
          path :path.resolve('./prod/js'),
          filename: "[name].min.js"
        },
        ...
    };
    

    [name].min.js

    This [name] would let you save a build for each entry as the name(key) given. The output will be:

    ./prod/js/main.js, 
    ./prod/js/main2.js
    // so on