Skip to content

Commit 3d9781c

Browse files
authored
fix(cli): fix entrypoint validation when running with npx on some packages with alpha API. (janus-idp#1545)
Signed-off-by: David Festal <[email protected]>
1 parent 8de16e2 commit 3d9781c

File tree

1 file changed

+45
-43
lines changed

1 file changed

+45
-43
lines changed

packages/cli/src/commands/export-dynamic-plugin/backend-embed-as-dependencies.ts

Lines changed: 45 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,16 @@ export async function backend(opts: OptionValues): Promise<string> {
161161
fs.rmSync(embeddedDestDir, { force: true, recursive: true });
162162
fs.cpSync(embedded.dir, embeddedDestDir, { recursive: true });
163163
}
164+
165+
// Remove the `node_modules` sub-folder of the embedded package,
166+
// if it has been copied (in the case typical case of wrappers).
167+
if (fs.pathExistsSync(path.join(embeddedDestDir, 'node_modules'))) {
168+
fs.rmSync(path.join(embeddedDestDir, 'node_modules'), {
169+
force: true,
170+
recursive: true,
171+
});
172+
}
173+
164174
Task.log(
165175
`Customizing embedded package ${chalk.cyan(
166176
embedded.packageName,
@@ -215,7 +225,7 @@ export async function backend(opts: OptionValues): Promise<string> {
215225
targetDir: target,
216226
});
217227

218-
// Small cleanup in case the `dist-dynamic` entr was still in the `files` of the main plugin package.
228+
// Small cleanup in case the `dist-dynamic` entry was still in the `files` of the main plugin package.
219229
if (fs.pathExistsSync(path.join(target, 'dist-dynamic'))) {
220230
fs.rmSync(path.join(target, 'dist-dynamic'), {
221231
force: true,
@@ -770,24 +780,20 @@ function isPackageShared(
770780
function validatePluginEntryPoints(target: string): string {
771781
const dynamicPluginRequire = createRequire(`${target}/package.json`);
772782

773-
// require plugin main module
774-
775-
let pluginModule: any | undefined;
776-
let needsTypeScriptSupport: boolean = false;
777-
try {
778-
pluginModule = dynamicPluginRequire(target);
779-
} catch (e) {
780-
if (
781-
e?.name === SyntaxError.name &&
782-
!dynamicPluginRequire.extensions['.ts']
783-
) {
784-
needsTypeScriptSupport = true;
785-
} else {
786-
return `Unable to validate plugin entry points: ${e}`;
783+
function requireModule(modulePath: string): any {
784+
try {
785+
return dynamicPluginRequire(modulePath);
786+
} catch (e) {
787+
// Retry only if we failed with SyntaxError because the `ts` require extension was not there.
788+
// Else we should throw.
789+
if (
790+
e?.name !== SyntaxError.name ||
791+
dynamicPluginRequire.extensions['.ts'] !== undefined
792+
) {
793+
throw e;
794+
}
787795
}
788-
}
789796

790-
if (needsTypeScriptSupport) {
791797
Task.log(
792798
` adding typescript extension support to enable entry point validation`,
793799
);
@@ -809,35 +815,31 @@ function validatePluginEntryPoints(target: string): string {
809815
}
810816

811817
// Retry requiring the plugin main module after adding typescript extensions
812-
try {
813-
pluginModule = dynamicPluginRequire(target);
814-
} catch (e) {
815-
return `Unable to validate plugin entry points: ${e}`;
816-
}
818+
return dynamicPluginRequire(modulePath);
817819
}
818820

819-
let pluginAlphaModule: any | undefined;
820-
const alphaPackage = path.resolve(target, 'alpha');
821-
if (fs.pathExistsSync(alphaPackage)) {
822-
try {
823-
pluginAlphaModule = dynamicPluginRequire(alphaPackage);
824-
} catch (e) {
825-
return `Unable to validate plugin entry points: ${e}`;
826-
}
827-
}
821+
try {
822+
const pluginModule = requireModule(target);
823+
const alphaPackage = path.resolve(target, 'alpha');
824+
const pluginAlphaModule = fs.pathExistsSync(alphaPackage)
825+
? requireModule(alphaPackage)
826+
: undefined;
828827

829-
if (
830-
![pluginAlphaModule, pluginModule]
831-
.filter(m => m !== undefined)
832-
.some(isValidPluginModule)
833-
) {
834-
return `Backend plugin is not valid for dynamic loading: it should either export a ${chalk.cyan(
835-
'BackendFeature',
836-
)} or ${chalk.cyan(
837-
'BackendFeatureFactory',
838-
)} as default export, or export a ${chalk.cyan(
839-
'const dynamicPluginInstaller: BackendDynamicPluginInstaller',
840-
)} field as dynamic loading entrypoint`;
828+
if (
829+
![pluginAlphaModule, pluginModule]
830+
.filter(m => m !== undefined)
831+
.some(isValidPluginModule)
832+
) {
833+
return `Backend plugin is not valid for dynamic loading: it should either export a ${chalk.cyan(
834+
'BackendFeature',
835+
)} or ${chalk.cyan(
836+
'BackendFeatureFactory',
837+
)} as default export, or export a ${chalk.cyan(
838+
'const dynamicPluginInstaller: BackendDynamicPluginInstaller',
839+
)} field as dynamic loading entrypoint`;
840+
}
841+
} catch (e) {
842+
return `Unable to validate plugin entry points: ${e}`;
841843
}
842844

843845
return '';

0 commit comments

Comments
 (0)