flecks/packages/core/build/webpack.js

108 lines
2.4 KiB
JavaScript
Raw Normal View History

2023-11-30 21:41:42 -06:00
const {chmod} = require('fs');
const {join} = require('path');
const CopyPlugin = require('copy-webpack-plugin');
const webpack = require('webpack');
const nodeExternals = require('webpack-node-externals');
const {
FLECKS_CORE_ROOT = process.cwd(),
} = process.env;
exports.banner = (options) => (
new webpack.BannerPlugin({
entryOnly: true,
raw: true,
...options,
})
);
exports.copy = (options) => (new CopyPlugin(options));
exports.defaultConfig = (flecks, specializedConfig) => {
2024-01-16 00:28:20 -06:00
const {extensions} = flecks;
2023-11-30 21:41:42 -06:00
const extensionsRegex = exports.regexFromExtensions(extensions);
const defaults = {
context: FLECKS_CORE_ROOT,
devtool: 'source-map',
entry: {},
module: {
rules: [
{
enforce: 'pre',
test: extensionsRegex,
use: ['source-map-loader'],
},
],
},
output: {
clean: true,
path: join(FLECKS_CORE_ROOT, 'dist'),
},
plugins: [],
resolve: {
alias: {},
extensions,
fallback: {},
},
stats: {
colors: true,
errorDetails: true,
},
};
return 'function' === typeof specializedConfig
? specializedConfig(defaults)
: {
...defaults,
...specializedConfig,
module: {
...specializedConfig.module,
rules: [
...defaults.module.rules,
...(specializedConfig.module?.rules || []),
],
},
output: {
...defaults.output,
...specializedConfig.output,
},
plugins: [
...defaults.plugins,
...(specializedConfig.plugins || []),
],
resolve: {
...defaults.resolve,
...specializedConfig.resolve,
},
stats: {
...defaults.stats,
...specializedConfig.stats,
},
};
};
// Include a shebang and set the executable bit..
exports.executable = () => ([
new class Executable {
// eslint-disable-next-line class-methods-use-this
apply(compiler) {
compiler.hooks.afterEmit.tapAsync(
'Executable',
(compilation, callback) => {
2024-01-16 00:28:20 -06:00
chmod(join(FLECKS_CORE_ROOT, 'dist', 'build', 'cli.js'), 0o755, callback);
2023-11-30 21:41:42 -06:00
},
);
}
}(),
]);
exports.externals = nodeExternals;
exports.regexFromExtensions = (exts) => (
2024-01-10 06:43:26 -06:00
new RegExp(String.raw`(?:${exts.map((ext) => ext.replaceAll('.', '\\.')).join('|')})$`)
2023-11-30 21:41:42 -06:00
);
exports.webpack = webpack;