Scaffolding based on webpack5 | webpack5-boilerplate
English | 中文
- Scaffolding based on
webpack5
- Minimal dependent installation
- Support
ES6
syntax compilation - Compatible with version
ie10
and above - Compile
Less
- Support third-party code splitting
- Support your own module code division
- Support multi page packaging
webpack5-boilerplate
├── README.md
├── README_CN.md
├── babel.config.json
├── package.json
├── public
│ └── lMC.ico
├── src
│ ├── assets
│ │ ├── font
│ │ ├── img
│ │ ├── media
│ │ ├── plugins
│ │ └── styles
│ ├── entry
│ │ ├── index.js
│ │ └── share.js
│ └── pages
│ ├── index.html
│ └── share.html
└── webpack
├── config.js
├── webpack.common.js
├── webpack.dev.js
└── webpack.prod.js
package.json
: related dependenciespublic
: files that will not be compiled bywebpack
will be directly output to the root directory of the package after packagingsrc
: code during development, which will be compiled bywebpack
src/assets/plugins
: used to store your own 'js' (use the directory to separate your own plug-in code)
entry
: entry filepages
: thehtml
template to be compiledwebpack
: related configuration aboutwebpack
config.js
: Some configuration items forDIY
webpack.common.js
: shared configuration inwebpack
development and production configurationwebpack.dev.js
: Configuration fordevelopment
mode (local development)webpack.prod.js
:production
mode configuration (production goes live)
- Install dependencies
npm i
- Enable development mode
npm start
- Packaging production
npm run build
-
If you want to split your own code modules, just open the following code in
webpack.common.js
myplugin: { name: 'myplugin', test(module) { // `module.resource` contains the absolute path of the file on disk. // Note the usage of `path.sep` instead of / or \, for cross-platform compatibility. // console.log(module.resource, 111); // console.log(module.resource && module.resource.includes(`${path.sep}Myplugins${path.sep}`)); return( module.resource && module.resource.includes(`${path.sep}plugins${path.sep}myplugin`) ); }, minSize: 0, priority: 10, chunks: 'initial' },
-
Description:
${path.sep}plugins${path.sep}myplugin
can be interpreted as${path.sep}your plug-in storage directory${path.sep}your plug-in name
name
means to split your code and rename it to [name
]plugins
refers tosrc/assets/plugins
myplugin
means that the name of your ownjs
code to be split must match the name you choose so that it can be matched and split.
-
For example:
Now I want to split the filesrc/assets/other/otherplugin.js
, so the plug-in storage directory isother
, and the plug-in name isotherplugin
. According to${path.sep}your plug-in storage directory${path.sep}your plug-in name
, the configuration is:${path.sep}other${path. sep}otherplugin
- During the local development process,
webpack-dev-server
is enabled, and the package file directory structure stored in memory:(http://localhost:8080/) ├── js ├── img ├── font ├── media ├── favicon.ico ├── index.html └── share.html
In
dev
mode,css
is directly rendered to the inline<style></style>
, which is not separated, in order to speed up the development preview;
js
does code splitting, because inwebpack
, I personally think that the code splitting module is more complicated, so the development environment and production environment are divided, so that problems can be found in the development environment in advance. Code splitting, just moveoptimization
inwebpack.common.js
towebpack.prod.js
- The directory structure after packaging:
dist ├── css ├── js ├── img ├── font ├── media ├── favicon.ico ├── index.html └── share.html
Will separate
css
,js
,img
,media
- The
proPublicPath
field inconfig.js
can be used to access the absolute path ofhtml
static resources after packaging. For example, if a static resource deployscdn
, you can set the value to thecdn
path - The
staticFileName
field inconfig.js
indicates that the static resource directory that is not packaged bywebpack
will be copied directly to the root directory of the packaging folder after production packaging - The
proResFileName
field inconfig.js
indicates that static resources such asjs, css, media, img
are placed in theproResFileName
folder, and only thehtml
file or thedist
package root directory is left. Other files for easy deployment. For example, when the value ofproResFileName
isassets
, the packaged structure is:dist ├── assets │ ├── css │ ├── font │ ├── img │ ├── js │ └── media ├── favicon.ico ├── index.html └── share.html
- The file name in the
public
folder should not be the same as the file name undersrc
, otherwise the packaged file undersrc
will be overwritten after packaging
The test shows that the @babel/plugin-transform-modules-commonjs
plug-in will affect the webpack
tree shaking
function, so this plug-in should be installed with caution
- Every time you finish modifying the files in the
webpack
folder, remember to restart the service withnpm start
- For more analysis of this scaffolding, you can see my article some experience on Webpack5 construction
- Welcome to
Fork
to learn and exchange - Notes are written in the scaffolding, if you find it useful, welcome to
Star