diff --git a/CHANGELOG.md b/CHANGELOG.md index c8fc3185..2bea0772 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,22 @@ All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. +## v2.3.2 + +[compare changes](https://github.com/unjs/jiti/compare/v2.3.1...v2.3.2) + +### 🩹 Fixes + +- **eval:** Fallback in async mode ([#325](https://github.com/unjs/jiti/pull/325)) + +### 🏡 Chore + +- Update lockfile ([3627a56](https://github.com/unjs/jiti/commit/3627a56)) + +### ❤️ Contributors + +- Pooya Parsa ([@pi0](http://github.com/pi0)) + ## v2.3.1 [compare changes](https://github.com/unjs/jiti/compare/v2.3.0...v2.3.1) diff --git a/lib/types.d.ts b/lib/types.d.ts index 9e0789ef..e5cf6f53 100644 --- a/lib/types.d.ts +++ b/lib/types.d.ts @@ -223,6 +223,7 @@ export type EvalModuleOptions = Partial<{ ext: string; cache: ModuleCache; async: boolean; + forceTranspile: boolean; }>; export interface TransformOptions { diff --git a/package.json b/package.json index 72f77eba..c0f9181e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "jiti", - "version": "2.3.1", + "version": "2.3.2", "description": "Runtime typescript and ESM support for Node.js", "repository": "unjs/jiti", "license": "MIT", @@ -103,8 +103,8 @@ "etag": "^1.8.1", "fast-glob": "^3.3.2", "mime": "^4.0.4", - "mlly": "^1.7.1", - "moment-timezone": "^0.5.45", + "mlly": "^1.7.2", + "moment-timezone": "^0.5.46", "nano-jsx": "^0.1.0", "pathe": "^1.1.2", "pkg-types": "^1.2.0", @@ -129,5 +129,5 @@ "yoctocolors": "^2.1.1", "zod": "^3.23.8" }, - "packageManager": "pnpm@9.11.0" + "packageManager": "pnpm@9.12.0" } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 34ec8d94..9a218fe5 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -120,11 +120,11 @@ importers: specifier: ^4.0.4 version: 4.0.4 mlly: - specifier: ^1.7.1 - version: 1.7.1 + specifier: ^1.7.2 + version: 1.7.2 moment-timezone: - specifier: ^0.5.45 - version: 0.5.45 + specifier: ^0.5.46 + version: 0.5.46 nano-jsx: specifier: ^0.1.0 version: 0.1.0 @@ -160,13 +160,13 @@ importers: version: 3.7.0 terser-webpack-plugin: specifier: ^5.3.10 - version: 5.3.10(webpack@5.95.0(webpack-cli@5.1.4)) + version: 5.3.10(webpack@5.95.0) tinyexec: specifier: ^0.3.0 version: 0.3.0 ts-loader: specifier: ^9.5.1 - version: 9.5.1(typescript@5.6.2)(webpack@5.95.0(webpack-cli@5.1.4)) + version: 9.5.1(typescript@5.6.2)(webpack@5.95.0) typescript: specifier: ^5.6.2 version: 5.6.2 @@ -187,7 +187,7 @@ importers: version: 5.1.4(webpack-bundle-analyzer@4.10.2)(webpack@5.95.0) webpack-license-plugin: specifier: ^4.5.0 - version: 4.5.0(webpack@5.95.0(webpack-cli@5.1.4)) + version: 4.5.0(webpack@5.95.0) yoctocolors: specifier: ^2.1.1 version: 2.1.1 @@ -1137,8 +1137,8 @@ packages: concat-map@0.0.1: resolution: {integrity: sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==} - confbox@0.1.7: - resolution: {integrity: sha512-uJcB/FKZtBMCJpK8MQji6bJHgu1tixKPxRLeGkNzBoOZzpnZUJm0jm2/sBDWcuBx1dYgxV4JU+g5hmNxCyAmdA==} + confbox@0.1.8: + resolution: {integrity: sha512-RMtmw0iFkeR4YV+fUOSucriAQNb9g8zFR52MWCtl+cCZOFRNL6zeB395vPzFhEjjn4fMxXudmELnl/KF/WrK6w==} config@3.3.12: resolution: {integrity: sha512-Vmx389R/QVM3foxqBzXO8t2tUikYZP64Q6vQxGrsMpREeJc/aWRnPRERXWsYzOHAumx/AOoILWe6nU3ZJL+6Sw==} @@ -1413,9 +1413,6 @@ packages: resolution: {integrity: sha512-3hN7NaskYvMDLQY55gnW3NQ+mesEAepTqlg+VEbj7zzqEMBVNhzcGYYeqFo/TlYz6eQiFcp1HcsCZO+nGgS8zg==} engines: {node: '>=6.9.0'} - get-func-name@2.0.2: - resolution: {integrity: sha512-8vXOvuE167CtIc3OyItco7N/dpRtBbYOsPsXCz7X/PMnlGjYjSGuZJgM1Y7mmew7BKf9BqvLX2tnOVy1BBUsxQ==} - get-stream@8.0.1: resolution: {integrity: sha512-VaUJspBffn/LMCJVoMvSAdmscJyS1auj5Zulnn5UoYcY531UWmdwhRWkcGKnGU93m5HSXP9LP2usOryrBtQowA==} engines: {node: '>=16'} @@ -1680,8 +1677,8 @@ packages: resolution: {integrity: sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q==} hasBin: true - loupe@3.1.1: - resolution: {integrity: sha512-edNu/8D5MKVfGVFRhFf8aAxiTM6Wumfz5XsaatSxlD3w4R1d/WEKUTydCdPGbl9K7QG/Ca3GnDV2sIKIpXRQcw==} + loupe@3.1.2: + resolution: {integrity: sha512-23I4pFZHmAemUnz8WZXbYRSKYj801VDaNv9ETuMh7IrMc7VuVVSo+Z9iLE3ni30+U48iDWfi30d3twAXBYmnCg==} lru-cache@10.4.3: resolution: {integrity: sha512-JNAzZcXrCt42VGLuYz0zfAzDfAvJWW6AfYlDBQyDV5DClI2m5sAmK+OIO7s59XfsRsWHp02jAJrRadPRGTt6SQ==} @@ -1768,11 +1765,11 @@ packages: engines: {node: '>=10'} hasBin: true - mlly@1.7.1: - resolution: {integrity: sha512-rrVRZRELyQzrIUAVMHxP97kv+G786pHmOKzuFII8zDYahFBS7qnHh2AlYSl1GAHhaMPCz6/oHjVMcfFYgFYHgA==} + mlly@1.7.2: + resolution: {integrity: sha512-tN3dvVHYVz4DhSXinXIk7u9syPYaJvio118uomkovAtWBT+RdbP6Lfh/5Lvo519YMmwBafwlh20IPTXIStscpA==} - moment-timezone@0.5.45: - resolution: {integrity: sha512-HIWmqA86KcmCAhnMAN0wuDOARV/525R2+lOLotuGFzn4HO+FH+/645z2wx0Dt3iDv6/p61SIvKnDstISainhLQ==} + moment-timezone@0.5.46: + resolution: {integrity: sha512-ZXm9b36esbe7OmdABqIWJuBBiLLwAjrN7CE+7sYdCCx82Nabt1wHDj8TVseS59QIlfFPbOoiBPm6ca9BioG4hw==} moment@2.30.1: resolution: {integrity: sha512-uEmtNhbDOrWPFS+hdjFCBfy9f2YoyzRpwcl+DqpC6taX21FzsTLQVbMV/W7PzNSX6x/bhC1zA3c2UQ5NzH6how==} @@ -3145,7 +3142,7 @@ snapshots: '@vitest/utils@2.1.2': dependencies: '@vitest/pretty-format': 2.1.2 - loupe: 3.1.1 + loupe: 3.1.2 tinyrainbow: 1.2.0 '@vue/compiler-core@3.5.11': @@ -3278,17 +3275,17 @@ snapshots: '@webassemblyjs/ast': 1.12.1 '@xtuc/long': 4.2.2 - '@webpack-cli/configtest@2.1.1(webpack-cli@5.1.4(webpack-bundle-analyzer@4.10.2)(webpack@5.95.0))(webpack@5.95.0(webpack-cli@5.1.4))': + '@webpack-cli/configtest@2.1.1(webpack-cli@5.1.4)(webpack@5.95.0)': dependencies: webpack: 5.95.0(webpack-cli@5.1.4) webpack-cli: 5.1.4(webpack-bundle-analyzer@4.10.2)(webpack@5.95.0) - '@webpack-cli/info@2.0.2(webpack-cli@5.1.4(webpack-bundle-analyzer@4.10.2)(webpack@5.95.0))(webpack@5.95.0(webpack-cli@5.1.4))': + '@webpack-cli/info@2.0.2(webpack-cli@5.1.4)(webpack@5.95.0)': dependencies: webpack: 5.95.0(webpack-cli@5.1.4) webpack-cli: 5.1.4(webpack-bundle-analyzer@4.10.2)(webpack@5.95.0) - '@webpack-cli/serve@2.0.5(webpack-cli@5.1.4(webpack-bundle-analyzer@4.10.2)(webpack@5.95.0))(webpack@5.95.0(webpack-cli@5.1.4))': + '@webpack-cli/serve@2.0.5(webpack-cli@5.1.4)(webpack@5.95.0)': dependencies: webpack: 5.95.0(webpack-cli@5.1.4) webpack-cli: 5.1.4(webpack-bundle-analyzer@4.10.2)(webpack@5.95.0) @@ -3382,12 +3379,12 @@ snapshots: c12@1.11.2(magicast@0.3.5): dependencies: chokidar: 3.6.0 - confbox: 0.1.7 + confbox: 0.1.8 defu: 6.1.4 dotenv: 16.4.5 giget: 1.2.3 jiti: 1.21.6 - mlly: 1.7.1 + mlly: 1.7.2 ohash: 1.1.4 pathe: 1.1.2 perfect-debounce: 1.0.0 @@ -3407,7 +3404,7 @@ snapshots: assertion-error: 2.0.1 check-error: 2.1.1 deep-eql: 5.0.2 - loupe: 3.1.1 + loupe: 3.1.2 pathval: 2.0.0 chalk@2.4.2: @@ -3504,7 +3501,7 @@ snapshots: concat-map@0.0.1: {} - confbox@0.1.7: {} + confbox@0.1.8: {} config@3.3.12: dependencies: @@ -3812,8 +3809,6 @@ snapshots: gensync@1.0.0-beta.2: {} - get-func-name@2.0.2: {} - get-stream@8.0.1: {} giget@1.2.3: @@ -4037,9 +4032,7 @@ snapshots: dependencies: js-tokens: 4.0.0 - loupe@3.1.1: - dependencies: - get-func-name: 2.0.2 + loupe@3.1.2: {} lru-cache@10.4.3: {} @@ -4124,14 +4117,14 @@ snapshots: mkdirp@1.0.4: {} - mlly@1.7.1: + mlly@1.7.2: dependencies: acorn: 8.12.1 pathe: 1.1.2 pkg-types: 1.2.0 ufo: 1.5.4 - moment-timezone@0.5.45: + moment-timezone@0.5.46: dependencies: moment: 2.30.1 @@ -4281,8 +4274,8 @@ snapshots: pkg-types@1.2.0: dependencies: - confbox: 0.1.7 - mlly: 1.7.1 + confbox: 0.1.8 + mlly: 1.7.2 pathe: 1.1.2 pluralize@8.0.0: {} @@ -4545,7 +4538,7 @@ snapshots: mkdirp: 1.0.4 yallist: 4.0.0 - terser-webpack-plugin@5.3.10(webpack@5.95.0(webpack-cli@5.1.4)): + terser-webpack-plugin@5.3.10(webpack@5.95.0): dependencies: '@jridgewell/trace-mapping': 0.3.25 jest-worker: 27.5.1 @@ -4591,7 +4584,7 @@ snapshots: dependencies: typescript: 5.6.2 - ts-loader@9.5.1(typescript@5.6.2)(webpack@5.95.0(webpack-cli@5.1.4)): + ts-loader@9.5.1(typescript@5.6.2)(webpack@5.95.0): dependencies: chalk: 4.1.2 enhanced-resolve: 5.17.1 @@ -4742,9 +4735,9 @@ snapshots: webpack-cli@5.1.4(webpack-bundle-analyzer@4.10.2)(webpack@5.95.0): dependencies: '@discoveryjs/json-ext': 0.5.7 - '@webpack-cli/configtest': 2.1.1(webpack-cli@5.1.4(webpack-bundle-analyzer@4.10.2)(webpack@5.95.0))(webpack@5.95.0(webpack-cli@5.1.4)) - '@webpack-cli/info': 2.0.2(webpack-cli@5.1.4(webpack-bundle-analyzer@4.10.2)(webpack@5.95.0))(webpack@5.95.0(webpack-cli@5.1.4)) - '@webpack-cli/serve': 2.0.5(webpack-cli@5.1.4(webpack-bundle-analyzer@4.10.2)(webpack@5.95.0))(webpack@5.95.0(webpack-cli@5.1.4)) + '@webpack-cli/configtest': 2.1.1(webpack-cli@5.1.4)(webpack@5.95.0) + '@webpack-cli/info': 2.0.2(webpack-cli@5.1.4)(webpack@5.95.0) + '@webpack-cli/serve': 2.0.5(webpack-cli@5.1.4)(webpack@5.95.0) colorette: 2.0.20 commander: 10.0.1 cross-spawn: 7.0.3 @@ -4758,7 +4751,7 @@ snapshots: optionalDependencies: webpack-bundle-analyzer: 4.10.2 - webpack-license-plugin@4.5.0(webpack@5.95.0(webpack-cli@5.1.4)): + webpack-license-plugin@4.5.0(webpack@5.95.0): dependencies: chalk: 5.3.0 lodash: 4.17.21 @@ -4797,7 +4790,7 @@ snapshots: neo-async: 2.6.2 schema-utils: 3.3.0 tapable: 2.2.1 - terser-webpack-plugin: 5.3.10(webpack@5.95.0(webpack-cli@5.1.4)) + terser-webpack-plugin: 5.3.10(webpack@5.95.0) watchpack: 2.4.2 webpack-sources: 3.2.3 optionalDependencies: diff --git a/src/eval.ts b/src/eval.ts index b058f533..4be48cf6 100644 --- a/src/eval.ts +++ b/src/eval.ts @@ -38,12 +38,11 @@ export function evalModule( (ext === ".js" && readNearestPackageJSON(filename)?.type === "module"); const isCommonJS = ext === ".cjs"; const needsTranspile = - !isCommonJS && // CommonJS skips transpile - !(isESM && evalOptions.async) && // In async mode, we can skip native ESM as well - (isTypescript || - isESM || - ctx.isTransformRe.test(filename) || - hasESMSyntax(source)); + evalOptions.forceTranspile ?? + (!isCommonJS && // CommonJS skips transpile + !(isESM && evalOptions.async) && // In async mode, we can skip native ESM as well + // prettier-ignore + (isTypescript || isESM || ctx.isTransformRe.test(filename) || hasESMSyntax(source))); const start = performance.now(); if (needsTranspile) { source = transform(ctx, { @@ -62,24 +61,35 @@ export function evalModule( `(${time}ms)`, ); } else { - try { - debug( - ctx, - "[native]", - evalOptions.async ? "[import]" : "[require]", - filename, - ); - return nativeImportOrRequire(ctx, filename, evalOptions.async); - } catch (error: any) { - debug(ctx, "Native require error:", error); - debug(ctx, "[fallback]", filename); - source = transform(ctx, { - filename, - source, - ts: isTypescript, - async: evalOptions.async ?? false, - jsx: ctx.opts.jsx, + debug( + ctx, + "[native]", + evalOptions.async ? "[import]" : "[require]", + filename, + ); + + if (evalOptions.async) { + return Promise.resolve( + nativeImportOrRequire(ctx, filename, evalOptions.async), + ).catch((error: any) => { + debug(ctx, "Native import error:", error); + debug(ctx, "[fallback]", filename); + evalModule(ctx, source, { ...evalOptions, forceTranspile: true }); }); + } else { + try { + return nativeImportOrRequire(ctx, filename, evalOptions.async); + } catch (error: any) { + debug(ctx, "Native require error:", error); + debug(ctx, "[fallback]", filename); + source = transform(ctx, { + filename, + source, + ts: isTypescript, + async: evalOptions.async ?? false, + jsx: ctx.opts.jsx, + }); + } } }