【问题标题】:Webpack commonchunks vue-router, only showing 1 chunkwebpack commonchunks vue-router,只显示1个chunk
【发布时间】:2017-03-14 08:31:09
【问题描述】:

我当前的 webpack 构建是 7MB,显然它重新包含了它看到的每个导入。我正在尝试使用 commonchunks 插件来确保 node_modules 库只捆绑一次。但是它只看到 1 个块。

Asset       Size       Chunks                  Chunk Names
main.js     617 kB        0  [emitted]  [big]  main
vendor.js   6.98 MB       1  [emitted]  [big]  vendor



var path = require('path')
var webpack = require('webpack')

var assetsSrcDir = path.resolve(__dirname, './resources/assets');

module.exports = {
    entry: path.resolve(assetsSrcDir, 'app.js'),
    plugins: [
        new webpack.optimize.CommonsChunkPlugin({
            name: 'vendor',
            minChunks: function(module, count) {
                return ((module.context && module.context.indexOf("node_modules") > -1) || count >= 2);
            }
        })
    ],
    output: {
        path: path.resolve(__dirname, './public'),
        publicPath: '/',
        filename: '[name].js'
    },
    module: {
        rules: [{
            test: /\.vue$/,
            loader: 'vue-loader',
            options: {
                loaders: {
                    'stylus': 'vue-style-loader!css-loader!stylus-loader',
                },
            }
        }, {
            test: /\.js$/,
            loader: 'babel-loader',
            exclude: /node_modules/
        }, {
            test: /\.(png|jpg|gif|svg)$/,
            loader: 'file-loader',
            options: {
                name: '[name].[ext]?[hash]'
            }
        }, {
            test: /\.styl$/,
            use: [
                'style-loader',
                'css-loader', {
                    loader: 'stylus-loader'
                },
            ]
        }, {
            test: /\.css$/,
            loader: 'style-loader!css-loader'
        }, {
            test: /\.(eot|svg|ttf|woff(2)?)(\?v=\d+\.\d+\.\d+)?/,
            loader: 'url-loader'
        }]
    },
    resolve: {
        alias: {
            'vue$': 'vue/dist/vue.common.js',
            http: path.resolve(assetsSrcDir, 'http.js'),
            routes: path.resolve(assetsSrcDir, 'routes.js'),
            filters: path.resolve(assetsSrcDir, 'filters.js'),
            eventBus: path.resolve(assetsSrcDir, 'eventBus.js'),
            styles: path.resolve(assetsSrcDir, 'assets/style'),
            images: path.resolve(assetsSrcDir, 'assets/images'),
            components: path.resolve(assetsSrcDir, 'components'),
            views: path.resolve(assetsSrcDir, 'views'),
            playback: path.resolve(assetsSrcDir, 'playback'),
        }
    },
    devtool: '#eval-source-map'
}

if (process.env.NODE_ENV === 'production') {
    module.exports.devtool = '#source-map'
        // http://vue-loader.vuejs.org/en/workflow/production.html
    module.exports.plugins = (module.exports.plugins || []).concat([
        new webpack.DefinePlugin({
            'process.env': {
                NODE_ENV: '"production"'
            }
        }),
        new webpack.optimize.UglifyJsPlugin({
            sourceMap: true,
            compress: {
                warnings: false
            }
        }),
        new webpack.LoaderOptionsPlugin({
            minimize: true
        })
    ])
}

如果上下文有帮助,这是我的条目:https://github.com/alexcroox/R3-Web/blob/v1-refactor-data-optimisation/resources/assets/app.js

【问题讨论】:

    标签: webpack vue.js vuejs2 vue-router


    【解决方案1】:

    你需要做的是将你的路由组件定义为异步组件:

    const Foo = resolve => {
    // require.ensure is Webpack's special syntax for a code-split point.
     require.ensure(['./Foo.vue'], () => {
        resolve(require('./Foo.vue'))
     })
    }
    

    https://github.com/vuejs/vue-router/blob/dev/docs/en/advanced/lazy-loading.md

    【讨论】:

    • 这给了我每个路由的单独 js 文件,但是,我只想要一个 main.js 和一个 vendor.js,供应商自动从所有 Vue 文件中提取 node_module imports
    猜你喜欢
    • 2019-10-20
    • 2017-07-04
    • 2018-03-15
    • 1970-01-01
    • 2017-04-21
    • 2019-08-02
    • 2020-12-24
    • 2021-08-28
    • 1970-01-01
    相关资源
    最近更新 更多