How to build multi-page applications using webpack
这次给大家带来如何利用webpack构建多页面应用,利用webpack构建多页面应用的注意事项有哪些,下面就是实战案例,一起来看一下。
前言
之前使用 vue2.x + webpack3.x 撸了一个 vue 单页脚手架
vue 版 spa 脚手架
有兴趣的同学可以看下,内附详细注释,适合刚学习 webpack 的童鞋.
react 版 spa 脚手架
但在一些场景下,单页应用显然无法满足我们的需求,于是便有了
mulXc-cli
好了,废话不多说,进入正题!!!!
文件结构
├── build 构建服务和webpack配置 ├──── build.js 构建全量压缩包 (打包项目) ├──── setting.js 多页面入口配置 ├──── style.js 页面1对1抽取生成css文件 ├──── utils.js 工具类 ├──── webpack.base.conf.js webpack通用配置 ├──── webpack.dev.conf.js webpack开发环境配置 ├──── webpack.prod.conf.js webpack生产环境配置 ├── config webpack开发/生产环境部分配置 ├── dist 项目打包目录 ├── package.json 项目配置文件 ├── src 项目目录 ├──── common 多页面公用方法与css ├──── about about页面 ├──── home home页面
思路
多页面应用,顾名思义:就是有多个页面(废话!!!)
从 webpack 的角度来看:
1.多个入口(entry),每个页面对应一个入口,理解为 js 资源.
2.多个 html 实例,webpack 使用html-webpack-plugin 插件来生成 html 页面.
3.每个页面需要对应的 css 文件.webpack 使用 extract-text-webpack-plugin 抽取 css.
这样我们一个多页面应用该有的东西都具备了,go,开撸!!!
入口配置与 html 页面生成
通过以上文件结构,我们可以找到我们在 build/setting.js 进行了多页面入口配置与 html 页面生成。
setting.js
//node 文件操作模块 const fs = require('fs'); //node 路径模块 const path = require('path'); //使用node.js 的文件操作模块来获取src文件夹下的文件夹名称 ->[about,common,home] const entryFiles = fs.readdirSync(path.resolve(dirname, '../src')); //生成html文件插件 const HtmlWebpackPlugin = require('html-webpack-plugin'); //工具类提取_resolve方法 const { _resolve } = require('./utils'); //入口文件过滤common文件夹(因为common文件夹我们用来存放多页面之间公用的方法与css,所以不放入入口进行构建!) const rFiles = entryFiles.filter(v => v != 'common'); module.exports = { //构建webpack入口 entryList: () => { const entryList = {}; rFiles.map(v => { entryList[v] = _resolve(`../src/${v}/index.js`); }); return entryList; }, //src文件夹下的文件夹名称 ->[about,common,home] entryFiles: entryFiles, //使用html-webpack-plugin生成多个html页面.=>[home.html,about.html] pageList: () => { const pageList = []; rFiles.map(v => { pageList.push( new HtmlWebpackPlugin({ template: _resolve(`../src/${v}/index.html`), filename: _resolve(`../dist/${v}.html`), chunks: ['common', v], //压缩配置 minify: { //删除Html注释 removeComments: true, //去除空格 collapseWhitespace: true, //去除属性引号 removeAttributeQuotes: true }, chunksSortMode: 'dependency' }) ); }); return pageList; } };
webpack.base.conf.js
//引入setting.js 入口配置方法,与html生成配置 const { entryList, pageList } = require('./setting.js'); const baseConf = { entry: entryList(), plugins: [...pageList()] };
css 文件生成
通过以上文件结构,我们可以找到我们在 build/style.js 进行了 css 文件生成。
style.js
const path = require('path'); //抽取css文件插件 const ExtractTextPlugin = require('extract-text-webpack-plugin'); //引入入口配置 const { entryList, entryFiles } = require('./setting.js'); //多个ExtractTextPlugin实例 const plugins = []; entryFiles.map(v => { plugins.push( new ExtractTextPlugin({ filename: `css/${v}.[contenthash].css`, allChunks: false }) ); }); module.exports = { //使用正则匹配到每个页面对应style文件夹下的css/less文件,并配置loader来进行解析.从而实现html<->css 1对1 rulesList: () => { const rules = []; entryFiles.map((v, k) => { rules.push({ test: new RegExp(`src(\\\\|\/)${v}(\\\\|\/)style(\\\\|\/).*\.(css|less)$`), use: plugins[k].extract({ fallback: 'style-loader', use: ['css-loader', 'postcss-loader', 'less-loader'] }) }); }); return rules; }, //插件实例 stylePlugins: plugins };
webpack.prod.conf.js
//引入方法 const { rulesList, stylePlugins } = require('./style.js'); const prodConf = { module: { rules: [...rulesList()] }, plugins: [...stylePlugins] };
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
The above is the detailed content of How to build multi-page applications using webpack. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Want to copy a page in Microsoft Word and keep the formatting intact? This is a smart idea because duplicating pages in Word can be a useful time-saving technique when you want to create multiple copies of a specific document layout or format. This guide will walk you through the step-by-step process of copying pages in Word, whether you are creating a template or copying a specific page in a document. These simple instructions are designed to help you easily recreate your page without having to start from scratch. Why copy pages in Microsoft Word? There are several reasons why copying pages in Word is very beneficial: When you have a document with a specific layout or format that you want to copy. Unlike recreating the entire page from scratch

