1

我正在开发一个具有多个单独页面的基于 CMS 的网站,并且我正在使用通常的代码拆分策略app.js在文件之后加载的文件将站点范围的功能加载到每个页面上。是主入口文件,它使用动态导入来加载特定于页面的脚本。这是我的一个例子:vendor.jsapp.jsapp.js

/**
 * "Register for a class" page
 */
if (document.body.classList.contains("page-id-4136")) {
    import(/* webpackChunkName: "register-for-a-class" */ './pages/register-for-a-class/main')
}

在这种情况下,'pages/register-for-a-class/main.js' 文件页面使用Vue作为依赖项,但它是该站点迄今为止唯一需要它的页面。vendor.js当仅在此页面上使用Vue 时,我宁愿不将其捆绑到其中。我试图通过尝试以下方法将 Vue 本身包含为动态导入:

// pages/register-for-a-class/main.js

import(/* webpackChunkName: "vue" */ 'vue').then(({ default: Vue }) => {
  new Vue({
    ...
  })
})

但这给了我一个 webpack 捆绑错误,上面写着Module parse failed: 'import' and 'export' may only appear at the top level. 所以我也试过这个:

// app.js

/**
 * "Register for a class" page
 */
if (document.body.classList.contains("page-id-4136")) {
    import(/* webpackChunkName: "vue" */ 'vue').then(({ default: Vue }) => {
        import(/* webpackChunkName: "register-for-a-class" */ './pages/register-for-a-class/main')
    })
}

这完全没有改变任何东西。没有创建“vue.bundle.js”文件,加载页面自然会产生Vue is not defined错误。

所以这就是我真正要问的。如果我可以自动pages/register-for-a-class/main.js调用正常的静态导入(例如import Vue from 'vue')并让 webpack 生成register-for-a-class.bundle.js文件和相应的register-for-a-class-vendor.bundle.js并自动动态导入该供应商包,那将是非常棒的。这可能吗?

这是我当前的 webpack.common.js 文件(我正在使用webpack-merge):

const path = require('path')
const webpack = require('webpack')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const VueLoaderPlugin = require('vue-loader/lib/plugin')
const devMode = process.env.NODE_ENV === 'development'

module.exports = {
    entry: {
        app: './assets/src/app.js'
    },
    resolve: {
        alias: {
            '~': path.resolve(__dirname, 'assets/src')
        }
    },
    module: {
        rules: [
            {
                test: /\.vue$/,
                loader: 'vue-loader'
            },
            {
                test: /\.scss$/,
                use: [
                    {
                        loader: MiniCssExtractPlugin.loader
                    },
                    'css-loader',
                    'postcss-loader',
                    'sass-loader'
                ]
            },
            {
                test: /\.css$/,
                use: [
                    'vue-style-loader',
                    'css-loader'
                ]
            },
            {
                test: /\.(woff2?|ttf|eot|svg)$/,
                use: ['url-loader']
            }
        ]
    },
    optimization: {
        runtimeChunk: 'single',
        splitChunks: {
            cacheGroups: {
                commons: {
                    test: /[\\/]node_modules[\\/]/,
                    name: 'vendor',
                    chunks: 'all'
                }
            }
        }
    },
    externals: {
        jquery: 'jQuery'
    },
    plugins: [
        new VueLoaderPlugin()
    ]
}
4

1 回答 1

0

我将推荐以下拆分方法。希望这可以帮助 :)

const path = require('path');
const webpack = require('webpack');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const VueLoaderPlugin = require('vue-loader/lib/plugin');
const devMode = process.env.NODE_ENV === 'development';

module.exports = {
    entry: {
        vendor1: ['vendor1', 'vendor2', 'vendor 3'], // all other vendor lists, package.json dependencies 
        vendor2: ['vue'],
        app: './assets/src/app.js'
    },
    resolve: {
        alias: {
            '~': path.resolve(__dirname, 'assets/src')
        }
    },
    module: {
        rules: [
            {
                test: /\.vue$/,
                loader: 'vue-loader'
            },
            {
                test: /\.scss$/,
                use: [
                    {
                        loader: MiniCssExtractPlugin.loader
                    },
                    'css-loader',
                    'postcss-loader',
                    'sass-loader'
                ]
            },
            {
                test: /\.css$/,
                use: ['vue-style-loader', 'css-loader']
            },
            {
                test: /\.(woff2?|ttf|eot|svg)$/,
                use: ['url-loader']
            }
        ]
    },
    optimization: {
        runtimeChunk: 'single',
        splitChunks: {
            cacheGroups: {
                vendor1: {
                    name: 'vendor1',
                    test: 'vendor1',
                    enforce: true
                },
                vendor2: {
                    name: 'vendor2',
                    test: 'vendor2',
                    enforce: true
                }
            }
        }
    },
    externals: {
        jquery: 'jQuery'
    },
    plugins: [new VueLoaderPlugin()]
};
于 2018-10-24T13:11:12.703 回答