1 Star 0 Fork 0

happy-learning/learning webpack

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
webpack.prod.conf.js 3.07 KB
一键复制 编辑 原始数据 按行查看 历史
gulinrang 提交于 2018-01-09 01:05 . 重构代码
'use strict';
require('shelljs/global');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
// const CleanWebpackPlugin = require('clean-webpack-plugin');
const path = require('path');
const outputPath = path.resolve(__dirname, './dist');
rm('-rf', outputPath);
module.exports = {
// entry: __dirname + '/src/main.js', // 已多次提及的唯一入口文件
entry: { // 已多次提及的唯一入口文件
app: path.join(__dirname, '/src/main.js'),
// ================================
// 框架 / 类库 分离打包
// ================================
vendor: [
'react', 'react-dom',
],
},
output: {
path: outputPath,
filename: '[name].[chunkhash:6].js',
chunkFilename: '[id].[chunkhash:6].js',
},
devtool: 'null', // 注意修改了这里,这能大大压缩我们的打包代码
devServer: {
contentBase: './src', // 本地服务器所加载的页面所在的目录
historyApiFallback: true, // 不跳转
inline: true,
hot: true,
},
module: {
rules: [{
test: /(\.jsx|\.js)$/,
use: {
loader: 'babel-loader',
},
exclude: /node_modules/,
}, {
test: /\.css$/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: [{
loader: 'css-loader',
options: {
modules: true,
localIdentName: '[name]__[local]--[hash:base64:5]', // 指定css的类名格式
},
}, {
loader: 'postcss-loader',
options: {
config: {
path: 'postcss.config.js', // 这个得在项目根目录创建此文件
},
},
}],
}),
}],
},
plugins: [
new webpack.BannerPlugin('版权所有,翻版必究'),
new HtmlWebpackPlugin({
template: path.join(__dirname, '/src/index.html'), // 简单的index.html模板,用于生成一个自动引用你打包后的JS文件的新index.html
filename: path.join(__dirname, '/dist/index.html'), // 包含引用js文件的,新index.html文件存放路径
inject: 'body', // 引用js文件的<script>标签插入的位置
}),
new ExtractTextPlugin('css/[name].[chunkhash].css'), // 使css和js文件分离
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || 'development'), // 配置的全局常量
}),
new webpack.optimize.OccurrenceOrderPlugin(), // 根据模块调用次数,给模块分配ids,常被调用的ids分配更短的id,使得ids可预测,降低文件大小,该模块推荐使用
new webpack.optimize.UglifyJsPlugin({ minimize: true }), // 压缩JS代码
new webpack.optimize.CommonsChunkPlugin({
name: 'vendor.[hash]', // 入口文件名
filename: 'vendor.[hash].bundle.js', // 打包后的文件名
}),
// new CleanWebpackPlugin('*.*', { // 每次bulid之前清理之前已经build的老文件
// root: outputPath,
// verbose: true,
// dry: false,
// }),
],
};
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/happy-learning/learning-webpack.git
git@gitee.com:happy-learning/learning-webpack.git
happy-learning
learning-webpack
learning webpack
master

搜索帮助

D67c1975 1850385 1daf7b77 1850385