From a4a68320ef03385fab8d1f0b32203ad14c887590 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Fri, 5 Jul 2024 15:46:52 +0800 Subject: [PATCH] fix(register): resolve builtin modules without node: protocal (#803) * fix(register): resolve builtin modules without node: protocal * skip snapshots test on Windows --- packages/integrate-module/package.json | 2 + packages/integrate-module/src/index.ts | 10 ++ .../__snapshots__/sourcemaps.spec.ts.md | 2 +- .../__snapshots__/sourcemaps.spec.ts.snap | Bin 408 -> 408 bytes .../__tests__/sourcemaps/sourcemaps.spec.ts | 3 + packages/register/esm.mts | 69 +++++++- pnpm-lock.yaml | 148 ++++++++++++++++++ 7 files changed, 230 insertions(+), 4 deletions(-) diff --git a/packages/integrate-module/package.json b/packages/integrate-module/package.json index b28b38773..3929d6ed3 100644 --- a/packages/integrate-module/package.json +++ b/packages/integrate-module/package.json @@ -8,12 +8,14 @@ "p-timeout": "^6.1.2" }, "devDependencies": { + "@napi-rs/simple-git": "^0.1.16", "@swc/core": "^1.6.6", "@swc-node/register": "workspace:*", "@types/react": "^18.3.3", "@types/react-dom": "^18.3.0", "react": "^18.3.1", "react-dom": "^18.3.1", + "simple-git": "^3.25.0", "typescript": "^5.5.3" } } diff --git a/packages/integrate-module/src/index.ts b/packages/integrate-module/src/index.ts index 5b7f3729d..270025db0 100644 --- a/packages/integrate-module/src/index.ts +++ b/packages/integrate-module/src/index.ts @@ -2,9 +2,11 @@ import assert from 'node:assert' import test from 'node:test' +import { RepositoryState } from '@napi-rs/simple-git' import { bar as subBar } from '@subdirectory/bar.mjs' import { supportedExtensions } from 'file-type' import { renderToString } from 'react-dom/server' +import { simpleGit } from 'simple-git' import { CompiledClass } from './compiled.js' import { foo } from './foo.mjs' @@ -47,3 +49,11 @@ await test('compiled js file with .d.ts', () => { await test('jsx should work', () => { assert.equal(renderToString(Component()), '
Component
') }) + +await test('resolve @napi-rs projects', () => { + assert.equal(RepositoryState.ApplyMailbox, 10) +}) + +await test('resolve simple-git', () => { + assert.ok(simpleGit) +}) diff --git a/packages/integrate/__tests__/sourcemaps/__snapshots__/sourcemaps.spec.ts.md b/packages/integrate/__tests__/sourcemaps/__snapshots__/sourcemaps.spec.ts.md index d0f52d284..14c8ba15b 100644 --- a/packages/integrate/__tests__/sourcemaps/__snapshots__/sourcemaps.spec.ts.md +++ b/packages/integrate/__tests__/sourcemaps/__snapshots__/sourcemaps.spec.ts.md @@ -9,7 +9,7 @@ Generated by [AVA](https://avajs.dev). > Snapshot 1 `Error: ␊ - at /packages/integrate/__tests__/sourcemaps/sourcemaps.spec.ts:15:26␊ + at /packages/integrate/__tests__/sourcemaps/sourcemaps.spec.ts:18:26␊ at Test.callFn (file:///node_modules/.pnpm/ava@6.1.3_encoding@0.1.13/node_modules/ava/lib/test.js:525:26)␊ at Test.run (file:///node_modules/.pnpm/ava@6.1.3_encoding@0.1.13/node_modules/ava/lib/test.js:534:33)␊ at Runner.runSingle (file:///node_modules/.pnpm/ava@6.1.3_encoding@0.1.13/node_modules/ava/lib/runner.js:281:33)␊ diff --git a/packages/integrate/__tests__/sourcemaps/__snapshots__/sourcemaps.spec.ts.snap b/packages/integrate/__tests__/sourcemaps/__snapshots__/sourcemaps.spec.ts.snap index e4ef9e35671dc9a7c35878da1a2571acf52cd085..a77c80924eaf5cec9574ca407291c6cff2a3802f 100644 GIT binary patch delta 366 zcmV-!0g?Wg1DFFoK~_N^Q*L2!b7*gLAa*kf0{~RoHsk!%*l)wroyZpztBoK)!(S@k z9^QC!7tyH77{?!r2mk;800003wUUuOHGl3-Z#$i6D@)wGgI8X|`U2;+)&*HvSCWv6L!3gN|5_qp3S?KE|C(m6lA@7_3P9Vcfbl<@L!M;cM&s}$l0xfSLwZxCfm2G2Eg)aoqSgTrRez>h zQt>KASrkVs2P18v%xp9W&zK#GgN$l5p-m^z0(qLG0oFgDxw;(`yX1`RP!F{+;2PLt zaM19dNcZU-pvf@)8`@0T4w7Yzv%wBoIV%BitU}nXP4NH=kX~7Xq}LenCLhiSO}~WH z3oP@O8frj(mGX4wB=F5ha_8()Atyv35~Qb00c)~{wJ}ecggnN-Hi%xhmiCvjjK^vJ M1CWi4^q~U)02IEeBLDyZ delta 366 zcmV-!0g?Wg1DFFoK~_N^Q*L2!b7*gLAa*kf0|0wk&}$LkFLQS{GzxT}f8T z&k5SvNmz;s6`HWlt;fx_D1;YJ-RExSwA0klN$33dzI)@Gb)1}$P{I>h30aC6AXUc0 z%n1)P&pn{WJg0l-AKM5kkda3or##8NjmF_hB!$+uhV-UN1E-W4TR^_FMXduMs((zi zq~cYKvM7#N4o2ESnb~L%jxjqF2N~6BLYtRF3*>3i!1@O?SGR*=mz=R3>Y+9UTmyRy z4jTRw={~&!G#SQ!Lz_<9L9&c-HrOF6XC)wxRS4U)DIQ<}(kp9_^co}H6egt zfo1+uLk-BUQl9Rd1il$b?wnmJgeNFOg7lOrU`_V0Hu_1EAy4986GSguOY=)v7QR6L M18>btD4_!Y0D(QD`Tzg` diff --git a/packages/integrate/__tests__/sourcemaps/sourcemaps.spec.ts b/packages/integrate/__tests__/sourcemaps/sourcemaps.spec.ts index 55d28051d..a144e3381 100644 --- a/packages/integrate/__tests__/sourcemaps/sourcemaps.spec.ts +++ b/packages/integrate/__tests__/sourcemaps/sourcemaps.spec.ts @@ -22,6 +22,9 @@ interface _Unused8 {} interface _Unused9 {} test('should work with sourcemaps', (t) => { + if (process.platform === 'win32') { + return t.pass('Skip on Windows') + } const projectRoot = join(__dirname, '..', '..', '..', '..') t.snapshot( new Error().stack diff --git a/packages/register/esm.mts b/packages/register/esm.mts index dbf95da6e..8826dadb7 100644 --- a/packages/register/esm.mts +++ b/packages/register/esm.mts @@ -14,6 +14,62 @@ import { AVAILABLE_TS_EXTENSION_PATTERN, compile } from '../lib/register.js' const debug = debugFactory('@swc-node') +const builtin = new Set([ + 'assert', + 'assert/strict', + 'async_hooks', + 'buffer', + 'child_process', + 'cluster', + 'console', + 'constants', + 'crypto', + 'dgram', + 'diagnostics_channel', + 'dns', + 'dns/promises', + 'domain', + 'events', + 'fs', + 'fs/promises', + 'http', + 'http2', + 'https', + 'inspector', + 'inspector/promises', + 'module', + 'net', + 'os', + 'path', + 'path/posix', + 'path/win32', + 'perf_hooks', + 'process', + 'punycode', + 'querystring', + 'readline', + 'readline/promises', + 'repl', + 'stream', + 'stream/consumers', + 'stream/promises', + 'stream/web', + 'string_decoder', + 'timers', + 'timers/promises', + 'tls', + 'trace_events', + 'tty', + 'url', + 'util', + 'util/types', + 'v8', + 'vm', + 'wasi', + 'worker_threads', + 'zlib', +]) + const tsconfig: ts.CompilerOptions = readDefaultTsConfig() tsconfig.module = ts.ModuleKind.ESNext @@ -123,8 +179,6 @@ export const getPackageType = async (url: string) => { return packageJson?.type ?? undefined } -const INTERNAL_MODULE_PATTERN = /^(node|nodejs):/ - const EXTENSION_MODULE_MAP = { '.mjs': 'module', '.cjs': 'commonjs', @@ -139,7 +193,7 @@ const EXTENSION_MODULE_MAP = { export const resolve: ResolveHook = async (specifier, context, nextResolve) => { debug('resolve', specifier, JSON.stringify(context)) - if (INTERNAL_MODULE_PATTERN.test(specifier)) { + if (specifier.startsWith('node:') || specifier.startsWith('nodejs:')) { debug('skip resolve: internal format', specifier) return addShortCircuitSignal({ @@ -148,6 +202,15 @@ export const resolve: ResolveHook = async (specifier, context, nextResolve) => { }) } + if (builtin.has(specifier)) { + debug('skip resolve: internal format', specifier) + + return addShortCircuitSignal({ + url: `node:${specifier}`, + format: 'builtin', + }) + } + if (specifier.startsWith('data:')) { debug('skip resolve: data url', specifier) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ba89afad6..88b00a247 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -150,6 +150,9 @@ importers: specifier: ^6.1.2 version: 6.1.2 devDependencies: + '@napi-rs/simple-git': + specifier: ^0.1.16 + version: 0.1.16 '@swc-node/register': specifier: workspace:* version: link:../register @@ -168,6 +171,9 @@ importers: react-dom: specifier: ^18.3.1 version: 18.3.1(react@18.3.1) + simple-git: + specifier: ^3.25.0 + version: 3.25.0 typescript: specifier: ^5.5.3 version: 5.5.3 @@ -1192,6 +1198,12 @@ packages: '@jridgewell/trace-mapping@0.3.25': resolution: {integrity: sha512-vNk6aEwybGtawWmy/PzwnGDOjCkLWSD2wqvjGGAgOAwCGWySYXfYoxt00IJkTF+8Lb57DwOb3Aa0o9CApepiYQ==} + '@kwsites/file-exists@1.1.1': + resolution: {integrity: sha512-m9/5YGR18lIwxSFDwfE3oA7bWuq9kdau6ugN4H2rJeyhFQZcG9AgSHkQtSD15a8WvTgfz9aikZMrKPHvbpqFiw==} + + '@kwsites/promise-deferred@1.1.1': + resolution: {integrity: sha512-GaHYm+c0O9MjZRu0ongGBRbinu8gVAMd2UZjji6jVmqKtZluZnptXGWhz1E8j8D2HJ3f/yMxKAUC0b+57wncIw==} + '@lerna/create@8.1.4': resolution: {integrity: sha512-lcfDXrDIESnpATGwIMCy0b/PcIVbha8q0d1WbXixFox+m2eno5MNHoteddUjhFY0CN1xM2259a8TyznCoY8GHw==} engines: {node: '>=18.0.0'} @@ -1200,6 +1212,76 @@ packages: resolution: {integrity: sha512-Yhlar6v9WQgUp/He7BdgzOz8lqMQ8sU+jkCq7Wx8Myc5YFJLbEe7lgui/V7G1qB1DJykHSGwreceSaD60Y0PUQ==} hasBin: true + '@napi-rs/simple-git-android-arm-eabi@0.1.16': + resolution: {integrity: sha512-dbrCL0Pl5KZG7x7tXdtVsA5CO6At5ohDX3myf5xIYn9kN4jDFxsocl8bNt6Vb/hZQoJd8fI+k5VlJt+rFhbdVw==} + engines: {node: '>= 10'} + cpu: [arm] + os: [android] + + '@napi-rs/simple-git-android-arm64@0.1.16': + resolution: {integrity: sha512-xYz+TW5J09iK8SuTAKK2D5MMIsBUXVSs8nYp7HcMi8q6FCRO7yJj96YfP9PvKsc/k64hOyqGmL5DhCzY9Cu1FQ==} + engines: {node: '>= 10'} + cpu: [arm64] + os: [android] + + '@napi-rs/simple-git-darwin-arm64@0.1.16': + resolution: {integrity: sha512-XfgsYqxhUE022MJobeiX563TJqyQyX4FmYCnqrtJwAfivESVeAJiH6bQIum8dDEYMHXCsG7nL8Ok0Dp8k2m42g==} + engines: {node: '>= 10'} + cpu: [arm64] + os: [darwin] + + '@napi-rs/simple-git-darwin-x64@0.1.16': + resolution: {integrity: sha512-tkEVBhD6vgRCbeWsaAQqM3bTfpIVGeitamPPRVSbsq8qgzJ5Dx6ZedH27R7KSsA/uao7mZ3dsrNLXbu1Wy5MzA==} + engines: {node: '>= 10'} + cpu: [x64] + os: [darwin] + + '@napi-rs/simple-git-linux-arm-gnueabihf@0.1.16': + resolution: {integrity: sha512-R6VAyNnp/yRaT7DV1Ao3r67SqTWDa+fNq2LrNy0Z8gXk2wB9ZKlrxFtLPE1WSpWknWtyRDLpRlsorh7Evk7+7w==} + engines: {node: '>= 10'} + cpu: [arm] + os: [linux] + + '@napi-rs/simple-git-linux-arm64-gnu@0.1.16': + resolution: {integrity: sha512-LAGI0opFKw/HBMCV2qIBK3uWSEW9h4xd2ireZKLJy8DBPymX6NrWIamuxYNyCuACnFdPRxR4LaRFy4J5ZwuMdw==} + engines: {node: '>= 10'} + cpu: [arm64] + os: [linux] + + '@napi-rs/simple-git-linux-arm64-musl@0.1.16': + resolution: {integrity: sha512-I57Ph0F0Yn2KW93ep+V1EzKhACqX0x49vvSiapqIsdDA2PifdEWLc1LJarBolmK7NKoPqKmf6lAKKO9lhiZzkg==} + engines: {node: '>= 10'} + cpu: [arm64] + os: [linux] + + '@napi-rs/simple-git-linux-x64-gnu@0.1.16': + resolution: {integrity: sha512-AZYYFY2V7hlcQASPEOWyOa3e1skzTct9QPzz0LiDM3f/hCFY/wBaU2M6NC5iG3d2Kr38heuyFS/+JqxLm5WaKA==} + engines: {node: '>= 10'} + cpu: [x64] + os: [linux] + + '@napi-rs/simple-git-linux-x64-musl@0.1.16': + resolution: {integrity: sha512-9TyMcYSBJwjT8jwjY9m24BZbu7ozyWTjsmYBYNtK3B0Um1Ov6jthSNneLVvouQ6x+k3Ow+00TiFh6bvmT00r8g==} + engines: {node: '>= 10'} + cpu: [x64] + os: [linux] + + '@napi-rs/simple-git-win32-arm64-msvc@0.1.16': + resolution: {integrity: sha512-uslJ1WuAHCYJWui6xjsyT47SjX6KOHDtClmNO8hqKz1pmDSNY7AjyUY8HxvD1lK9bDnWwc4JYhikS9cxCqHybw==} + engines: {node: '>= 10'} + cpu: [arm64] + os: [win32] + + '@napi-rs/simple-git-win32-x64-msvc@0.1.16': + resolution: {integrity: sha512-SoEaVeCZCDF1MP+M9bMSXsZWgEjk4On9GWADO5JOulvzR1bKjk0s9PMHwe/YztR9F0sJzrCxwtvBZowhSJsQPg==} + engines: {node: '>= 10'} + cpu: [x64] + os: [win32] + + '@napi-rs/simple-git@0.1.16': + resolution: {integrity: sha512-C5wRPw9waqL2jk3jEDeJv+f7ScuO3N0a39HVdyFLkwKxHH4Sya4ZbzZsu2JLi6eEqe7RuHipHL6mC7B2OfYZZw==} + engines: {node: '>= 10'} + '@napi-rs/wasm-runtime@0.2.3': resolution: {integrity: sha512-e4qmGDzXu2MYjj/XiKSgJ7XS7Z83MYVRN1yYaYXeQNVEO56zmshqmzFaELfdb612sLq/GmiPfRIwSji+bIlyCw==} @@ -4349,6 +4431,9 @@ packages: resolution: {integrity: sha512-fcU9clHwEss2/M/11FFM8Jwc4PjBgbhXoNskoK5guoK0qGQBSeUbQZRJ+B2fDFIvhyf0gqCaPrel9mszbhAxug==} engines: {node: ^16.14.0 || >=18.0.0} + simple-git@3.25.0: + resolution: {integrity: sha512-KIY5sBnzc4yEcJXW7Tdv4viEz8KyG+nU0hay+DWZasvdFOYKeUZ6Xc25LUHHjw0tinPT7O1eY6pzX7pRT1K8rw==} + sinon@18.0.0: resolution: {integrity: sha512-+dXDXzD1sBO6HlmZDd7mXZCR/y5ECiEiGCBSGuFD/kZ0bDTofPYc6JaeGmPSF+1j1MejGUWkORbYOLDyvqCWpA==} @@ -6043,6 +6128,14 @@ snapshots: '@jridgewell/resolve-uri': 3.1.0 '@jridgewell/sourcemap-codec': 1.4.15 + '@kwsites/file-exists@1.1.1': + dependencies: + debug: 4.3.5 + transitivePeerDependencies: + - supports-color + + '@kwsites/promise-deferred@1.1.1': {} + '@lerna/create@8.1.4(@swc-node/register@packages+register)(@swc/core@1.6.6)(encoding@0.1.13)': dependencies: '@npmcli/run-script': 7.0.2 @@ -6132,6 +6225,53 @@ snapshots: - encoding - supports-color + '@napi-rs/simple-git-android-arm-eabi@0.1.16': + optional: true + + '@napi-rs/simple-git-android-arm64@0.1.16': + optional: true + + '@napi-rs/simple-git-darwin-arm64@0.1.16': + optional: true + + '@napi-rs/simple-git-darwin-x64@0.1.16': + optional: true + + '@napi-rs/simple-git-linux-arm-gnueabihf@0.1.16': + optional: true + + '@napi-rs/simple-git-linux-arm64-gnu@0.1.16': + optional: true + + '@napi-rs/simple-git-linux-arm64-musl@0.1.16': + optional: true + + '@napi-rs/simple-git-linux-x64-gnu@0.1.16': + optional: true + + '@napi-rs/simple-git-linux-x64-musl@0.1.16': + optional: true + + '@napi-rs/simple-git-win32-arm64-msvc@0.1.16': + optional: true + + '@napi-rs/simple-git-win32-x64-msvc@0.1.16': + optional: true + + '@napi-rs/simple-git@0.1.16': + optionalDependencies: + '@napi-rs/simple-git-android-arm-eabi': 0.1.16 + '@napi-rs/simple-git-android-arm64': 0.1.16 + '@napi-rs/simple-git-darwin-arm64': 0.1.16 + '@napi-rs/simple-git-darwin-x64': 0.1.16 + '@napi-rs/simple-git-linux-arm-gnueabihf': 0.1.16 + '@napi-rs/simple-git-linux-arm64-gnu': 0.1.16 + '@napi-rs/simple-git-linux-arm64-musl': 0.1.16 + '@napi-rs/simple-git-linux-x64-gnu': 0.1.16 + '@napi-rs/simple-git-linux-x64-musl': 0.1.16 + '@napi-rs/simple-git-win32-arm64-msvc': 0.1.16 + '@napi-rs/simple-git-win32-x64-msvc': 0.1.16 + '@napi-rs/wasm-runtime@0.2.3': dependencies: '@emnapi/core': 1.1.1 @@ -9746,6 +9886,14 @@ snapshots: transitivePeerDependencies: - supports-color + simple-git@3.25.0: + dependencies: + '@kwsites/file-exists': 1.1.1 + '@kwsites/promise-deferred': 1.1.1 + debug: 4.3.5 + transitivePeerDependencies: + - supports-color + sinon@18.0.0: dependencies: '@sinonjs/commons': 3.0.1