cli: add local linking of backend packages too

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2024-10-22 12:54:41 +02:00
parent df231cf395
commit d2e166cbb2
5 changed files with 41 additions and 11 deletions
+33
View File
@@ -16,6 +16,39 @@
const { transformSync } = require('@swc/core');
const { addHook } = require('pirates');
const { Module } = require('module');
// This hooks into module resolution and overrides imports of packages that
// exist in the linked workspace to instead be resolved from the linked workspace.
if (process.env.LINKED_WORKSPACE) {
const { join: joinPath } = require('path');
const { getPackagesSync } = require('@manypkg/get-packages');
const { packages: linkedPackages, root: linkedRoot } = getPackagesSync(
process.env.LINKED_WORKSPACE,
);
// Matches all packages in the linked workspaces, as well as sub-path exports from them
const replacementRegex = new RegExp(
`^(?:${linkedPackages
.map(pkg => pkg.packageJson.name)
.join('|')})(?:/.*)?$`,
);
const origLoad = Module._load;
Module._load = function requireHook(request, parent) {
if (!replacementRegex.test(request)) {
return origLoad.call(this, request, parent);
}
// The package import that we're overriding will always existing in the root
// node_modules of the linked workspace, so it's enough to override the the
// parent paths with that single entry
return origLoad.call(this, request, {
...parent,
paths: [joinPath(linkedRoot.dir, 'node_modules')],
});
};
}
addHook(
(code, filename) => {
@@ -34,20 +34,10 @@ export async function command(opts: OptionValues): Promise<void> {
switch (role) {
case 'backend':
if (options.linkedWorkspace) {
throw new Error(
'The --link flag is not supported for this package role',
);
}
return startBackend(options);
case 'backend-plugin':
case 'backend-plugin-module':
case 'node-library':
if (options.linkedWorkspace) {
throw new Error(
'The --link flag is not supported for this package role',
);
}
return startBackendPlugin(options);
case 'frontend':
return startFrontend({
@@ -22,6 +22,7 @@ interface StartBackendOptions {
checksEnabled: boolean;
inspectEnabled: boolean;
inspectBrkEnabled: boolean;
linkedWorkspace?: string;
require?: string;
}
@@ -30,6 +31,7 @@ export async function startBackend(options: StartBackendOptions) {
entry: 'src/index',
inspectEnabled: options.inspectEnabled,
inspectBrkEnabled: options.inspectBrkEnabled,
linkedWorkspace: options.linkedWorkspace,
require: options.require,
});
@@ -52,6 +54,7 @@ export async function startBackendPlugin(options: StartBackendOptions) {
inspectEnabled: options.inspectEnabled,
inspectBrkEnabled: options.inspectBrkEnabled,
require: options.require,
linkedWorkspace: options.linkedWorkspace,
});
await waitForExit();
@@ -40,6 +40,8 @@ export type RunBackendOptions = {
inspectBrkEnabled: boolean;
/** Additional module to require via the --require flag to the node process */
require?: string;
/** An external linked workspace to override module resolution towards */
linkedWorkspace?: string;
};
export async function runBackend(options: RunBackendOptions) {
@@ -119,6 +121,7 @@ export async function runBackend(options: RunBackendOptions) {
stdio: ['ignore', 'inherit', 'inherit', 'ipc'],
env: {
...process.env,
LINKED_WORKSPACE: options.linkedWorkspace,
BACKSTAGE_CLI_CHANNEL: '1',
ESBK_TSCONFIG_PATH: paths.resolveTargetRoot('tsconfig.json'),
},