Last active
July 25, 2018 11:25
-
-
Save capJavert/34238d98879b0aba7f97d483f628923f to your computer and use it in GitHub Desktop.
Webpack config example (js, css, scss, min, assets)
This file contains hidden or 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 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