未在浏览器中执行Webpack 4 splitChunks

时间:2019-03-07 08:37:45

标签: javascript webpack

我具有以下Webpack 4配置:

const webpack = require('webpack');
const path = require('path')
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;

module.exports = {
    mode: "development",
    entry: {
        main: './js/main.js',
        login: './js/apps/login/login_app.js',
    },
    output: {
        path: __dirname + '/dist',
        filename: '[name].[hash].js'
    },
    optimization: {
        splitChunks: {
            chunks: 'all',
            cacheGroups: {
                vendors: {
                    name: 'vendors',
                    chunks: 'all',
                    enforce: true,
                    priority: 1,
                    test(module, chunks) {
                        const name = module.nameForCondition && module.nameForCondition();
                        return chunks.some(chunk => chunk.name === 'main' && /node_modules/.test(name));
                    }
                },
                main: {
                    name: 'main',
                    chunks: 'all',
                    enforce: true,
                    priority: 0,
                    test(module, chunks) {
                        return chunks.some(chunk => chunk.name === 'main');
                    }
                },
                login: {
                    name: 'login',
                    chunks: 'all',
                    enforce: true,
                    priority: 0,
                    test(module, chunks) {
                        return chunks.some(chunk => chunk.name === 'login');
                    }
                },
            }
        }
    },
    resolve: {
        extensions: ['*', '.js', '.jsx'],
        modules: ['js', 'node_modules'],
        alias: {
            "backbone": path.join(__dirname, 'node_modules', 'backbone', 'backbone'),
            "spin.jquery": 'lib/spin.jquery',
            "datetimepicker": 'lib/datetimepicker',
            "common-behavior": 'common/common_behavior',
            "utilities": 'common/utilities',
            "storage": 'models/storage',
            "local_storage": 'local_storage',
            "session_storage": 'session_storage',
            "cookie_storage": 'cookie_storage',
            "head_app": 'apps/head/head_app',
            "pages": 'pages',
            "header_app": 'apps/header/header_app',
            "footer_app": 'apps/footer/footer_app',
            "views": 'common/views',
            "search_activity_app": 'apps/search/search_activity_app',
            "calendar": 'components/calendar/calendar',
            "calendar-render": 'components/calendar/calendar.render',
            "calendar-costants": 'components/calendar/calendar.costants',
            "components-utils": 'components/utils',
            "dropdown": "components/dropdown/dropdown",
            "dropdown-render": "components/dropdown/dropdown.render",
            "dropdown-constants": "components/dropdown/dropdown.constants",
            "handlebars": 'handlebars/dist/handlebars.min.js',
            "bootstrap-datetimepicker": 'lib/bootstrap-datetimepicker.min',
            "bootstrap-select": "lib/bootstrap-select",
            "jasny-bootstrap": "jasny-bootstrap/dist/js/jasny-bootstrap.min.js",
            "card": 'lib/jquery.card',
        }
    },
    module: {
        rules: [
            { test: /pages/, loader: 'imports-loader?head_app' },
            { test: /views/, loader: 'imports-loader?head_app' },
            { test: /footer_app/, loader: 'imports-loader?head_app' },
            { test: /header_app/, loader: 'imports-loader?head_app' },
            { test: /spin.jquery/, loader: 'imports-loader?jquery,spin' },
            { test: /bootstrap-datetimepicker/, loader: 'imports-loader?bootstrap,moment' },
            { test: /bootstrap-select/, loader: 'imports-loader?bootstrap' },
            { test: /jasny-bootstrap/, loader: 'imports-loader?jquery,bootstrap' },
            {
                test: /\.(js|jsx)$/,
                exclude: /node_modules/,
                use: ['babel-loader']
              }
        ]
    },
    plugins: [
        //new BundleAnalyzerPlugin()
        new webpack.ProvidePlugin({
            $: "jquery",
            jQuery: "jquery"
           })
      ]
}

将生成以下文件:

main.ca06beb99f4c5bcd9ee2.js
vendors.ca06beb99f4c5bcd9ee2.js
login.ca06beb99f4c5bcd9ee2.js

但是如果我将js导入到我的页面中,则什么也不会执行,在我的项目中,我没有登录页面,也有其他页面,并且使用路由器,我希望仅动态加载特定的构建,例如:我访问了/home页,我想加载home.js等。

所以我的问题是:

  1. 为什么要导入主要供应商和供应商,浏览器中什么也没发生?
  2. 使用此webpack代码进行拆分后,能否在路由匹配时仅动态加载页面的构建?
  3. 所有页面代码都使用node_modules库,例如login_app使用某些node_modules。这些节点模块添加在哪里?在供应商?在login.js中还是在另一个vendor-login.js中?什么是正确的方法以及如何实现?

谢谢

0 个答案:

没有答案