React从入门到放弃之前奏(1):webpack4简介

React从入门到放弃之前奏(1):webpack4简介

接触webpack是好久之前的事情了,最近看了下webpack没想到都到4了。

webpack 是一个现代 JavaScript 应用程序的静态模块打包器(module bundler).

会创建1个依赖关系图(dependency graph),包含所有依赖的模块,然后将模块打包成1个或多个bundle.

webpack4 仍然支持高度可配,但完全可以不用配置文件了(基于mode)。

核心配置:

  • 入口(entry):
  • 输出(output):
  • loader:
  • 插件(plugins):

基本特性

Entry

入口起点(entry point)指示 webpack 应该使用哪个模块,来作为构建其内部依赖图的开始。

可以通过在 webpack 配置中配置 entry 属性,来指定一个入口起点(或多个入口起点)。默认值为 ./src/index.js

webpack.config.js

module.exports = {
    entry: {
        main: './src/index.js'
    },
};

Output

output 属性告诉 webpack 在哪里输出它所创建的 bundles,以及如何命名这些文件,默认值为./dist/[name].js

webpack.config.js

const path = require('path');

module.exports = {
  entry: './src/index.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: '[name].js'
  }
};

Loader

loader 让 webpack 能够去处理那些非 JavaScript 文件(webpack 自身只理解 JavaScript)。

loaders 有2个核心参数:

  1. test 属性,用于标识出需要转换的某个或某些文件。
  2. use 属性,表示进行转换时,应该使用哪个 loader。
const path = require('path');

const config = {
  output: {
    filename: 'bundle.js'
  },
  module: {
    rules: [
      { test: /\.txt$/, use: 'raw-loader' }
    ]
  }
};

module.exports = config;

Plugins

插件的范围包括,从打包优化和压缩,一直到重新定义环境中的变量。插件接口功能极其强大,可以用来处理各种各样的任务。

webpack.config.js

const HtmlWebpackPlugin = require('html-webpack-plugin'); // 通过 npm 安装
const webpack = require('webpack'); // 用于访问内置插件

const config = {
  module: {
    rules: [
      { test: /\.txt$/, use: 'raw-loader' }
    ]
  },
  plugins: [
    new webpack.optimize.UglifyJsPlugin(),
    new HtmlWebpackPlugin({template: './src/index.html'})
  ]
};

module.exports = config;

Mode

通过选择 development 或 production 之中的一个,来设置 mode 参数

webpack.config.js

module.exports = {
  mode: 'production'
};

mode.js

// webpack.development.config.js
module.exports = www.feifanyule.cn/ {
+ mode: 'development'
- plugins: [
-   new webpack.NamedModulesPlugin(),
-   new webpack.DefinePlugin({ "process.env.NODE_ENV": JSON.stringify("development") }),
- ]
}
// webpack.production.config.js
module.exports = {
+  mode: 'production',
-  plugins: [
-    new UglifyJsPlugin(/* ... */),
-    new webpack.DefinePlugin({ "process.env.NODE_ENV": JSON.stringify("production") }),
-    new webpack.optimize.ModuleConcatenationPlugin(),
-    new webpack.NoEmitOnErrorsPlugin(www.baohuayule.net )
-  ]
}

常用插件

HtmlWebpackPlugin:

Options

const HtmlWebpackPlugin = require('html-webpack-plugin');

new HtmlWebpackPlugin({ template: 'src/index.html', minify: true, hash: true })

CleanWebpackPlugin:

const CleanWebpackPlugin = require('clean-webpack-plugin');

new CleanWebpackPlugin(['dist'])

MiniCssExtractPlugin(ExtractTextPlugin):

new MiniCssExtractPlugin({www.ysgj1688.com  filename: "[name].css",chunkFilename: "[id].css" })

module: {
    rules: [{
        test: /\.css$/,
        use: [
            MiniCssExtractPlugin.loader,
            'css-loader'
        ]}
    ]
}

SplitChunksPlugin:

module.exports = {
    mode: 'development',
    entry: {
        main: '.www.089188.cn /src/index.js',
        vendors: 'lodash'
    },
    optimization: {
        splitChunks: {
            cacheGroups: {
                vendors: {
                    test: /[\\/]node_modules[\\/]/,
                    name: "vendors",
                    chunks: "initial"
                }
            }
        }
    }
}

配置示例

const CleanWebpackPlugin = www.vboyule.cn   require('clean-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");

const path = require('path');

module.exports = {
    mode: 'development',
    entry: {
        main: './src/index.js',
    },
    devtool: 'inline-source-map',
    devServer: {
        contentBase:www.qinlinyule.cn/  './dist'
    },
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: '[name].js',
    },
    module: {
        rules: [{
            test: /\.css$/,
            use: [
                MiniCssExtractPlugin.loader,
                'css-loader'
            ]
        }, {
            test: /\.(js|jsx)$/,
            loader: 'babel-loader',
            options: {
                presets: ['react']
            },
            exclude: www.120xh.cn  /node_modules/
        }]
    },
    plugins: [
        new CleanWebpackPlugin(['dist']),
        new HtmlWebpackPlugin({ template: 'src/index.html', minify: true, hash: true }),
        new MiniCssExtractPlugin({ filename: "[name].css", chunkFilename: "[id].css" })
    ],
    externals: {
        lodash: '_',
        react: 'React',
        'react-dom': 'ReactDOM'
    },
    optimization: {
        splitChunks: {
            cacheGroups: www.wmyl88.com {
                common: {
                    test: /[\\/]node_modules[\\/]/,
                    name: www.hjsd1.com "common",
                    chunks: "initial"
                }
            }
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值