Standby is a new feature in the iOS 17 update that provides a new and enhanced way to access information when your phone is idle quickly. With StandBy, you can conveniently check the time, view upcoming events, browse your calendar, get weather updates for your location, and more. Once activated, the iPhone will intuitively enter standby mode when set to landscape while charging. This feature is perfect for wireless charging points like your bedside table, or when you're away from your iPhone charging during daily tasks. It allows you to swipe through various widgets displayed in standby to access different sets of information from various applications. However, you may want to modify these widgets or even delete some based on your preferences and the information you need frequently. So let's dive into

Page refresh is very common in our daily network use. When we visit a web page, we sometimes encounter some problems, such as the web page not loading or displaying abnormally, etc. At this time, we usually choose to refresh the page to solve the problem, so how to refresh the page quickly? Let’s discuss the shortcut keys for page refresh. The page refresh shortcut key is a method to quickly refresh the current web page through keyboard operations. In different operating systems and browsers, the shortcut keys for page refresh may be different. Below we use the common W

In iOS, Apple allows you to disable individual home screen pages on your iPhone. It's also possible to rearrange the order of home screen pages and delete pages directly instead of just disabling them. Here's how it works. How to Rearrange Home Screen Pages Touch and hold Space on the Home screen to enter jitter mode. Tap the row of dots that represent Home screen pages. In the Home screen grid that appears, touch and drag a page to rearrange it relative to other pages. Others move in response to your dragging. When you're happy with your new arrangement, tap "Done" in the upper right corner of the screen, then tap "Done" again to exit dither mode. How to Disable or Remove Home Screen Pages Touch and hold Space on the Home screen to enter dither mode. Tap to represent home screen

Title: Implementation method of page jump in 3 seconds: PHP Programming Guide In web development, page jump is a common operation. Generally, we use meta tags in HTML or JavaScript methods to jump to pages. However, in some specific cases, we need to perform page jumps on the server side. This article will introduce how to use PHP programming to implement a function that automatically jumps to a specified page within 3 seconds, and will also give specific code examples. The basic principle of page jump using PHP. PHP is a kind of

Form validation is a very important link in web application development. It can check the validity of the data before submitting the form data to avoid security vulnerabilities and data errors in the application. Form validation for web applications can be easily implemented using Golang. This article will introduce how to use Golang to implement form validation for web applications. 1. Basic elements of form validation Before introducing how to implement form validation, we need to know what the basic elements of form validation are. Form elements: form elements are

"Methods to handle Laravel pages that cannot display CSS correctly, need specific code examples" When using the Laravel framework to develop web applications, sometimes you will encounter the problem that the page cannot display CSS styles correctly, which may cause the page to render abnormal styles. Affect user experience. This article will introduce some methods to deal with the failure of Laravel pages to display CSS correctly, and provide specific code examples to help developers solve this common problem. 1. Check the file path. First check the path of the CSS file.

Web standards are a set of specifications and guidelines developed by W3C and other related organizations. It includes standardization of HTML, CSS, JavaScript, DOM, Web accessibility and performance optimization. By following these standards, the compatibility of pages can be improved. , accessibility, maintainability and performance. The goal of web standards is to enable web content to be displayed and interacted consistently on different platforms, browsers and devices, providing better user experience and development efficiency.
