Mastering Vue Multi‑Page Apps with Webpack: A Step‑by‑Step Guide
This tutorial explains why and how to build multi‑page Vue applications using vue‑cli and webpack, covering project setup, configuration files, webpack entry and output handling, HTML generation, asset management, and performance optimizations for both development and production environments.
Introduction
While a single‑page Vue app is usually preferred, multi‑page architecture can be advantageous in special scenarios such as QQ's multi‑webview design, iOS swipe‑back navigation, and avoiding page refreshes on back actions.
Goal
The article demonstrates how to create a pure‑frontend multi‑page project and ultimately a Node.js multi‑page server‑side rendering setup.
Source Code
Repository: https://github.com/kenkozheng/HTML5_research/tree/master/Vue-Multipages-Webpack3
Step 1: Set Up the Base Project with vue‑cli
Install vue‑cli and initialize a project using the
webpack‑simpletemplate.
npm i -g vue-cli vue init webpack-simple my-projectThe generated directory structure includes configuration files and a basic Vue entry.
Project Files Overview
.babelrc – Babel configuration (uses
envplugin, leaves module syntax to webpack)
.editorconfig – optional editor settings
.gitignore – Git ignore rules
index.html – basic HTML entry for a single page
webpack.config.js – distinguishes development and production, adds uglify in production
src directory – Vue single‑file components and
main.jsStep 2: Run and Analyze the Prototype
Execute
npm run devto start a development server listening on port 8080 with hot‑module replacement.
Key snippets:
.babelrcUses the
envpreset and disables module transformation for webpack.
webpack.config.jsShows entry configuration, output filename pattern
[name].[hash:8].js, and explains
pathvs
publicPath.
Key Webpack Concepts
entry can be an array, object, or string; determines which files are bundled.
output.filename supports placeholders like
[name]and
[hash:8]for dynamic naming.
path is the physical output directory; publicPath is the URL prefix for assets.
module.rules define loaders;
file-loaderhandles assets and can create directories automatically.
resolve.alias creates shortcuts such as
vue$to a specific build.
devServer configures the hot‑reload server; uses
memory-fsunless
WriteFilePluginforces disk writes.
devtool controls source‑map generation for easier debugging.
Step 3: Transform to Multi‑Page
Create multiple Vue entry files, configure multiple entries in webpack, and reuse HTML via
html-webpack-plugin.
Automated HTML Generation
Install the plugin and add an instance for each page in the webpack configuration.
npm install html-webpack-plugin --save-devConfigure the
pagesarray to generate multiple
HtmlWebpackPlugininstances automatically.
Project Optimizations
Global CSS can be imported in
main.jsand will be injected into the HTML.
Image filenames include a hash for cache busting; placing them in dedicated directories simplifies CDN configuration.
Use
HtmlWebpackInlineSourcePlugin(inlineSource field) to inline HTML, JS, and CSS, reducing HTTP requests for mobile users.
Run the Final Project
Clone the repository and start the development server with
npm run dev. The multi‑page setup will automatically generate
page1and
page2bundles.
Tencent IMWeb Frontend Team
IMWeb Frontend Community gathering frontend development enthusiasts. Follow us for refined live courses by top experts, cutting‑edge technical posts, and to sharpen your frontend skills.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.