Skip to content

Commit

Permalink
feat: use jiti v2, support fallback loaders, disable tsx detection in…
Browse files Browse the repository at this point in the history
… electron (#11)
  • Loading branch information
antfu authored Jul 29, 2024
1 parent ac6da09 commit b1b4122
Show file tree
Hide file tree
Showing 10 changed files with 432 additions and 354 deletions.
38 changes: 30 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ Use [`jiti`](https://github.com/unjs/jiti) to import the module. It uses a bundl

#### Limitations

- [Does not support top-level await yet](https://github.com/unjs/jiti/issues/72)
- ~~[Does not support top-level await yet](https://github.com/unjs/jiti/issues/72)~~ (supported in v2)
- Runs in CJS mode (transpiles all TS/ESM to CJS)

### `bundle-require`
Expand Down Expand Up @@ -215,20 +215,42 @@ const mod = await import('importx')
}))
```

## Fallback Loaders

Since v0.4, importx supports fallback loaders when previous loaders fail to load the module. By default `['jiti']` will be used as it's the most compatible loader. You can customize the fallback loaders by setting the `fallbackLoaders` option:

```ts
const mod = await import('importx')
.then(x => x.import('./path/to/module.ts', {
fallbackLoaders: ['jiti', 'tsx'],
parentURL: import.meta.url,
}))
```

You can also disable fallback loaders by setting it to `false`:

```ts
const mod = await import('importx')
.then(x => x.import('./path/to/module.ts', {
fallbackLoaders: false,
parentURL: import.meta.url,
}))
```

## Runtime-Loader Compatibility Table

Importing a TypeScript module with `importx`:

<!-- TABLE_START -->

> Generated with version `v0.3.7` at 2024-06-28T10:07:29.630Z
> Generated with version `v0.3.11` at 2024-07-29T13:55:23.094Z
| | native | tsx | jiti | bundle-require |
| ------- | --- | --- | --- | --- |
| node | Import: ❌<br>Cache: ❌<br>No cache: ❌<br>Deps: ❌<br>CTS Import: ❌<br>ESM/CJS Mixed: ❌<br>Const Enum: ❌<br>Import ESM Dep: ❌ | Import: ✅<br>Cache: ✅<br>No cache: ✅<br>Deps: ✅<br>CTS Import: ✅<br>ESM/CJS Mixed: ✅<br>Const Enum: ✅<br>Import ESM Dep: ✅ | Import: ✅<br>Cache: ✅<br>No cache: ✅<br>Deps: ✅<br>CTS Import: ✅<br>ESM/CJS Mixed: ✅<br>Const Enum: ✅<br>Import ESM Dep: | Import: ✅<br>Cache: ❌<br>No cache: ✅<br>Deps: ✅<br>CTS Import: ❌<br>ESM/CJS Mixed: ✅<br>Const Enum: ✅<br>Import ESM Dep: ✅ |
| tsx | Import: ✅<br>Cache: ✅<br>No cache: ❌<br>Deps: ❌<br>CTS Import: ✅<br>ESM/CJS Mixed: ✅<br>Const Enum: ✅<br>Import ESM Dep: ✅ | Import: ✅<br>Cache: ✅<br>No cache: ✅<br>Deps: ✅<br>CTS Import: ✅<br>ESM/CJS Mixed: ✅<br>Const Enum: ✅<br>Import ESM Dep: ✅ | Import: ✅<br>Cache: ✅<br>No cache: ✅<br>Deps: ✅<br>CTS Import: ✅<br>ESM/CJS Mixed: ✅<br>Const Enum: ✅<br>Import ESM Dep: | Import: ✅<br>Cache: ❌<br>No cache: ✅<br>Deps: ✅<br>CTS Import: ❌<br>ESM/CJS Mixed: ✅<br>Const Enum: ✅<br>Import ESM Dep: ✅ |
| deno | Import: ✅<br>Cache: ✅<br>No cache: ❌<br>Deps: ❌<br>CTS Import: ❌<br>ESM/CJS Mixed: ❌<br>Const Enum: ❌<br>Import ESM Dep: ✅ | Import: ❌<br>Cache: ❌<br>No cache: ❌<br>Deps: ❌<br>CTS Import: ❌<br>ESM/CJS Mixed: ❌<br>Const Enum: ❌<br>Import ESM Dep: ❌ | Import: <br>Cache: <br>No cache: <br>Deps: <br>CTS Import: <br>ESM/CJS Mixed: <br>Const Enum: <br>Import ESM Dep: ❌ | Import: ✅<br>Cache: ❌<br>No cache: ✅<br>Deps: ✅<br>CTS Import: ❌<br>ESM/CJS Mixed: ✅<br>Const Enum: ✅<br>Import ESM Dep: ✅ |
| bun | Import: ✅<br>Cache: ✅<br>No cache: ❌<br>Deps: ❌<br>CTS Import: ✅<br>ESM/CJS Mixed: ✅<br>Const Enum: ✅<br>Import ESM Dep: ✅ | Import: ❌<br>Cache: ❌<br>No cache: ❌<br>Deps: ❌<br>CTS Import: ❌<br>ESM/CJS Mixed: ❌<br>Const Enum: ❌<br>Import ESM Dep: ❌ | Import: ✅<br>Cache: <br>No cache: ❌<br>Deps: ✅<br>CTS Import: ✅<br>ESM/CJS Mixed: ✅<br>Const Enum: ✅<br>Import ESM Dep: | Import: ✅<br>Cache: ❌<br>No cache: ✅<br>Deps: ✅<br>CTS Import: ❌<br>ESM/CJS Mixed: ✅<br>Const Enum: ✅<br>Import ESM Dep: ✅ |
| node | Import: ❌<br>Cache: ❌<br>No cache: ❌<br>Deps: ❌<br>CTS Import: ❌<br>ESM/CJS Mixed: ❌<br>Const Enum: ❌<br>Import ESM Dep: ❌ | Import: ✅<br>Cache: ✅<br>No cache: ✅<br>Deps: ✅<br>CTS Import: ✅<br>ESM/CJS Mixed: ✅<br>Const Enum: ✅<br>Import ESM Dep: ✅ | Import: ✅<br>Cache: ✅<br>No cache: ✅<br>Deps: ✅<br>CTS Import: ✅<br>ESM/CJS Mixed: ✅<br>Const Enum: ✅<br>Import ESM Dep: | Import: ✅<br>Cache: ❌<br>No cache: ✅<br>Deps: ✅<br>CTS Import: ❌<br>ESM/CJS Mixed: ✅<br>Const Enum: ✅<br>Import ESM Dep: ✅ |
| tsx | Import: ✅<br>Cache: ✅<br>No cache: ❌<br>Deps: ❌<br>CTS Import: ✅<br>ESM/CJS Mixed: ✅<br>Const Enum: ✅<br>Import ESM Dep: ✅ | Import: ✅<br>Cache: ✅<br>No cache: ✅<br>Deps: ✅<br>CTS Import: ✅<br>ESM/CJS Mixed: ✅<br>Const Enum: ✅<br>Import ESM Dep: ✅ | Import: ✅<br>Cache: ✅<br>No cache: ✅<br>Deps: ✅<br>CTS Import: ✅<br>ESM/CJS Mixed: ✅<br>Const Enum: ✅<br>Import ESM Dep: | Import: ✅<br>Cache: ❌<br>No cache: ✅<br>Deps: ✅<br>CTS Import: ❌<br>ESM/CJS Mixed: ✅<br>Const Enum: ✅<br>Import ESM Dep: ✅ |
| deno | Import: ✅<br>Cache: ✅<br>No cache: ❌<br>Deps: ❌<br>CTS Import: ❌<br>ESM/CJS Mixed: ❌<br>Const Enum: ❌<br>Import ESM Dep: ✅ | Import: ❌<br>Cache: ❌<br>No cache: ❌<br>Deps: ❌<br>CTS Import: ❌<br>ESM/CJS Mixed: ❌<br>Const Enum: ❌<br>Import ESM Dep: ❌ | Import: <br>Cache: <br>No cache: <br>Deps: <br>CTS Import: <br>ESM/CJS Mixed: <br>Const Enum: <br>Import ESM Dep: ❌ | Import: ✅<br>Cache: ❌<br>No cache: ✅<br>Deps: ✅<br>CTS Import: ❌<br>ESM/CJS Mixed: ✅<br>Const Enum: ✅<br>Import ESM Dep: ✅ |
| bun | Import: ✅<br>Cache: ✅<br>No cache: ❌<br>Deps: ❌<br>CTS Import: ✅<br>ESM/CJS Mixed: ✅<br>Const Enum: ✅<br>Import ESM Dep: ✅ | Import: ❌<br>Cache: ❌<br>No cache: ❌<br>Deps: ❌<br>CTS Import: ❌<br>ESM/CJS Mixed: ❌<br>Const Enum: ❌<br>Import ESM Dep: ❌ | Import: ✅<br>Cache: <br>No cache: ❌<br>Deps: ✅<br>CTS Import: ✅<br>ESM/CJS Mixed: ✅<br>Const Enum: ✅<br>Import ESM Dep: | Import: ✅<br>Cache: ❌<br>No cache: ✅<br>Deps: ✅<br>CTS Import: ❌<br>ESM/CJS Mixed: ✅<br>Const Enum: ✅<br>Import ESM Dep: ✅ |

<!-- TABLE_END -->

Expand All @@ -240,8 +262,8 @@ Importing a TypeScript module with `importx`:
| Cache: `false` |||||
| List dependencies |||||
| Runtimes other than Node.js |||||
| Native ESM Import ||| ||
| Top-level await ||| ||
| Native ESM Import ||| ||
| Top-level await ||| ||
| Runtime module type* | ESM | ESM | CJS | ESM/CJS |

> *This indicates what's the module type for each loader to evaluate the modules. For `CJS`, it means the loader transpiles the module to CJS and executes it in CJS mode, which may have some limitations like top-level await.
Expand Down
15 changes: 9 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
},
"files": [
"dist",
"dummy.mts"
"runtime-fixtures"
],
"scripts": {
"build": "unbuild",
Expand All @@ -51,21 +51,21 @@
},
"dependencies": {
"bundle-require": "^5.0.0",
"debug": "^4.3.5",
"debug": "^4.3.6",
"esbuild": "^0.20.2",
"jiti": "^1.21.6",
"jiti": "2.0.0-beta.2",
"pathe": "^1.1.2",
"pkg-types": "^1.1.3",
"tsx": "^4.16.2"
},
"devDependencies": {
"@antfu/eslint-config": "^2.23.2",
"@antfu/eslint-config": "^2.24.0",
"@antfu/ni": "^0.22.0",
"@antfu/utils": "^0.7.10",
"@types/debug": "^4.1.12",
"@types/node": "^20.14.12",
"@types/node": "^22.0.0",
"bumpp": "^9.4.1",
"eslint": "^9.7.0",
"eslint": "^9.8.0",
"esno": "^4.7.0",
"execa": "^9.3.0",
"find-up": "^7.0.0",
Expand All @@ -80,6 +80,9 @@
"vite": "^5.3.5",
"vitest": "^2.0.4"
},
"resolutions": {
"jiti": "2.0.0-beta.2"
},
"simple-git-hooks": {
"pre-commit": "npx lint-staged"
},
Expand Down
Loading

0 comments on commit b1b4122

Please sign in to comment.