ironbar-persea/webpack.config.js

136 lines
3.4 KiB
JavaScript
Raw Permalink Normal View History

2018-09-03 02:03:23 -05:00
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const webpack = require('webpack');
module.exports = (config) => {
if ('production' === config.mode) {
return [config, module.exports.webpackConfig()];
}
else {
return config;
}
};
const SOURCE_PATH = process.env.SOURCE_PATH || '/var/node/src';
const OUTPUT_PATH = process.env.OUTPUT_PATH || '/var/node/dist';
2018-09-08 22:56:39 -05:00
const hashFormat = {
chunk: ('production' === process.env.NODE_ENV) ? '.[chunkhash:20]' : '',
}
2018-09-03 02:03:23 -05:00
module.exports.webpackConfig = function() {
2018-09-08 22:56:39 -05:00
const styleDirectory = path.join(SOURCE_PATH, 'styles');
const cssPrefix = '!style-loader!css-loader!';
const scssPrefix = `${cssPrefix}sass-loader!`;
2018-09-03 02:03:23 -05:00
const config = {
mode: 'production' !== process.env.NODE_ENV ? 'development' : 'production',
2018-09-08 22:56:39 -05:00
entry: {
index: [
'@babel/polyfill',
path.join(SOURCE_PATH, 'frontend', 'index.js'),
],
style: [
path.join(cssPrefix, styleDirectory, 'reset.css'),
path.join(scssPrefix, styleDirectory, 'global.scss'),
],
},
2019-03-05 23:20:26 -06:00
optimization: {
splitChunks: {
cacheGroups: {
commons: {
test: /(node_modules\/(?!contempo|@avocado|@truss))/,
name: 'vendor',
chunks: 'all',
}
}
}
},
2018-09-03 02:03:23 -05:00
module: {
rules: [
{
test: /\.js$/,
2018-09-08 22:56:39 -05:00
exclude: [
2019-03-05 23:20:26 -06:00
/(node_modules\/(?!contempo|@avocado|@truss))/,
2018-09-08 22:56:39 -05:00
],
2018-09-03 02:03:23 -05:00
use: {
loader: 'babel-loader',
options: {
babelrcRoots: [
OUTPUT_PATH,
],
plugins: [
2019-03-05 23:20:26 -06:00
'babel-plugin-redux-saga',
['@babel/plugin-proposal-decorators', {
legacy: true,
}],
'@babel/plugin-proposal-class-properties',
2018-09-03 02:03:23 -05:00
'@babel/plugin-proposal-object-rest-spread',
2019-03-05 23:20:26 -06:00
'@babel/plugin-syntax-dynamic-import',
2018-09-08 22:56:39 -05:00
'react-hot-loader/babel',
2018-09-03 02:03:23 -05:00
],
presets: [
'@babel/preset-react',
'@babel/preset-env',
],
},
},
},
2018-09-08 22:56:39 -05:00
{
test: /\.css$/,
use: [{
loader: 'raw-loader',
}],
},
{
test: /\.scss$/,
use: [{
loader: 'raw-loader',
}, {
loader: 'sass-loader',
options: {
sourceMap: 'production' !== process.env.NODE_ENV,
},
}],
},
2018-09-03 02:03:23 -05:00
],
},
2018-09-08 22:56:39 -05:00
node: {
fs: 'empty',
},
2018-09-03 02:03:23 -05:00
output: {
2018-09-08 22:56:39 -05:00
filename: `[name]${hashFormat.chunk}.js`,
chunkFilename: `[id]${hashFormat.chunk}.chunk.js`,
2018-09-03 02:03:23 -05:00
path: path.join(OUTPUT_PATH, 'frontend'),
},
plugins: [
new HtmlWebpackPlugin({
title: 'Truss',
template: path.join(SOURCE_PATH, 'frontend', 'index.html'),
}),
],
resolve: {
modules: [path.join(OUTPUT_PATH, 'node_modules')],
2018-09-08 22:56:39 -05:00
},
resolveLoader: {
modules: [path.join(OUTPUT_PATH, 'node_modules')],
},
2018-09-03 02:03:23 -05:00
};
if ('production' === config.mode) {
config.optimization.minimizer = [
new UglifyJsPlugin({cache: false})
];
}
else {
2018-09-08 22:56:39 -05:00
config.devtool = 'eval-source-map';
2018-09-03 02:03:23 -05:00
config.plugins.push(new webpack.HotModuleReplacementPlugin());
}
return config;
}