flecks/packages/server/build/runtime.js

86 lines
2.9 KiB
JavaScript
Raw Normal View History

2024-01-16 00:28:20 -06:00
const {externals} = require('@flecks/core/server');
2022-02-25 04:58:08 -06:00
2023-11-30 21:41:42 -06:00
module.exports = async (config, env, argv, flecks) => {
2024-01-16 00:28:20 -06:00
const runtime = await flecks.resolver.resolve('@flecks/server/runtime');
2023-11-30 21:41:42 -06:00
// Inject flecks configuration.
2024-01-16 00:28:20 -06:00
const paths = Object.keys(flecks.flecks);
const resolvedPaths = (await Promise.all(
paths.map(async (path) => [path, await flecks.resolver.resolve(path)]),
))
.filter(([, resolved]) => resolved)
.map(([path]) => path);
2023-11-30 21:41:42 -06:00
const source = [
"process.env.FLECKS_CORE_BUILD_TARGET = 'server';",
'module.exports = (async () => ({',
` config: ${JSON.stringify(flecks.config)},`,
' loadFlecks: async () => Object.fromEntries(await Promise.all([',
2024-01-16 00:28:20 -06:00
...resolvedPaths.map((path) => (
` ['${path}', import('${path}')],`
)),
2023-11-30 21:41:42 -06:00
' ].map(async ([path, M]) => [path, await M]))),',
2024-01-16 00:28:20 -06:00
` stubs: ${JSON.stringify(flecks.stubs.map((stub) => (
stub instanceof RegExp ? [stub.source, stub.flags] : stub
)))}`,
2023-11-30 21:41:42 -06:00
'}))();',
];
// HMR.
source.push('if (module.hot) {');
// Keep HMR junk out of our output path.
source.push(' const {unlink} = require("fs/promises");');
source.push(' const {join} = require("path");');
source.push(' let previousHash = __webpack_hash__;');
source.push(' module.hot.addStatusHandler((status) => {');
source.push(' if ("idle" === status) {');
source.push(' require("glob")(');
source.push(` join('${config.output.path}', \`*\${previousHash}.hot-update.*\`),`);
source.push(' async (error, disposing) => {');
source.push(' if (error) {');
source.push(' throw error;');
source.push(' return;');
source.push(' }');
source.push(' await Promise.all(disposing.map(unlink));');
source.push(' },');
source.push(' );');
source.push(' previousHash = __webpack_hash__;');
source.push(' }');
source.push(' });');
// Hooks for each fleck.
2024-01-16 00:28:20 -06:00
resolvedPaths.forEach((path) => {
2023-11-30 21:41:42 -06:00
source.push(` module.hot.accept('${path}', async () => {`);
source.push(` global.flecks.refresh('${path}', require('${path}'));`);
source.push(` global.flecks.invoke('@flecks/core.hmr', '${path}');`);
2022-02-25 04:58:08 -06:00
source.push(' });');
2023-11-30 21:41:42 -06:00
});
source.push('}');
// Create runtime.
config.module.rules.push(
{
test: runtime,
use: [
{
loader: runtime,
options: {
source: source.join('\n'),
},
},
],
},
);
const allowlist = [
'@flecks/server/entry',
'@flecks/server/runtime',
/^@babel\/runtime\/helpers\/esm/,
];
config.resolve.alias['@flecks/server/runtime$'] = runtime;
const nodeExternalsConfig = {
allowlist,
2022-03-01 10:14:38 -06:00
};
2024-01-16 00:28:20 -06:00
flecks.runtimeCompiler('server', config, nodeExternalsConfig);
2023-11-30 21:41:42 -06:00
// Rewrite to signals for HMR.
if ('production' !== argv.mode) {
allowlist.push(/^webpack/);
}
// Externalize the rest.
config.externals = externals(nodeExternalsConfig);
2022-02-25 04:58:08 -06:00
};