Skip to content

Commit ab4c97c

Browse files
committed
fix: wrap generated require() calls in a promise
1 parent 250b7f0 commit ab4c97c

2 files changed

Lines changed: 21 additions & 7 deletions

File tree

babel-plugin-import-meta-glob/index.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,30 @@ function ImportMetaGlobPlugin ({ types: t }) {
5454
}
5555

5656
const transformLazyImportMetaGlob = (path, files, useImport) => {
57-
const functionIdentifier = useImport ? 'import' : 'require';
57+
const importedModule = useImport
58+
? t.callExpression(
59+
t.identifier('import'),
60+
[t.stringLiteral(file)]
61+
)
62+
: t.callExpression(
63+
t.memberExpression(
64+
t.identifier('Promise'),
65+
t.identifier('resolve')
66+
),
67+
[
68+
t.callExpression(
69+
t.identifier('require'),
70+
[t.stringLiteral(file)]
71+
)
72+
]
73+
);
74+
5875
const newObjectProperties = files.map((file) => {
5976
return t.objectProperty(
6077
t.stringLiteral(file),
6178
t.arrowFunctionExpression(
6279
[],
63-
t.callExpression(
64-
t.identifier(functionIdentifier),
65-
[t.stringLiteral(file)]
66-
)
80+
importedModule
6781
)
6882
);
6983
});

babel-plugin-import-meta-glob/tests/lazy-import.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ test('it transforms import.meta.glob to lazy import', () => {
1010
});
1111
expect(output.code).toMatchInlineSnapshot(`
1212
"const modules = {
13-
"tests/fixtures/_fixture-b": () => require("tests/fixtures/_fixture-b"),
14-
"tests/fixtures/_fixture-a": () => require("tests/fixtures/_fixture-a")
13+
"tests/fixtures/_fixture-b": () => Promise.resolve(require("tests/fixtures/_fixture-b")),
14+
"tests/fixtures/_fixture-a": () => Promise.resolve(require("tests/fixtures/_fixture-a"))
1515
};"
1616
`);
1717
})

0 commit comments

Comments
 (0)