File tree 2 files changed +36
-8
lines changed 2 files changed +36
-8
lines changed Original file line number Diff line number Diff line change @@ -13,13 +13,25 @@ export async function loadPlugins(
13
13
const pluginDisplay = getPluginDisplayName ( plugin ) ;
14
14
15
15
try {
16
- // On Windows, we need to ensure this path is a file path.
17
- // Or we'll get ERR_UNSUPPORTED_ESM_URL_SCHEME
18
- const esmPath = isAbsolute ( plugin )
19
- ? pathToFileURL ( plugin ) . toString ( )
20
- : plugin ;
21
- let instance : any = await import ( esmPath ) ;
22
- const initFunction = instance . load || instance . default ?. load ;
16
+ let instance : any ;
17
+ // Try importing first to avoid warnings about requiring ESM being experimental.
18
+ // If that fails due to importing a directory, fall back to require.
19
+ try {
20
+ // On Windows, we need to ensure this path is a file path.
21
+ // Or we'll get ERR_UNSUPPORTED_ESM_URL_SCHEME
22
+ const esmPath = isAbsolute ( plugin )
23
+ ? pathToFileURL ( plugin ) . toString ( )
24
+ : plugin ;
25
+ instance = await import ( esmPath ) ;
26
+ } catch ( error : any ) {
27
+ if ( error . code === "ERR_UNSUPPORTED_DIR_IMPORT" ) {
28
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
29
+ instance = require ( plugin ) ;
30
+ } else {
31
+ throw error ;
32
+ }
33
+ }
34
+ const initFunction = instance . load ;
23
35
24
36
if ( typeof initFunction === "function" ) {
25
37
await initFunction ( app ) ;
Original file line number Diff line number Diff line change @@ -14,7 +14,7 @@ describe("loadPlugins", () => {
14
14
logger = fakeApp . logger = new TestLogger ( ) ;
15
15
} ) ;
16
16
17
- it ( "Should support loading a basic plugin" , async ( ) => {
17
+ it ( "Should support loading a CJS plugin with directory target " , async ( ) => {
18
18
using project = tempdirProject ( ) ;
19
19
project . addJsonFile ( "package.json" , {
20
20
type : "commonjs" ,
@@ -28,6 +28,22 @@ describe("loadPlugins", () => {
28
28
logger . expectMessage ( `info: Loaded plugin ${ plugin } ` ) ;
29
29
} ) ;
30
30
31
+ it ( "Should support loading a CJS plugin with full path" , async ( ) => {
32
+ using project = tempdirProject ( ) ;
33
+ project . addJsonFile ( "package.json" , {
34
+ type : "commonjs" ,
35
+ main : "index.js" ,
36
+ } ) ;
37
+ const plugin = project . addFile (
38
+ "index.js" ,
39
+ "exports.load = function load() {}" ,
40
+ ) . path ;
41
+ project . write ( ) ;
42
+
43
+ await loadPlugins ( fakeApp , [ plugin ] ) ;
44
+ logger . expectMessage ( `info: Loaded plugin ${ plugin } ` ) ;
45
+ } ) ;
46
+
31
47
it ( "Should support loading a ESM plugin" , async ( ) => {
32
48
using project = tempdirProject ( ) ;
33
49
project . addJsonFile ( "package.json" , {
You can’t perform that action at this time.
0 commit comments