Skip to content

Commit 8072af9

Browse files
committed
version bump
1 parent d74a49b commit 8072af9

3 files changed

Lines changed: 16 additions & 3 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
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>",

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ import cookie from './data/map.js';
8181
import map from './data/map.js';
8282
import set from './data/set.js';
8383

84-
import FileLoader from './system/FileLoader.js';
84+
import FileLoader, { include } from './system/FileLoader.js';
8585
import NodeFS from './system/NodeFS.js';
8686

8787
import ItemQueue from './queue/ItemQueue.js';
@@ -127,6 +127,7 @@ export {
127127
Status,
128128
codes,
129129
control,
130+
include,
130131
cookie,
131132
map,
132133
set,

src/system/FileLoader.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@ import { pathToFileURL } from 'node:url';
55
import type { FileSystem } from '../types.js';
66
import 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

Comments
 (0)