From 5953e2bc19e23a0edc4ad90032be5941e936a1b7 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Wed, 17 Jul 2024 16:00:36 +0800 Subject: [PATCH] fix(register): skip load files in node_modules --- packages/integrate-module/package.json | 1 + packages/integrate-module/src/index.ts | 7 +++++++ packages/register/esm.mts | 19 ++++++++++++++----- pnpm-lock.yaml | 9 +++++++++ 4 files changed, 31 insertions(+), 5 deletions(-) diff --git a/packages/integrate-module/package.json b/packages/integrate-module/package.json index 4f1cacda7..9a7a5dbbf 100644 --- a/packages/integrate-module/package.json +++ b/packages/integrate-module/package.json @@ -15,6 +15,7 @@ "@types/react-dom": "^18.3.0", "esmock": "^2.6.6", "ipaddr.js": "^2.2.0", + "postgres": "^3.4.4", "react": "^18.3.1", "react-dom": "^18.3.1", "simple-git": "^3.25.0", diff --git a/packages/integrate-module/src/index.ts b/packages/integrate-module/src/index.ts index bedd63e64..8b5fafee2 100644 --- a/packages/integrate-module/src/index.ts +++ b/packages/integrate-module/src/index.ts @@ -9,6 +9,7 @@ import { supportedExtensions } from 'file-type' import { renderToString } from 'react-dom/server' import { simpleGit } from 'simple-git' import ipaddr from 'ipaddr.js' +import postgres from 'postgres' import { CompiledClass } from './compiled.js' import cjs from './cjs' @@ -90,3 +91,9 @@ await test('esmock should work', async () => { assert.strictEqual(main.pathbasenamewrap(), 'hello') }) + +await test('postgres should work', async () => { + postgres({ + host: 'postgres://localhost', + }) +}) diff --git a/packages/register/esm.mts b/packages/register/esm.mts index d1527c28e..05692c6d9 100644 --- a/packages/register/esm.mts +++ b/packages/register/esm.mts @@ -215,11 +215,7 @@ export const resolve: ResolveHook = async (specifier, context, nextResolve) => { } // local project file - if ( - path && - ((process.platform !== 'win32' && !path.includes('/node_modules/')) || - (process.platform === 'win32' && !path.includes('\\node_modules\\'))) - ) { + if (path && isPathNotInNodeModules(path)) { debug('resolved: typescript', specifier, path) const url = new URL(join('file://', path)) return addShortCircuitSignal({ @@ -272,6 +268,12 @@ export const load: LoadHook = async (url, context, nextLoad) => { return nextLoad(url, context) } + if (url.includes('/node_modules/')) { + debug('skip load: node_modules', url) + + return nextLoad(url, context) + } + if (['builtin', 'json', 'wasm'].includes(context.format)) { debug('loaded: internal format', url) return nextLoad(url, context) @@ -300,3 +302,10 @@ export const load: LoadHook = async (url, context, nextLoad) => { source: compiled, }) } + +function isPathNotInNodeModules(path: string) { + return ( + (process.platform !== 'win32' && !path.includes('/node_modules/')) || + (process.platform === 'win32' && !path.includes('\\node_modules\\')) + ) +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 12770d606..fe17717e8 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -171,6 +171,9 @@ importers: ipaddr.js: specifier: ^2.2.0 version: 2.2.0 + postgres: + specifier: ^3.4.4 + version: 3.4.4 react: specifier: ^18.3.1 version: 18.3.1 @@ -4166,6 +4169,10 @@ packages: resolution: {integrity: sha512-VP/72JeXqak2KiOzjgKtQen5y3IZHn+9GOuLDafPv0eXa47xq0At93XahYBs26MsifCQ4enGKwbjBTKgb9QJXg==} engines: {node: ^12.20.0 || ^14.13.1 || >=16.0.0} + postgres@3.4.4: + resolution: {integrity: sha512-IbyN+9KslkqcXa8AO9fxpk97PA4pzewvpi2B3Dwy9u4zpV32QicaEdgmF3eSQUzdRk7ttDHQejNgAEr4XoeH4A==} + engines: {node: '>=12'} + prettier@3.3.2: resolution: {integrity: sha512-rAVeHYMcv8ATV5d508CFdn+8/pHPpXeIid1DdrPwXnaAdH7cqjVbpJaT5eq4yRAFU/lsbwYwSF/n5iNrdJHPQA==} engines: {node: '>=14'} @@ -9627,6 +9634,8 @@ snapshots: dependencies: irregular-plurals: 3.5.0 + postgres@3.4.4: {} + prettier@3.3.2: {} pretty-format@29.7.0: