fix: server stubs

This commit is contained in:
cha0s 2022-03-12 09:03:07 -06:00
parent 0ddb8e2d29
commit 365a855abc
5 changed files with 10 additions and 16 deletions

View File

@ -26,8 +26,6 @@
"build",
"cli.js",
"cli.js.map",
"empty.js",
"empty.js.map",
"index.js",
"index.js.map",
"server.js",

View File

@ -513,7 +513,7 @@ export default class ServerFlecks extends Flecks {
return undefined;
}
runtimeCompiler(runtime, neutrino, allowlist = []) {
runtimeCompiler(runtime, neutrino, {additionalModuleDirs = [], allowlist = []} = {}) {
const {config} = neutrino;
// Pull the default compiler.
if (config.module.rules.has('compile')) {
@ -546,6 +546,7 @@ export default class ServerFlecks extends Flecks {
const resolved = dirname(R.resolve(join(root, 'package.json')));
const sourcepath = this.sourcepath(resolved);
const sourceroot = join(sourcepath, '..');
additionalModuleDirs.push(join(sourceroot, 'node_modules'));
const configFile = this.buildConfig('babel.config.js');
debug('compiling: %s with %s', root, configFile);
const babel = {

View File

@ -65,13 +65,17 @@ module.exports = async (flecks) => {
];
config.resolve.alias
.set('@flecks/server/runtime$', runtime);
flecks.runtimeCompiler('server', neutrino, allowlist);
const nodeExternalsConfig = {
additionalModuleDirs: [],
allowlist,
};
flecks.runtimeCompiler('server', neutrino, nodeExternalsConfig);
// Rewrite to signals for HMR.
if ('production' !== config.get('mode')) {
allowlist.push(/^webpack/);
}
// Externalize the rest.
const nodeExternals = R('webpack-node-externals');
config.externals(nodeExternals({allowlist}));
config.externals(nodeExternals(nodeExternalsConfig));
};
};

View File

@ -109,19 +109,10 @@ module.exports = (async () => {
start,
],
};
// Stub out non-server-friendly modules on the server.
const stubs = flecks.stubs();
if (stubs.length > 0) {
config.use.unshift(({config}) => {
stubs.forEach((path) => {
config.resolve.alias
.set(path, '@flecks/core/empty');
});
});
}
// Hardcore hax for module aliasing.
const aliases = flecks.aliases();
// Do we need to get up in `require()`'s guts?
if (
Object.keys(aliases).length > 0
|| stubs.length > 0
@ -130,7 +121,7 @@ module.exports = (async () => {
.map((stub) => (
'string' === typeof stub
? JSON.stringify(stub)
: `new RegExp(${JSON.stringify(stub.toString())})`
: `new RegExp(${JSON.stringify(stub.toString().slice(1, -1))})`
));
const code = [
`const aliases = ${JSON.stringify(aliases)};`,