Skip to content

Commit 21f5744

Browse files
authoredJan 17, 2024
fix(vite-node): externalize network imports (#4987)
1 parent 6c5fe49 commit 21f5744

File tree

8 files changed

+62
-3
lines changed

8 files changed

+62
-3
lines changed
 

‎eslint.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export default antfu(
1313
'test/core/src/self',
1414
'test/workspaces/results.json',
1515
'test/reporters/fixtures/with-syntax-error.test.js',
16+
'test/network-imports/public/slash@3.0.0.js',
1617
'examples/**/mockServiceWorker.js',
1718
],
1819
},

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"test:run": "vitest run -r test/core",
2828
"test:all": "CI=true pnpm -r --stream run test --allowOnly",
2929
"test:ci": "CI=true pnpm -r --stream --filter !test-browser --filter !test-esm --filter !test-browser run test --allowOnly",
30-
"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",
30+
"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",
3131
"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",
3232
"typecheck": "tsc -p tsconfig.check.json --noEmit",
3333
"typecheck:why": "tsc -p tsconfig.check.json --noEmit --explainFiles > explainTypes.txt",

‎packages/vite-node/src/externalize.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,9 @@ async function _shouldExternalize(
9595
return id
9696

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

102103
id = patchWindowsImportPath(id)

‎pnpm-lock.yaml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎test/network-imports/package.json

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "@vitest/test-network-imports",
3+
"type": "module",
4+
"private": true,
5+
"scripts": {
6+
"test": "vitest"
7+
},
8+
"devDependencies": {
9+
"vitest": "workspace:*"
10+
}
11+
}

‎test/network-imports/public/slash@3.0.0.js

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { expect, test } from 'vitest'
2+
3+
// @ts-expect-error network imports
4+
import slash from 'http://localhost:9602/slash@3.0.0.js'
5+
6+
// test without local server
7+
// import slash from 'https://esm.sh/slash@3.0.0'
8+
9+
test('network imports', () => {
10+
expect(slash('foo\\bar')).toBe('foo/bar')
11+
})

‎test/network-imports/vitest.config.ts

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { defineConfig } from 'vitest/config'
2+
3+
export default defineConfig({
4+
test: {
5+
poolOptions: {
6+
threads: {
7+
execArgv: ['--experimental-network-imports'],
8+
},
9+
forks: {
10+
execArgv: ['--experimental-network-imports'],
11+
},
12+
// not supported?
13+
// FAIL test/basic.test.ts [ test/basic.test.ts ]
14+
// Error: ENOENT: no such file or directory, open 'http://localhost:9602/slash@3.0.0.js'
15+
// ❯ Object.openSync node:fs:596:3
16+
// ❯ readFileSync node:fs:464:35
17+
vmThreads: {
18+
execArgv: ['--experimental-network-imports'],
19+
},
20+
},
21+
// let vite serve public/slash@3.0.0.js
22+
api: 9602,
23+
},
24+
})

0 commit comments

Comments
 (0)
Please sign in to comment.