cli: add fix for module resolution in backend bundle config

This commit is contained in:
Patrik Oldsberg
2021-01-29 16:58:18 +01:00
parent ec02b0591d
commit 03177610a8
+30 -1
View File
@@ -219,7 +219,7 @@ export async function createBackendConfig(
}
: {}),
externals: [
nodeExternals({
nodeExternalsWithResolve({
modulesDir: paths.rootNodeModules,
additionalModuleDirs: moduleDirs,
allowlist: ['webpack/hot/poll?100', ...localPackageNames],
@@ -296,3 +296,32 @@ export async function createBackendConfig(
],
};
}
// This makes the module resolution happen from the context of each non-external module, rather
// than the main entrypoint. This fixes a bug where dependencies would be resolved from the backend
// package rather than each individual backend package and plugin.
//
// TODO(Rugvip): Feature suggestion/contribute this to webpack-externals
function nodeExternalsWithResolve(
options: Parameters<typeof nodeExternals>[0],
) {
let currentContext: string;
const externals = nodeExternals({
...options,
importType(request) {
const resolved = require.resolve(request, {
paths: [currentContext],
});
return `commonjs ${resolved}`;
},
});
return (
context: string,
request: string,
callback: webpack.ExternalsFunctionCallback,
) => {
currentContext = context;
return externals(context, request, callback);
};
}