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: { index: [ '@babel/polyfill', path.join(SOURCE_PATH, 'index.js'), ] }, 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: {}, mainFields: ['module', 'main'], 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; } }