Skip to content

Instantly share code, notes, and snippets.

@capJavert
Last active July 25, 2018 11:25
Show Gist options
  • Save capJavert/34238d98879b0aba7f97d483f628923f to your computer and use it in GitHub Desktop.
Save capJavert/34238d98879b0aba7f97d483f628923f to your computer and use it in GitHub Desktop.
Webpack config example (js, css, scss, min, assets)
const path = require('path');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin')
const CleanWebpackPlugin = require('clean-webpack-plugin')
const CopyWebpackPlugin = require('copy-webpack-plugin')
module.exports = env => {
isProduction = env.production || false
const plugins = [
new MiniCssExtractPlugin({
filename: isProduction ? '[name].[hash].css' : '[name].css',
chunkFilename: '[id].css'
})
]
const devPlugins = [
new HtmlWebpackPlugin({
template: './src/index.html'
}),
new HtmlWebpackPlugin({
template: './src/404.html',
filename: '404.html',
}),
new CopyWebpackPlugin([
{ from: './src/assets/js/vendor', to: 'js/vendor' },
{ from: './src/assets/img', to: 'img' },
{ from: './src/favicon.ico' }
]),
new CleanWebpackPlugin(
[
'dist',
],
{
verbose: isProduction,
}
)
]
console.log('Production:', isProduction)
return {
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: isProduction ? 'main.[hash].js' : 'main.js'
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader'
}
},
{
test: /\.scss$/,
use: [
isProduction ? MiniCssExtractPlugin.loader : 'style-loader',
'css-loader',
'sass-loader',
],
},
{
test: /\.html$/,
use: [ {
loader: 'html-loader',
options: {
minimize: isProduction,
removeComments: false
}
}],
}
]
},
plugins: isProduction ? devPlugins.concat(plugins) : devPlugins
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment