Master Multi‑Page Webpack 4 Configuration: From Zero to Production
This tutorial walks through setting up a zero‑configuration Webpack 4 project for multi‑page, multi‑platform applications, covering entry and output settings, dynamic entry generation, loader and plugin configurations, development server setup, and advanced optimization techniques such as code splitting, on‑demand loading, and asset handling.
Webpack 4 Multi‑Page Project Configuration
Webpack treats everything as a module, building a dependency graph and bundling modules into one or more bundles. Webpack 4 introduces zero‑configuration mode with a new
modeoption (development or production) that provides sensible defaults.
Project Structure
<code>- src
- common // shared code
- pages
- [pageName]
- index.js
- index.html
</code>1. Multi‑Page Entry Configuration
Entry points can be defined as an object where each key is the entry name. For many pages, a
getEntry()function scans the
src/pagesfolder and builds the entry object dynamically.
<code>const config = {
entry: {
pageOne: './src/pageOne/index.js',
pageTwo: './src/pageTwo/index.js',
pageThree: './src/pageThree/index.js'
},
mode: 'development'
};
function getEntry() {
const entry = {};
const files = glob.sync('./src/pages/*/*/index.js');
files.forEach(filePath => {
const name = filePath.match(/\/pages\/(.+)\/index.js/)[1];
entry[name] = filePath;
});
return entry;
}
module.exports = {
mode: 'development',
entry: getEntry(),
};
</code>2. Output Configuration
The output configuration defines
publicPath,
filename,
chunkFilename, and
path. In development,
publicPathcan be omitted; in production it should point to the CDN.
<code>output: {
publicPath: CDN.js,
filename: '[name].[chunkhash].js',
chunkFilename: '[name]_[chunkhash].min.js',
path: distDir
}
</code>Dynamic publicPath
Set
__webpack_public_path__at the top of the entry file to adjust the public path per platform (PC or H5).
Hash Types
hash– project‑wide,
chunkhash– per chunk,
contenthash– based on file content.
chunkhashis commonly used.
3. Loader Configuration
JavaScript
Use
babel-loaderto transpile modern JavaScript. Remember Babel only transforms syntax, not new APIs; add polyfills via
transform-runtimeand
runtimeplugins.
<code>{
test: /\.js$/,
loader: 'babel-loader',
include: [path.resolve(rootDir, 'src')]
}
</code>CSS/SCSS
Chain
style-loader,
css-loader,
postcss-loader, and
sass-loader(right‑to‑left order). Set
importLoaders: 2for
css-loader.
<code>{
test: /\.scss|\.css/,
use: [
'style-loader',
{
loader: 'css-loader',
options: { importLoaders: 2 }
},
'postcss-loader',
'sass-loader'
]
}
</code>Assets (Images, Fonts, PDFs)
Use
file-loaderto copy files and generate URLs.
<code>{
test: /\.(gif|png|jpe?g|eot|woff|ttf|pdf)$/,
loader: 'file-loader'
}
</code>AMD Modules (e.g., Zepto)
Combine
exports-loaderand
script-loaderto import AMD libraries.
<code>{
test: require.resolve('zepto'),
use: ['exports-loader?window.Zepto', 'script-loader']
}
</code>4. Plugin Configuration
html-webpack-plugin
Automatically inject bundled scripts into HTML files. For multiple pages, create an array of plugin instances using the dynamic entry list.
<code>const htmlPluginArray = [];
// inside getEntry loop:
htmlPluginArray.push(new HtmlWebpackPlugin({
filename: `./${name}/index.html`,
template: `./src/pages/${name}/index.html`
}));
</code>mini-css-extract-plugin
Extract CSS into separate files with
contenthashto avoid cache invalidation when JS changes.
<code>{
loader: MiniCssExtractPlugin.loader,
options: { publicPath: CDN.css }
},
'css-loader',
'postcss-loader',
'sass-loader'
</code>5. Other Configurations
resolve
Define aliases for frequently used paths, e.g.,
h5and
pcdirectories.
<code>resolve: {
alias: {
h5: path.resolve(__dirname, 'src/common/h5/'),
pc: path.resolve(__dirname, 'src/common/pc/')
}
}
</code>webpack-dev-server
Configure
devServerwith
publicPath,
port, and
hotfor HMR. Remember to add
HotModuleReplacementPlugin.
<code>devServer: {
publicPath: '/act/',
port: 8888,
hot: true
}
</code>Environment‑Specific Configs
Use
webpack-mergeto combine a common configuration with development or production specific files.
6. Optimization
On‑Demand Loading
Use the dynamic
import()syntax (preferred) or legacy
require.ensureto split code into separate chunks.
Extracting Common Modules
Configure
optimization.splitChunksto create
vendorand
commonbundles based on usage frequency.
<code>splitChunks: {
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
name: 'vendor',
chunks: 'initial',
priority: 2,
minChunks: 2
},
common: {
test: /.js$/,
name: 'common',
chunks: 'initial',
priority: 1,
minChunks: 2
}
}
}
</code>Multi‑Platform Specific Splitting
Define separate cache groups for H5 and PC dependencies (e.g., Zepto vs. jQuery).
Loader Performance
Use
excludeand
includeto limit the files processed by loaders, reducing build time.
Restrict Module Resolution Paths
Set
resolve.modulesto limit where Webpack searches for modules, improving resolution speed.
Conclusion
This article demonstrates a complete Webpack 4 setup for a multi‑platform, multi‑page project, covering zero‑configuration mode, dynamic entry generation, loader and plugin usage, development server configuration, and advanced optimizations that are applicable to both simple and complex frontend projects.
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.