ironbar-persea/webpack.config.js

121 lines
3.0 KiB
JavaScript
Raw 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'),
path.join(scssPrefix, styleDirectory, 'forms.scss'),
],
},
2018-09-03 02:03:23 -05:00
optimization: {},
module: {
rules: [
{
test: /\.js$/,
2018-09-08 22:56:39 -05:00
exclude: [
/(node_modules\/(?!contempo))/,
],
2018-09-03 02:03:23 -05:00
use: {
loader: 'babel-loader',
options: {
babelrcRoots: [
OUTPUT_PATH,
],
plugins: [
'@babel/plugin-proposal-object-rest-spread',
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;
}