Teach you to build a basic webpack environment (1)

One, front-end construction tools

  1. grunt
  2. gulp
  3. webpack (mainstream, https://webpack.js.org/)
  4. Fis3 (Baidu)

Second, what is webpack

webpack 是一个现代 JavaScript 应用程序的静态模块打包器 webpack稳定版本:v4.44

Third, how to use webpack to build a front-end ring

  1. Install node.js (v12.x)
注意:如果npm安装一些包由于网速原因,安装慢切换一下镜像源

改成淘宝镜像:
npm config set registry https://registry.npm.taobao.org

查看是否更改过来:
npm config list回车

2. Create a project directory and initialize package.json

默认创建package.json文件

npm init -y

3. Install webpack and webpack-cli

npm install webpack webpack-cli --save-dev

简写: npm i webpack webpack-cli -D

4. Run webpack to test CommonJS specifications: based on server-side modular specifications, node output

抛出:modules.exports
引入:require

5. File types supported by webpack

默认只支持JS和json文件的引入

注意:如果在JS中要引入其他文件类型:.css,.png,字体文件或其他做任意文件类型,解析时都需求安装合适的loader来进行解析处理

6.webpack configuration file

默认配置文件名:webpack.config.js

如果想运行其他配置文件
webpack --config webpack.dev.config.js

配置文件是webpack的核心,所有的loader和插件环境,运行环境配置都在配置文件中配置使用

E.g:

//引入webpack,主要用于对webpack内置插件调用时使用
const webpack = require('webpack')
//引入path,对路径进行处理
const path = require('path')

//创建一个配置对象
const config = {
    //配置入口
    entry: './src/main.js',
    //配置出口
    output: {
        //出口路径
        path:path.resolve(__dirname,'dist'),
        //出口文件名
        filename:'bundle.js'
    }

}

module.exports = config;

7. Configure various loaders (file parsers)

  • Loader for parsing css
  • Parse image file-loader, url-loader
  • Analysis of ES6/7/8/9/10... babel
第一步:npm install --save-dev babel-loader @babel/core @babel/preset-env 
第二步:在项目根目录下创建一个.babelrc文件,并写入
{
  "presets": ["@babel/preset-env"]
}
第三步:配置loader
module: {
  rules: [
    { test: /\.js$/, exclude: /node_modules/, loader: "babel-loader" }
  ]
}

8. Automatically clean up files

npm install clean-webpack-plugin --save-dev

引入
const { CleanWebpackPlugin } = require('clean-webpack-plugin');

使用:

//创建一个配置对象
const config = {
   .....,
    plugins: [
        new CleanWebpackPlugin()
    ]

}

9. Automatically inject html

npm install --save-dev html-webpack-plugin


//创建一个配置对象
const config = {
    ....,
    plugins: [
        //自动清理
        new CleanWebpackPlugin(),
        //自动注入html
        new HtmlWebpackPlugin({
            template: './src/index.html',
            filename:'home.html'
        }),
    ]

}

10. Operating environment

webpack-dev-server
webpack-dev-server

配置文件中:
 devServer: {
        contentBase: path.join(__dirname, "dist"),  //监听运行目录
        port: 9999,  //运行端口号
        hot:true  //热更新
    }

11.Webpack core concepts:

Entrance: entry: point to the main entry point for project execution. Exit: output file path and file name of the output build. Loader: loader
converts file (resource) types not recognized by webpack. Plug-in: plugins In order to extend the functions of webpack, for example: clean files, automatic Inject Html
mode: mode switch development environment (development) or online (production) environment

12.Integrate with vue

vue-loader: Parse vue files vue-template-compiler

Installation: npm install vue-loader vue-template-compiler -D Configure webpack file:
{test:/.vue$/,use:['vue-loader']},

Instantiate the vueLoaderPlugin plugin const {VueLoaderPlugin }=require('vue-loader')
Add plugin instantiation: },
plugins: [
new VueLoaderPlugin()
]

13. Integrate with less

Integrated installation with less: npm install less-loader less -D Configuration: module: { rules: [ {test:/.less$/,use:['style-loader','css-loader','less-loader' ]}, ] },



14.与sass集成

Installation: npm install sass-loader node-sass -D
Configuration:
module: { rules: [ {test:/.(scss|sass)$/,use:['style-loader','css-loader','sass -loader']}, ] }, sass common syntax: https://www.cnblogs.com/chyingp/p/sass-basic.html




15.与vue-router

Installation: npm install vue-router -D

16. Integration with vuex

Installation: npm install vuex -D Note: There is no loader to be resolved You may need an appropriate
loader to handle this file type
16. The complete webpack.config.js file content is as follows:

/引入webpack,主要用于对webpack内置插件调用时使用
const webpack = require('webpack')
    //引入path,对路径进行处理
const path = require('path')
    //引入清理插件
const {
    
     CleanWebpackPlugin } = require('clean-webpack-plugin');
//引入htmlwebpckPlugin插件
const HtmlWebpackPlugin = require('html-webpack-plugin')
    //引入VueLoaderPlugin
const {
    
     VueLoaderPlugin } = require('vue-loader')

//创建一个配置对象
const config = {
    
    
    //线上线下环境
    mode: 'production',
    //配置入口
    entry: './src/main.js',
    //配置出口
    output: {
    
    
        //出口路径
        path: path.resolve(__dirname, 'dist'),
        //出口文件名
        filename: 'bundle.js'
    },
    module: {
    
    
        rules: [
            // {test:/\.要解析的文件类型$/,use:['要使用的loader']},
            {
    
     test: /\.css$/, use: ['style-loader', 'css-loader'] },
            {
    
     test: /\.less$/, use: ['style-loader', 'css-loader', 'less-loader'] },
            {
    
     test: /\.(scss|sass)$/, use: ['style-loader', 'css-loader', 'sass-loader'] },
            {
    
     test: /\.css$/, use: ['style-loader', 'css-loader'] },
            {
    
     test: /\.vue$/, use: ['vue-loader'] },
            {
    
     test: /\.(png|jpg|jpeg|gif)$/, use: ['file-loader'] },
            {
    
     test: /\.js$/, exclude: /node_modules/, loader: "babel-loader" }
        ]
    },
    //配置缺少文件类型
    resolve: {
    
    
        extensions: ['.js', '.json', '.vue']
    },
    plugins: [
        new CleanWebpackPlugin(),
        new HtmlWebpackPlugin({
    
    
            template: './src/index.html',
            filename: 'index.html'
        }),
        new webpack.BannerPlugin('项目的入口'),
        new VueLoaderPlugin()
    ],
    devServer: {
    
    
        contentBase: path.join(__dirname, "dist"),
        port: 9999,
        hot: true
    }

}

module.exports = config;

Guess you like

Origin blog.csdn.net/weixin_48193717/article/details/108055352