@@ -161,6 +161,16 @@ export async function backend(opts: OptionValues): Promise<string> {
161
161
fs . rmSync ( embeddedDestDir , { force : true , recursive : true } ) ;
162
162
fs . cpSync ( embedded . dir , embeddedDestDir , { recursive : true } ) ;
163
163
}
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
+
164
174
Task . log (
165
175
`Customizing embedded package ${ chalk . cyan (
166
176
embedded . packageName ,
@@ -215,7 +225,7 @@ export async function backend(opts: OptionValues): Promise<string> {
215
225
targetDir : target ,
216
226
} ) ;
217
227
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.
219
229
if ( fs . pathExistsSync ( path . join ( target , 'dist-dynamic' ) ) ) {
220
230
fs . rmSync ( path . join ( target , 'dist-dynamic' ) , {
221
231
force : true ,
@@ -770,24 +780,20 @@ function isPackageShared(
770
780
function validatePluginEntryPoints ( target : string ) : string {
771
781
const dynamicPluginRequire = createRequire ( `${ target } /package.json` ) ;
772
782
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
+ }
787
795
}
788
- }
789
796
790
- if ( needsTypeScriptSupport ) {
791
797
Task . log (
792
798
` adding typescript extension support to enable entry point validation` ,
793
799
) ;
@@ -809,35 +815,31 @@ function validatePluginEntryPoints(target: string): string {
809
815
}
810
816
811
817
// 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 ) ;
817
819
}
818
820
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 ;
828
827
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 } ` ;
841
843
}
842
844
843
845
return '' ;
0 commit comments