Skip to content

Commit 1c2e382

Browse files
authored
fix: use try-catch with dynamic imports so that they can be replaced with require during bundling (#262)
1 parent 4be6dcf commit 1c2e382

File tree

3 files changed

+16
-13
lines changed

3 files changed

+16
-13
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
- name: Checkout the repository
1515
uses: actions/checkout@v4
1616
- name: Install pnpm
17-
uses: pnpm/action-setup@v2
17+
uses: pnpm/action-setup@v3
1818
with:
1919
version: 9
2020
- name: Install Node.js

src/req.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ let tsx
88

99
let jiti
1010

11-
let importError
11+
let importError = []
1212

1313
/**
1414
* @param {string} name
@@ -28,9 +28,11 @@ async function req(name, rootFile = __filename) {
2828
}
2929

3030
if (tsx === undefined) {
31-
tsx = await import('tsx/cjs/api').catch(error => {
32-
importError = error
33-
})
31+
try {
32+
tsx = await import('tsx/cjs/api')
33+
} catch (error) {
34+
importError.push(error)
35+
}
3436
}
3537

3638
if (tsx) {
@@ -39,20 +41,21 @@ async function req(name, rootFile = __filename) {
3941
}
4042

4143
if (jiti === undefined) {
42-
jiti = await import('jiti').then(
43-
m => m.default,
44-
error => {
45-
importError = importError ?? error
46-
}
47-
)
44+
try {
45+
jiti = (await import('jiti')).default
46+
} catch (error) {
47+
importError.push(error)
48+
}
4849
}
4950

5051
if (jiti) {
5152
return jiti(rootFile, { interopDefault: true })(name)
5253
}
5354

5455
throw new Error(
55-
`'tsx' or 'jiti' is required for the TypeScript configuration files. Make sure it is installed\nError: ${importError.message}`
56+
`'tsx' or 'jiti' is required for the TypeScript configuration files. Make sure it is installed\nError: ${importError
57+
.map(error => error.message)
58+
.join('\n')}`
5659
)
5760
}
5861

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"compilerOptions": {
3-
"module": "Preserve",
3+
"module": "node16",
44
"esModuleInterop": true,
55
"allowJs": true,
66
"noEmit": true

0 commit comments

Comments
 (0)