Skip to content

Instantly share code, notes, and snippets.

@kamthamc
Last active March 27, 2018 23:11
Show Gist options
  • Save kamthamc/8d8c4594965882abbdd11c8338e8fe82 to your computer and use it in GitHub Desktop.
Save kamthamc/8d8c4594965882abbdd11c8338e8fe82 to your computer and use it in GitHub Desktop.
const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const envConfig = require('./config/env.config');
const appVersion = require('./package.json').version;
const stages = {
dev: 'dev',
staging: 'stg',
production: 'prod',
};
// Webpack 4 Modes
const modes = {
dev: 'development',
production: 'production',
};
const sourceMaps = {
dev: 'source-map',
stg: 'none',
prod: 'none',
};
const { env } = process;
const { stage = stages.dev, NODE_ENV: mode = modes.dev, out } = env;
const config = envConfig[stage];
const rootDir = path.resolve(__dirname);
const src = `${rootDir}/src`;
const buildDir = out || `${rootDir}/build`;
const publicPath = './build/static/public';
const plugins = [
new HtmlWebpackPlugin({
filename: 'index.html',
title: 'Web',
template: `${src}/index.html`,
trackingId: config.segmentTrackingId,
appVersion,
}),
new MiniCssExtractPlugin({
filename: '[name].css',
chunkFilename: '[name].[id].css',
}),
];
module.exports = {
mode,
bail: true,
context: rootDir,
devtool: sourceMaps[stage],
entry: {
app: `${src}/index.js`,
},
optimization: {
minimize: true,
minimizer: [
new UglifyJsPlugin({
sourceMap: true,
uglifyOptions: {
exclude: module => /node_modules/.test(module.resource),
compress: {
warnings: true,
},
mangle: false,
output: {
beautify: true,
comments: false,
},
warnings: true,
},
}),
],
nodeEnv: mode,
splitChunks: {
name: 'vendor',
minChunks: 3,
},
mergeDuplicateChunks: true,
},
target: 'web',
output: {
path: `${buildDir}`,
filename: `${publicPath}/js/[name].[hash].js`,
chunkFilename: `${publicPath}/js/[name].[chunkhash].js`,
crossOriginLoading: false,
publicPath: config.publicPath || '/',
},
resolve: {
modules: [src, `${rootDir}/node_modules/`],
extensions: ['.js', '.jsx', '.scss', '.css'],
},
externals: {
AppConfig: JSON.stringify(config),
},
module: {
strictExportPresence: true,
rules: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
},
},
{
test: /\.scss$/,
use: [
'style-loader',
{
loader: 'css-loader',
options: {
sourceMap: stage !== stages.production,
},
}, {
loader: 'sass-loader',
options: {
includePaths: [
`${src}/styles`,
],
},
},
],
},
{
test: /\.(ttf|eot|woff)(\?[a-z0-9=&.]+)?$/i,
use: [
{
loader: 'file-loader',
query: {
hash: 'sha512',
digest: 'hex',
name: '[name].[hash].[ext]',
},
},
],
},
{
test: /\.(jpe?g|png|gif|svg)(\?[a-z0-9=&.]+)?$/i,
use: [
{
loader: 'url-loader',
options: {
limit: 8192,
},
},
],
},
],
},
plugins,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment