Created
December 23, 2020 23:31
-
-
Save donrestarone/33a209b4d82f4d20fde1d84602c9c572 to your computer and use it in GitHub Desktop.
webpack configuration for React (in a Phoenix application)
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
const path = require('path'); | |
const glob = require('glob'); | |
const HardSourceWebpackPlugin = require('hard-source-webpack-plugin'); | |
const MiniCssExtractPlugin = require('mini-css-extract-plugin'); | |
const TerserPlugin = require('terser-webpack-plugin'); | |
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin'); | |
const CopyWebpackPlugin = require('copy-webpack-plugin'); | |
module.exports = (env, options) => { | |
const devMode = options.mode !== 'production'; | |
return { | |
optimization: { | |
minimizer: [ | |
new TerserPlugin({ cache: true, parallel: true, sourceMap: devMode }), | |
new OptimizeCSSAssetsPlugin({}) | |
] | |
}, | |
entry: { | |
'app': glob.sync('./vendor/**/*.js').concat(['./js/app.js']) | |
}, | |
output: { | |
filename: '[name].js', | |
path: path.resolve(__dirname, '../priv/static/js'), | |
publicPath: '/js/' | |
}, | |
devtool: devMode ? 'eval-cheap-module-source-map' : undefined, | |
module: { | |
rules: [ | |
{ | |
test: /\.(js|jsx)$/, /* add jsx */ | |
exclude: /node_modules/, | |
use: { | |
loader: 'babel-loader' | |
} | |
}, | |
{ | |
test: /\.[s]?css$/, | |
use: [ | |
MiniCssExtractPlugin.loader, | |
'css-loader', | |
'sass-loader', | |
], | |
} | |
] | |
}, | |
plugins: [ | |
new MiniCssExtractPlugin({ filename: '../css/app.css' }), | |
new CopyWebpackPlugin([{ from: 'static/', to: '../' }]) | |
] | |
.concat(devMode ? [new HardSourceWebpackPlugin()] : []) | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment