Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【Webpack】常用 plugins 与配置 #41

Open
Tracked by #6
swiftwind0405 opened this issue Apr 14, 2020 · 0 comments
Open
Tracked by #6

【Webpack】常用 plugins 与配置 #41

swiftwind0405 opened this issue Apr 14, 2020 · 0 comments
Labels

Comments

@swiftwind0405
Copy link
Owner

swiftwind0405 commented Apr 14, 2020

压缩JS文件

uglifyjs-webpack-plugin

webpack.config.js

const UglifyWebpackPlugin = require('uglifyjs-webpack-plugin');

module.exports = {
    optimization: {
        minimizer: [
            new UglifyWebpackPlugin({
                parallel: 4
            })
        ]
    }
}

terser-webpack-plugin

使用terser-webpack-plugin来压缩js文件,替换掉之前的 uglifyjs-webpack-plugin,解决uglifyjs不支持es6语法问题

webpack.config.js

const TerserPlugin = require('terser-webpack-plugin');

module.exports = {
  plugins: [
    new TerserPlugin({
	    parallel: true,  // parallel 代表开启多进程
	    cache: true	     // 代表设置缓存
	}),
  ],
};

压缩CSS文件

使用 optimize-css-assets-webpack-plugin 来压缩 css 文件。

webpack.config.js

const OptimizeCssAssetsWebpackPlugin = require('optimize-css-assets-webpack-plugin');

module.exports = {
    optimization: {
        minimizer: [
            new OptimizeCssAssetsWebpackPlugin()
        ]
    }
}

OptimizeCSSAssetsPlugin 中加载了一个 cssnano 的东西, cssnano 是 PostCSS 的 CSS 优化和分解插件,会自动采用格式很好的 CSS,并通过许多优化,以确保最终的生产环境尽可能小。

拷贝静态文件

可以利用 copy-webpack-plugin 这个插件将文件拷贝到目标目录下

webpack.config.js

const CopyPlugin = require('copy-webpack-plugin');

module.exports = {
  plugins: [
    new CopyPlugin([
      { from: 'source', to: 'dest' },
      { from: 'other', to: 'public' },
    ]),
  ],
};

打包前先清空输出目录

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

module.exports = {
    plugins: [
        new CleanWebpackPlugin()
    ]
}

不是很建议这么使用,一般是在build命令前加rm -rf dist && webpack

热加载

代码改动,动态局部加载 HMR(模块热替换)

webpack自带的插件HotModuleReplacementPlugin、NamedModulesPlugin

const webpack = require('webpack');

module.exports = {
    plugins: [
            new webpack.NamedModulesPlugin(), // webpack自带的插件 以便查看修补patch依赖
    		new webpack.HotModuleReplacementPlugin()
    ]
}
@swiftwind0405 swiftwind0405 changed the title 【Day40】Webpack 中的 plugins 和配置 【Day40】Webpack 常用 plugins 与配置 Apr 14, 2020
@swiftwind0405 swiftwind0405 changed the title 【Day40】Webpack 常用 plugins 与配置 【Day40】Webpack 之 plugins Apr 17, 2020
@swiftwind0405 swiftwind0405 changed the title 【Day40】Webpack 之 plugins 【Day40】【Webpack】常用 plugins 与配置 Apr 17, 2020
@swiftwind0405 swiftwind0405 changed the title 【Day40】【Webpack】常用 plugins 与配置 【Webpack】常用 plugins 与配置 Apr 17, 2020
@swiftwind0405 swiftwind0405 changed the title 【Webpack】常用 plugins 与配置 【Webpack】【入门】常用 plugins 与配置 Apr 17, 2020
@swiftwind0405 swiftwind0405 changed the title 【Webpack】【入门】常用 plugins 与配置 【Webpack】常用 plugins 与配置 Apr 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant