File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11{
22 "type" : " module" ,
33 "name" : " @stackpress/lib" ,
4- "version" : " 0.6.2 " ,
4+ "version" : " 0.6.4 " ,
55 "license" : " Apache-2.0" ,
66 "description" : " Shared library used across stackpress projects" ,
77 "author" : " Chris <chris@incept.asia>" ,
Original file line number Diff line number Diff line change @@ -81,7 +81,7 @@ import cookie from './data/map.js';
8181import map from './data/map.js' ;
8282import set from './data/set.js' ;
8383
84- import FileLoader from './system/FileLoader.js' ;
84+ import FileLoader , { include } from './system/FileLoader.js' ;
8585import NodeFS from './system/NodeFS.js' ;
8686
8787import ItemQueue from './queue/ItemQueue.js' ;
@@ -127,6 +127,7 @@ export {
127127 Status ,
128128 codes ,
129129 control ,
130+ include ,
130131 cookie ,
131132 map ,
132133 set ,
Original file line number Diff line number Diff line change @@ -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 }
You can’t perform that action at this time.
0 commit comments