我正在尝试使用Webpack 动态语句延迟加载我的 Vue.js 路由,如下所示:vue-router
import
const routes = [
{
component: () => import("./components/home.vue"),
name: "home",
path: "/",
},
{
component: () => import("./components/child.vue"),
name: "child",
path: "/child",
}
};
这正确地导致 Webpack 按路由拆分我的 JavaScript 包,我最终得到了main.js
一系列文件。我还注意到这些是 Chrome 开发工具中要求的。0.js
1.js
但是,我似乎无法让它与热模块更换 (HMR) 一起使用。我在服务器端使用 ASP.NET Core。当我的页面加载时,我只看到一个空页面。在将 Vuex 与 HMR 一起使用时,我必须遵循此文档才能重新加载 Vuex 商店。对于延迟加载的路线,我需要做类似的事情吗?如果有帮助,这是我的 webpack.config.js 文件:
import * as BrotliPlugin from "brotli-webpack-plugin";
import * as CompressionPlugin from "compression-webpack-plugin";
import * as ExtractTextPlugin from "extract-text-webpack-plugin";
import * as ForkTsCheckerWebpackPlugin from "fork-ts-checker-webpack-plugin";
import * as path from "path";
import * as StyleLintPlugin from "stylelint-webpack-plugin";
import * as SpriteLoaderPlugin from "svg-sprite-loader/plugin";
import * as webpack from "webpack";
import { BundleAnalyzerPlugin } from "webpack-bundle-analyzer";
const configurationBuilder = (env) => {
const isDevBuild = !(env && env.prod);
const cssDirectoryName = "css";
const jsDirectoryName = "js";
const wwwRootDirectory = "./wwwroot";
const jsDirectory = `./wwwroot/${jsDirectoryName}`;
const configuration = {
stats: { modules: false },
context: __dirname,
resolve: {
extensions: [".js", ".ts", ".vue"],
alias: {
vue$: "vue/dist/vue.common.js",
},
},
entry: { main: "./ClientApp/Boot.ts" },
module: {
rules: [
{
test: /\.vue$/,
include: /ClientApp/,
loader: "vue-loader",
options: {
preserveWhitespace: isDevBuild, // Ignore white space in HTML
},
},
{
test: /\.ts$/,
include: /ClientApp/,
loader: "ts-loader",
options: {
appendTsSuffixTo: [/\.vue$/],
silent: true,
transpileOnly: true,
},
},
{
test: /\.scss$/,
use: isDevBuild ? getSassLoaders() : ExtractTextPlugin.extract({ use: getSassLoaders() }),
},
{
test: /\.svg$/,
oneOf: [
{
resourceQuery: /inline/,
use: getSvgUrlLoaders(),
},
{
use: getSvgSpriteLoaders(),
},
],
},
{
test: /\.(png|jpg|jpeg|gif)$/,
loader: "url-loader",
options: {
limit: 25000,
},
},
],
},
output: {
path: path.join(__dirname, wwwRootDirectory),
filename: "js/[name].js",
chunkFilename: "js/[name].js",
publicPath: `${jsDirectoryName}/`, // Needs a trailing slash for hot module replacement to work
},
plugins: [
new ForkTsCheckerWebpackPlugin({
tslint: true,
vue: true,
}),
new webpack.DefinePlugin({
"process.env": {
NODE_ENV: JSON.stringify(isDevBuild ? "development" : "production"),
},
}),
new StyleLintPlugin({
configFile: ".stylelintrc.json",
files: ["ClientApp/css/**/*.scss"],
}),
new SpriteLoaderPlugin(),
new webpack.DllReferencePlugin({
context: __dirname,
manifest: require(`${jsDirectory}/vendor-manifest.json`),
}),
].concat(isDevBuild ? [
new webpack.SourceMapDevToolPlugin({
filename: "[file].map",
moduleFilenameTemplate: path.relative(jsDirectory, "[resourcePath]"),
}),
] : [
// Plugins that apply in production builds only
new webpack.optimize.UglifyJsPlugin({
comments: false,
}),
new ExtractTextPlugin(
`${cssDirectoryName}/[name].css`),
new CompressionPlugin({
minRatio: 0.9,
test: /\.(css|js|json|svg)$/,
}),
new BrotliPlugin({
minRatio: 0.9,
test: /\.(css|js|json|svg)$/,
}),
new BundleAnalyzerPlugin({
analyzerMode: "static",
openAnalyzer: false,
reportFilename: "../bin/main-webpack-bundle-report.html",
}),
]),
};
return configuration;
};
export default configurationBuilder;