Skip to content

Commit

Permalink
fix(vite-node): externalize network imports (#4987)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa authored Jan 17, 2024
1 parent 6c5fe49 commit 21f5744
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 3 deletions.
1 change: 1 addition & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default antfu(
'test/core/src/self',
'test/workspaces/results.json',
'test/reporters/fixtures/with-syntax-error.test.js',
'test/network-imports/public/[email protected]',
'examples/**/mockServiceWorker.js',
],
},
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"test:run": "vitest run -r test/core",
"test:all": "CI=true pnpm -r --stream run test --allowOnly",
"test:ci": "CI=true pnpm -r --stream --filter !test-browser --filter !test-esm --filter !test-browser run test --allowOnly",
"test:ci:vm-threads": "CI=true pnpm -r --stream --filter !test-coverage --filter !test-single-thread --filter !test-browser --filter !test-esm --filter !test-browser --filter !example-react-testing-lib-msw run test --allowOnly --pool vmThreads",
"test:ci:vm-threads": "CI=true pnpm -r --stream --filter !test-coverage --filter !test-single-thread --filter !test-browser --filter !test-esm --filter !test-network-imports --filter !test-browser --filter !example-react-testing-lib-msw run test --allowOnly --pool vmThreads",
"test:ci:no-threads": "CI=true pnpm -r --stream --filter !test-vm-threads --filter !test-coverage --filter !test-watch --filter !test-bail --filter !test-esm --filter !test-browser run test --allowOnly --pool forks",
"typecheck": "tsc -p tsconfig.check.json --noEmit",
"typecheck:why": "tsc -p tsconfig.check.json --noEmit --explainFiles > explainTypes.txt",
Expand Down
5 changes: 3 additions & 2 deletions packages/vite-node/src/externalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,9 @@ async function _shouldExternalize(
return id

// data: should be processed by native import,
// since it is a feature of ESM
if (id.startsWith('data:'))
// since it is a feature of ESM.
// also externalize network imports since nodejs allows it when --experimental-network-imports
if (id.startsWith('data:') || /^(https?:)?\/\//.test(id))
return id

id = patchWindowsImportPath(id)
Expand Down
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions test/network-imports/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "@vitest/test-network-imports",
"type": "module",
"private": true,
"scripts": {
"test": "vitest"
},
"devDependencies": {
"vitest": "workspace:*"
}
}
5 changes: 5 additions & 0 deletions test/network-imports/public/[email protected]

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions test/network-imports/test/basic.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { expect, test } from 'vitest'

// @ts-expect-error network imports
import slash from 'http://localhost:9602/[email protected]'

// test without local server
// import slash from 'https://esm.sh/[email protected]'

test('network imports', () => {
expect(slash('foo\\bar')).toBe('foo/bar')
})
24 changes: 24 additions & 0 deletions test/network-imports/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { defineConfig } from 'vitest/config'

export default defineConfig({
test: {
poolOptions: {
threads: {
execArgv: ['--experimental-network-imports'],
},
forks: {
execArgv: ['--experimental-network-imports'],
},
// not supported?
// FAIL test/basic.test.ts [ test/basic.test.ts ]
// Error: ENOENT: no such file or directory, open 'http://localhost:9602/[email protected]'
// ❯ Object.openSync node:fs:596:3
// ❯ readFileSync node:fs:464:35
vmThreads: {
execArgv: ['--experimental-network-imports'],
},
},
// let vite serve public/[email protected]
api: 9602,
},
})

0 comments on commit 21f5744

Please sign in to comment.