我可以使用webpack分别缩小文件吗?

时间:2018-07-25 08:59:10

标签: javascript webpack minify

我有一个项目,其中有很多JavaScript文件放在几个文件夹中。

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

并且O需要使用webpack来分别缩小这些文件,以使每个文件都保持其路径。

我不想包含所有代码的单个文件。

我当前的配置:

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']
        }
        ]
    }
}

1 个答案:

答案 0 :(得分:1)

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

这是您应该进行单独文件构建的方式:

ClassLoader cl = Thread.currentThread().getContextClassLoader();
        // Loop through all drivers
        Enumeration<Driver> drivers = DriverManager.getDrivers();
        while (drivers.hasMoreElements()) {
            Driver driver = drivers.nextElement();
            if (driver.getClass().getClassLoader() == cl) {
                // This driver was registered by the webapp's ClassLoader, so deregister it:
                try {
                    System.out.println("Deregistering JDBC driver " + driver);
                    DriverManager.deregisterDriver(driver);
                } catch (SQLException ex) {
                    System.out.println("Error deregistering JDBC driver " + driver);
                    ex.printStackTrace();
                }
            } else {
                // driver was not registered by the webapp's ClassLoader and may be in use
                // elsewhere
                System.out.println(
                        "Not deregistering JDBC driver {} as it does not belong to this webapp's ClassLoader" + driver);
            }
        }

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,您可以将每个条目的版本保存为给定的名称(键)。输出将是:

[name]