Skip to content
This repository was archived by the owner on May 22, 2024. It is now read-only.

Commit f91c49d

Browse files
authored
fix: fix and test dynamic import() (#350)
1 parent a437e74 commit f91c49d

File tree

4 files changed

+23
-3
lines changed

4 files changed

+23
-3
lines changed

src/node_dependencies/index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,11 @@ const getFileDependencies = async function ({ path, packageJson, pluginsModulesP
6969
// but should use `fs.readFile()` instead
7070
const dependencies = precinct.paperwork(path, { includeCore: false })
7171
const depsPaths = await Promise.all(
72-
dependencies.map((dependency) =>
73-
getImportDependencies({ dependency, basedir, packageJson, state, treeShakeNext, pluginsModulesPath }),
74-
),
72+
dependencies
73+
.filter(Boolean)
74+
.map((dependency) =>
75+
getImportDependencies({ dependency, basedir, packageJson, state, treeShakeNext, pluginsModulesPath }),
76+
),
7577
)
7678
// TODO: switch to Array.flat() once we drop support for Node.js < 11.0.0
7779
// eslint-disable-next-line unicorn/prefer-spread
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"parserOptions": { "sourceType": "module" },
3+
"rules": {
4+
"node/no-unsupported-features/es-syntax": 0
5+
}
6+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export const handler = async function () {
2+
const variable = 'test'
3+
await import(variable)
4+
}

tests/main.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,14 @@ testBundlers('Ignore invalid require()', [ESBUILD, ESBUILD_ZISI, DEFAULT], async
181181
await zipNode(t, 'invalid-require', { opts: { jsBundler: bundler } })
182182
})
183183

184+
testBundlers('Can use dynamic import() with esbuild', [ESBUILD, ESBUILD_ZISI], async (bundler, t) => {
185+
await zipNode(t, 'dynamic-import', { opts: { jsBundler: bundler } })
186+
})
187+
188+
testBundlers('Bundling does not crash with dynamic import() with zisi', [DEFAULT], async (bundler, t) => {
189+
await t.throwsAsync(zipNode(t, 'dynamic-import', { opts: { jsBundler: bundler } }), /export/)
190+
})
191+
184192
testBundlers('Can require local files', [ESBUILD, ESBUILD_ZISI, DEFAULT], async (bundler, t) => {
185193
await zipNode(t, 'local-require', { opts: { jsBundler: bundler } })
186194
})

0 commit comments

Comments
 (0)