Skip to content

Commit 462317f

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

2 files changed

Lines changed: 25 additions & 7 deletions

File tree

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

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,35 @@ function ImportMetaGlobPlugin ({ types: t }) {
5353
path.replaceWith(t.objectExpression(newObjectProperties));
5454
}
5555

56+
const generateImportCall = (file, useImport) => {
57+
if (useImport) {
58+
return t.callExpression(
59+
t.identifier('import'),
60+
[t.stringLiteral(file)]
61+
);
62+
} else {
63+
return t.callExpression(
64+
t.memberExpression(
65+
t.identifier('Promise'),
66+
t.identifier('resolve')
67+
),
68+
[
69+
t.callExpression(
70+
t.identifier('require'),
71+
[t.stringLiteral(file)]
72+
)
73+
]
74+
);
75+
}
76+
}
77+
5678
const transformLazyImportMetaGlob = (path, files, useImport) => {
57-
const functionIdentifier = useImport ? 'import' : 'require';
5879
const newObjectProperties = files.map((file) => {
5980
return t.objectProperty(
6081
t.stringLiteral(file),
6182
t.arrowFunctionExpression(
6283
[],
63-
t.callExpression(
64-
t.identifier(functionIdentifier),
65-
[t.stringLiteral(file)]
66-
)
84+
generateImportCall(file, useImport)
6785
)
6886
);
6987
});

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)