-
Npm-init initializes an environment
-
Packaging orders webpack [entry path] – o [the output path] – mode = [development | prodution]
Webpack can only handle JS JSON resources to compile ES6 modularized code into ES5(browser recognized)
-
Process other resource loaders
-
Webpack.config. js load the configuration when running the webpack command, package it according to the configuration items inside, run it based on node platform, and adopt CommonJS modularization
-
Webpack has five core concepts
- entry
- output
- loader
- plugin
- babel
-
css-loader
css-loader
将css
Code intocommonjs
Module is loadedjs
The parsed intocss
stringstyle-loader
将js
thecss
Style resource concatenation<style></style>
Tags are inserted into the page<header></header>
To take effecttest
Regular de-matchingcss
File,use
Execute the correspondingloader
(From right to left)
-
Webpack packages HTML resources
plugin
You need to download, reference, and use it, unlike loaderhtml-webpack-plugin
By default, an empty HTML file is created, importing all the resources that are packaged into the output. In order for the HTML to be structured and not empty, an HTML template is set up/ / the plugin plugins: [ new HtmlWwebpackPlugin({ template: './src/index.html'})]Copy the code
-
Package image resources
url-loader
file-loader
- Images in HTML cannot be processed by default
- Solution: Add one
html-loader
And configure{ test: /\.html$/, loader: 'html-loader'.// img is responsible for importing img options: { esModule: false // Change to commonJS import mode to prevent image generation but page cannot find image}}Copy the code
-
Package other resources
- Resources that do not need to do any processing
- Loader rules exclude resources that require a Loader
file-loader
To deal with
-
DevServer hot deployment
- Not part of the core concept
- configuration
devServer: { contentBase: path.resolve(__dirname, 'build'), // Enable gzip compression compress: true.port: 1080 } Copy the code
webpack 5.x
Start thenpx webpack server