Created
February 10, 2019 20:29
-
-
Save mwmcode/45e37b8d137fb09d10d3ccb51a609f70 to your computer and use it in GitHub Desktop.
webpack config
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// common | |
const path = require('path'); | |
const CleanWebpackPlugin = require('clean-webpack-plugin'); | |
const HtmlWebpackPlugin = require('html-webpack-plugin'); | |
const Dotenv = require('dotenv-webpack'); | |
const rootpath = path.resolve(__dirname, '..'); | |
const fontRegex = /.(eot|ttf|woff|otf|woff2)$/; | |
const imgRegex = /.(png|jpg|gif|svg)$/; | |
const fileRegex = new RegExp(`(${fontRegex.source})|(${imgRegex.source})`); | |
module.exports = { | |
entry: { | |
vendor: ['react', 'react-dom', 'react-router-dom'], | |
app: './src/index.js', | |
}, | |
output: { | |
path: `${rootpath}/dist`, | |
filename: '[name].js', | |
chunkFilename: '[name].chunk.js', | |
}, | |
resolve: { | |
extensions: ['.mjs', '.js', '.jsx'], | |
alias: { | |
common: `${rootpath}/src/components/common`, | |
routes: `${rootpath}/src/routes`, | |
utils: `${rootpath}/src/utils`, | |
}, | |
}, | |
plugins: [ | |
new CleanWebpackPlugin([`dist`], { | |
root: rootpath, | |
exclude: ['manifest.json', 'favicon.ico'], | |
verbose: true, | |
dry: false, | |
}), | |
new HtmlWebpackPlugin({ | |
template: 'src/index.template.html', | |
}), | |
new Dotenv({ | |
path: `${rootpath}/.env`, | |
}), | |
], | |
module: { | |
rules: [ | |
{ | |
enforce: 'pre', | |
test: /\.(js|jsx)$/, | |
use: ['eslint-loader'], | |
exclude: /node_modules/, | |
}, | |
{ | |
test: /\.(js|jsx)$/, | |
exclude: /node_modules/, | |
use: ['babel-loader'], | |
}, | |
{ | |
test: /\.css$/, | |
exclude: /\.module\.css$/, | |
use: [{loader: 'style-loader'}, {loader: 'css-loader'}], | |
}, | |
{ | |
test: /\.module\.css$/, | |
use: [ | |
{ | |
loader: 'style-loader', | |
}, | |
{ | |
loader: 'css-loader', | |
options: { | |
camelCase: true, | |
modules: true, | |
localIdentName: '[local]_[hash:base64:5]', | |
}, | |
}, | |
], | |
}, | |
{ | |
test: fileRegex, | |
use: [ | |
{ | |
loader: 'file-loader', | |
options: { | |
outputPath: (url, resourcePath, context) => { | |
const dir = 'assets'; | |
if (imgRegex.test(resourcePath)) { | |
return `${dir}/images/${url}`; | |
} | |
if (fontRegex.test(resourcePath)) { | |
return `${dir}/fonts/${url}`; | |
} | |
return `${dir}/${url}`; | |
}, | |
}, | |
}, | |
], | |
}, | |
], | |
}, | |
}; | |
// dev | |
const path = require('path'); | |
const webpack = require('webpack'); | |
const merge = require('webpack-merge'); | |
const common = require('./webpack.common.js'); | |
module.exports = merge(common, { | |
mode: 'development', | |
devtool: 'inline-source-map', | |
plugins: [ | |
new webpack.NamedModulesPlugin(), | |
new webpack.HotModuleReplacementPlugin(), | |
], | |
devServer: { | |
contentBase: path.resolve(__dirname, '../', 'dist'), | |
host: '0.0.0.0', | |
port: 10004, | |
public: `0.0.0.0:${10004}`, | |
publicPath: '/', | |
headers: {'Access-Control-Allow-Origin': '*'}, | |
stats: { | |
all: false, | |
assets: true, | |
builtAt: true, | |
chunks: true, | |
colors: true, | |
errors: true, | |
chunkOrigins: false, | |
}, | |
}, | |
}); | |
// prod | |
const merge = require('webpack-merge'); | |
const UglifyJsPlugin = require('uglifyjs-webpack-plugin'); | |
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin'); | |
const {BundleAnalyzerPlugin} = require('webpack-bundle-analyzer'); | |
const common = require('./webpack.common.js'); | |
module.exports = merge(common, { | |
mode: 'production', | |
optimization: { | |
minimizer: [ | |
new UglifyJsPlugin({ | |
cache: true, | |
parallel: true, | |
sourceMap: false, // set to true if you want JS source maps | |
}), | |
new OptimizeCSSAssetsPlugin({}), | |
], | |
}, | |
plugins: [ | |
new BundleAnalyzerPlugin({ | |
analyzerMode: 'static', | |
generateStatsFile: true, | |
reportFilename: 'analysis/report.html', | |
openAnalyzer: false, | |
statsFilename: 'analysis/stats.json', | |
}), | |
], | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment