@@ -5,6 +5,17 @@ import { pathToFileURL } from 'node:url';
55import type { FileSystem } from '../types.js' ;
66import Exception from '../Exception.js' ;
77
8+ /**
9+ * When TS builds to CJS, it converts dynamic import() to require()
10+ * which does not work with ESM-only packages. So this is basically
11+ * a monkey-patch to allow dynamic import of ESM packages in CJS
12+ * builds. Either this is a safer way to do dynamic imports.
13+ */
14+ export const include = new Function (
15+ 'modulePath' ,
16+ 'return import(modulePath)'
17+ ) ;
18+
819/**
920 * Loader
1021 */
@@ -98,6 +109,7 @@ export default class FileLoader {
98109 if ( meta ) {
99110 try {
100111 //@ts -ignore - require supported in all node versions
112+ // (esm, cjs and otherwise)
101113 const absolute = require . resolve ( pathname ) ;
102114 if ( absolute . includes ( '/node_modules/' ) ) {
103115 //get the last index of node_modules
@@ -162,7 +174,7 @@ export default class FileLoader {
162174 }
163175 //if no require, try to esm dynamic import
164176 if ( typeof require === 'undefined' ) {
165- const imports = await import ( pathToFileURL ( absolute ) . href ) ;
177+ const imports = await include ( pathToFileURL ( absolute ) . href ) ;
166178 if ( getDefault ) {
167179 return imports . default as T ;
168180 }
0 commit comments