Skip to content

Commit

Permalink
build(webpack.config.js): normalize webpack configuration across repos
Browse files Browse the repository at this point in the history
  • Loading branch information
Xunnamius committed Feb 10, 2021
1 parent b63ed69 commit 8a3bc81
Showing 1 changed file with 40 additions and 20 deletions.
60 changes: 40 additions & 20 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,44 @@
// This webpack config is used to transpile src to dist, compile externals, etc
// This webpack config is used to transpile src to dist, compile externals,
// compile executables, etc

const { EnvironmentPlugin, DefinePlugin, BannerPlugin } = require('webpack');
const { config: populateEnv } = require('dotenv');
const { verifyEnvironment } = require('./env-expect');
const nodeExternals = require('webpack-node-externals');
const debug = require('debug')(`${require('./package.json').name}:webpack-config`);

const dotenv = populateEnv();
debug('saw dotenv result: %O', dotenv);
const env = dotenv.parsed || {};
let enableDotenvSupport = false;

try {
require('fs').access('.env');
enableDotenvSupport = true;
} catch {}

const dotenv = enableDotenvSupport ? populateEnv() : null;
debug(
...(enableDotenvSupport
? ['saw dotenv result: %O', dotenv]
: ['(dotenv support disabled)'])
);
const env = dotenv?.parsed || {};
debug('saw env: %O', env);
verifyEnvironment();

const plugins = [
const envPlugins = [
// ? Load our .env results as the defaults (overridden by process.env)
new EnvironmentPlugin({ ...env, ...process.env }),
// ? Create shim process.env for undefined vars (per my tastes!)
new DefinePlugin({ 'process.env': '{}' }),
// ? Add text to the top of the entry file (if necessary)
// * ▼ For bundled CLI applications
//new BannerPlugin({ banner: '#!/usr/bin/env node', raw: true, entryOnly: true })
// * ▼ For UMD libraries
new BannerPlugin({
banner: '"undefined"!=typeof window&&(window.global=window);',
raw: true,
entryOnly: true
})
new DefinePlugin({ 'process.env': '{}' })
];

const externals = [
nodeExternals(),
({ request }, cb) =>
// ? Externalize all .json imports (required as commonjs modules)
/\.json$/.test(request) ? cb(null, `commonjs ${request}`) : cb()
];

module.exports = {
const mainConfig = {
name: 'main',
mode: 'production',
target: 'node',
Expand All @@ -42,26 +52,36 @@ module.exports = {
// ! ▼ Only required for libraries (CJS2/UMD/etc)
// ! Note: ESM outputs are handled by Babel ONLY!
libraryTarget: 'umd',
//libraryTarget: 'commonjs2',
// ! ▼ Only required for when libraryTarget is UMD (to help globals work)
globalObject: 'this'
},

externals,
externalsPresets: { node: true },

stats: {
orphanModules: true,
providedExports: true,
usedExports: true
},

externals: [nodeExternals()],
externalsPresets: { node: true },

resolve: { extensions: ['.ts', '.wasm', '.mjs', '.cjs', '.js', '.json'] },
module: {
rules: [{ test: /\.(ts|js)x?$/, loader: 'babel-loader', exclude: /node_modules/ }]
},
optimization: { usedExports: true },
ignoreWarnings: [/critical dependency:/i],
plugins
plugins: [
...envPlugins,
// * ▼ For UMD libraries
new BannerPlugin({
banner: '"undefined"!=typeof window&&(window.global=window);',
raw: true,
entryOnly: true
})
]
};

module.exports = [mainConfig];
debug('exports: %O', module.exports);

0 comments on commit 8a3bc81

Please sign in to comment.