truss/webpack/webpack.config.js

132 lines
3.2 KiB
JavaScript
Raw Normal View History

2018-12-23 07:56:44 -06:00
const fs = require('fs');
const path = require('path');
const StartServerPlugin = require('start-server-webpack-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const webpack = require('webpack');
const nodeExternals = require('webpack-node-externals');
const SOURCE_PATH = process.env.SOURCE_PATH || '/var/node/src';
const OUTPUT_PATH = process.env.OUTPUT_PATH || '/var/node/dist';
const hashFormat = {
chunk: ('production' === process.env.NODE_ENV) ? '.[chunkhash:20]' : '',
}
function defaultConfig() {
const config = {
mode: 'production' !== process.env.NODE_ENV ? 'development' : 'production',
entry: {
2019-03-05 19:21:09 -06:00
index: [
'@babel/polyfill',
path.join(SOURCE_PATH, 'index.js'),
]
2018-12-23 07:56:44 -06:00
},
target: 'node',
output: {
filename: `[name]${hashFormat.chunk}.js`,
chunkFilename: `[id]${hashFormat.chunk}.chunk.js`,
path: OUTPUT_PATH,
},
optimization: {},
externals: [nodeExternals({
whitelist: [
'webpack/hot/poll?1000',
/^@truss/,
],
})],
module: {
rules: [
{
test: /\.js$/,
exclude: [
'node_modules',
],
use: {
loader: 'babel-loader',
options: {
babelrcRoots: [
OUTPUT_PATH,
],
plugins: [
['@babel/plugin-proposal-decorators', {
legacy: true,
}],
'@babel/plugin-proposal-class-properties',
'@babel/plugin-proposal-object-rest-spread',
],
presets: [
'@babel/preset-env',
],
},
},
},
{
test: /\.css$/,
use: [{
loader: 'raw-loader'
}],
},
{
test: /\.scss$/,
use: [{
loader: 'raw-loader',
}, {
loader: 'sass-loader',
options: {
sourceMap: 'production' !== process.env.NODE_ENV,
},
}],
},
],
},
plugins: [
new StartServerPlugin({
entryName: 'index',
restartable: false,
}),
],
resolve: {
alias: {},
2019-03-05 19:21:09 -06:00
mainFields: ['module', 'main'],
2018-12-23 07:56:44 -06:00
modules: [path.join(OUTPUT_PATH, 'node_modules')],
},
resolveLoader: {
modules: [path.join(OUTPUT_PATH, 'node_modules')],
},
};
if ('production' !== config.mode) {
config.devtool = 'sourcemap';
config.entry.whp = 'webpack/hot/poll?1000';
config.plugins.push(new webpack.BannerPlugin({
banner: 'require("source-map-support").install();',
raw: true,
entryOnly: false,
}));
config.plugins.push(new webpack.HotModuleReplacementPlugin());
}
else {
config.optimization.minimizer = [
new UglifyJsPlugin({cache: false})
];
config.externals = [];
}
return config
}
module.exports = () => {
const defaults = defaultConfig();
try {
const config = require(`${SOURCE_PATH}/webpack.config`);
return typeof config === "function" ? config(defaults) : config;
}
catch (error) {
if (`Cannot find module '${SOURCE_PATH}/webpack.config'` !== error.message) {
throw error;
}
return defaults;
}
}