Foreword
hello, little friends herein, this warehouse out as for my GitHub repository web-study, if you think you have help, then welcome to star, your point is I praise continually updated power
webpack
Strapping Tools
webpack
There is no doubt now is very hot front-end automated packaging tools, this tutorial series is based on the latest version webpack 4.X.X
to write.
webpack 4: Zero Configuration start
- Create a directory and then enter, generate initialization
package.json
file
md webpack-4-quickstart
cd webpack-4-quickstart
npm init -y
- Installation
webpack4
andwebpack-cli
npm install webpack webpack-cli --save-dev
- Open the
package.json
Add build script:
"scripts": {
"build": "webpack"
}
- Creating entry file:
index.js
,webpack4
the default entry for the file/src/index.js
console.log('hello world!')
- Construction of packing
npm run build
You will ~/webpack-4-quickstart/dist/main.js
get the file after you pack.
Webpack 4: production and development model
Create a webpack.config.js
webpack
profile, webpack 4
describes the production production
( ) and development development
( ) modes, need to mode
attribute.
In the webpack.config.js
specified file mode
attribute development
, packaged file again. ( npm run build
).
You can see the main.js
code in the file is not compressed, ha ha, this is webpack
a powerful place, it can be divided into model development.
webpack 4: override the default inlet / outlet document
In webpack.config.js
by file entry
specified property entry file is packaged, output
the attribute specifies the export file package.
const path = require('path')
module.exports = {
mode: 'development',
entry: './src/hello.js',
output: {
path: path.resolve(__dirname, 'dist'), // 这里必须指定一个绝对路径
filename: 'hello.js'
}
}
Packing again, it will pack ./src/hello.js
output ./dist/hello.js
.
webpack 4: js code translation with Babel ES6 7
Now we are accustomed to using Javascript to write ES6.
But not all browsers know how to deal ES6. We need to do some conversions.
This step is called conversion transpiling
. transpiling
It refers to the ES6 translated source browser recognizes.
webpack itself does not know how to convert, but there is loaders
. They imagine the converter.
babel-loader
Is a loader webpack, you can put the code translated into more than ES6 ES5.
In order to use this loader we need to install a series of dependency. especially:
- babel-core
- babel-loader
- babel-preset-env (for compiling Javascript ES6 code down to ES5)
Safety first, right:
npm i babel-core babel-loader babel-preset-env --save-dev
Next we create a directory in the project .babelrc
file is used to configure Babel.
{
"presets": ["env"]
}
Once configured using a configuration file babel-loader
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader'
}
}
]
}
webpack 4: HTML plug-in
webpack
It requires two additional components to deal with HTML: html-webpack-plugin
and html-loader
.
Install:
npm i html-webpack-plugin html-loader --save-dev
webpack configuration html-loader
{
test: /\.html$/,
use: [
{
loader: 'html-loader',
options: { minimize: true } // 是否压缩代码
}
]
}
... 插件使用...
plugins: [
new HtmlWebPackPlugin({
template: './src/index.html',
filename: './index.html'
})
]
In ./src/index.html
a new HTML file (template file, used to webpack
pack)
The last run npm run build
repackaging, see ./dist directory, you will see the results after the operation.
No need to introduce your JavaScript in your HTML file: it automatically injected into it.
Open ./dist/index.html in the browser: you can see the final result.
webpack 4: to extract the CSS file
mini-css-extract-plugin
Plug-in to extract the CSS file.
Install it:
npm i mini-css-extract-plugin css-loader --save-dev
Then create a CSS file to test:
body {
line-height: 2;
}
Loader configuration and plugin:
{
test: /\.css$/,
use: [MiniCssExtractPlugin.loader, 'css-loader']
}
...插件使用...
new MiniCssExtractPlugin({
filename: '[name].css',
chunkFilename: '[id].css'
})
Finally, at the entrance of the introduction of CSS file:
import style from './main.css'
Construct:
npm run build
View the ./dist
catalog, you should be able to see the results of CSS!
RECAP: extract-text-webpack-plugin
In webpack 4 can not be used. Please use mini-css-extract-plugin
.
webpack 4:webpack dev server
Once you have configured webpack dev server
it will load your app in the browser.
As long as you change the file, it will automatically refresh the browser page.
The installation package to the following structures webpack dev server
:
npm i webpack-dev-server --save-dev
Then turn package.json
adjust the script:
"scripts": {
"dev": "webpack-dev-server --mode development --open",
"build": "webpack --mode production"
}
Save and close.
Now run:
npm run dev
You will see webpack dev server to load your application in a browser.
webpack dev server is very suitable for development.