Skip to content

Instantly share code, notes, and snippets.

@gkatsanos
Created May 4, 2018 09:49
Show Gist options
  • Save gkatsanos/e7316e76e99461dd095a96db019ecfe3 to your computer and use it in GitHub Desktop.
Save gkatsanos/e7316e76e99461dd095a96db019ecfe3 to your computer and use it in GitHub Desktop.
const path = require('path');
const utils = require('./utils');
const webpack = require('webpack');
const config = require('../config');
const merge = require('webpack-merge');
const baseWebpackConfig = require('./webpack.base.conf');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCSSExtractPlugin = require('mini-css-extract-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
let env;
switch (process.env.NODE_ENV) {
case 'testing':
env = require('../config/test.env');
break;
case 'e2e-mock':
env = require('../config/e2e-mock.env');
break;
case 'cit':
env = require('../config/cit.env');
break;
default:
env = config.build.env;
}
const webpackConfig = merge(baseWebpackConfig, {
module: {
rules: [
{
test: /\.scss$/,
use: [
{
loader: MiniCSSExtractPlugin.loader,
options: {
sourceMap: true,
}
},
{
loader: 'css-loader',
options: {
sourceMap: true,
}
},
{ loader: 'postcss-loader', options: { sourceMap: true } },
{
loader: 'sass-loader',
options: {
includePaths: [config.build.sassVariables],
data: '@import "variables";@import "mixins/mixins";',
sourceMap: true,
}
},
]
}
]
},
output: {
path: config.build.assetsRoot,
filename: utils.assetsPath('js/[name].[chunkhash:3].js'),
chunkFilename: utils.assetsPath('js/[name].[chunkhash:3].js')
},
optimization: {
namedChunks: true,
minimize: true
},
mode: 'production',
plugins: [
// http://vuejs.github.io/vue-loader/en/workflow/production.html
new webpack.DefinePlugin({
'process.env': env
}),
new CopyWebpackPlugin([
{
from: path.resolve(__dirname, '../static'),
to: config.dev.assetsSubDirectory,
ignore: ['.*']
}
]),
new UglifyJsPlugin({
cache: true,
parallel: true,
sourceMap: false,
extractComments: {
banner: 'The website uses open source software please visit URL below for overview of the licenses\nhttp://app.tracking.here.com/HERE_NOTICE',
},
}),
new OptimizeCSSPlugin({
cssProcessorOptions: config.build.productionSourceMap ? { safe: true, map: { inline: false } } : { safe: true }
}),
// generate dist index.html with correct asset hash for caching.
// you can customize output by editing /index.html
// see https://github.com/ampedandwired/html-webpack-plugin
new HtmlWebpackPlugin({
filename: process.env.NODE_ENV === 'testing' ? 'index.html' : config.build.index,
template: 'index.html',
inject: true,
minify: {
removeComments: true,
collapseWhitespace: true,
removeAttributeQuotes: true
// more: https://github.com/kangax/html-minifier#options-quick-reference
},
// necessary to consistently work with multiple chunks via CommonsChunkPlugin
chunksSortMode: 'dependency'
}),
new MiniCSSExtractPlugin({
filename: utils.assetsPath('css/[name].[contenthash:3].css'),
chunkFilename: utils.assetsPath('css/[id].[contenthash:3].css'),
}),
utils.momentLocaleRemoval
]
});
if (config.build.productionGzip) {
const CompressionWebpackPlugin = require('compression-webpack-plugin');
webpackConfig.plugins.push(
new CompressionWebpackPlugin({
asset: '[path].gz[query]',
algorithm: 'gzip',
test: new RegExp('\\.(' + config.build.productionGzipExtensions.join('|') + ')$'),
threshold: 10240,
minRatio: 0.8
})
);
}
if (config.build.bundleAnalyzerReport) {
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
webpackConfig.plugins.push(new BundleAnalyzerPlugin());
}
module.exports = webpackConfig;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment