这次给大家带来如何利用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来进行解析.从而实现htmlcss 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!

C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.


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

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 Linux new version
SublimeText3 Linux latest version

Atom editor mac version download
The most popular open source editor

SublimeText3 English version
Recommended: Win version, supports code prompts!