Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(vite-node): provide import.meta.resolve #5188

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions packages/vite-node/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,24 @@ export class ViteNodeRunner {
filename: __filename,
dirname: __dirname,
}
if (typeof import.meta.resolve !== 'undefined') {
// check if 2nd argument feature is available
let ok = false
try {
// old Node returns Promise
const testParent = await import.meta.resolve('.', new URL('./__test_parent__/', import.meta.url))
ok = typeof testParent === 'string' && testParent.endsWith('/__test_parent__/')
}
catch {
// old Node throws when invalid path
}
(meta as any).resolve = (specifier: string, parent?: string | URL) => {
if (!ok)
throw new Error('[vite-node] "--experimental-import-meta-resolve" is required to enable "import.meta.resolve"')
return import.meta.resolve(specifier, parent ?? href)
}
}

const exports = Object.create(null)
Object.defineProperty(exports, Symbol.toStringTag, {
value: 'Module',
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/import-meta-resolve/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "@vitest/test-import-meta-resolve",
"type": "module",
"private": true,
"scripts": {
"test": "vitest"
},
"devDependencies": {
"vitest": "workspace:*"
}
}
41 changes: 41 additions & 0 deletions test/import-meta-resolve/test/basic.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { expect, test } from 'vitest'

test('basic', () => {
// relative path
expect(
cleanDir(import.meta.resolve('../package.json')),
).toMatchInlineSnapshot(`"__DIR__/test/import-meta-resolve/package.json"`)

// not throw in latest NodeJS
expect(cleanDir(import.meta.resolve('../no-such-file'))).toMatchInlineSnapshot(
`"__DIR__/test/import-meta-resolve/no-such-file"`,
)

// with 2nd argument `parent`
expect(
cleanDir(
import.meta.resolve('./package.json', new URL('..', import.meta.url)),
),
).toMatchInlineSnapshot(`"__DIR__/test/import-meta-resolve/package.json"`)

// node_module
expect(cleanDir(import.meta.resolve('vitest'))).toMatchInlineSnapshot(
`"__DIR__/packages/vitest/dist/index.js"`,
)

expect(() =>
cleanDir(import.meta.resolve('@vitest/not-such-module')),
).toThrow(
expect.objectContaining({
message: expect.stringContaining(
'Cannot find package \'@vitest/not-such-module\' imported from',
),
}),
)
})

// make output deterministic
function cleanDir(out: string) {
const dir = new URL('../../..', import.meta.url).toString()
return out.replace(dir, '__DIR__/')
}
18 changes: 18 additions & 0 deletions test/import-meta-resolve/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { defineConfig } from 'vitest/config'

export default defineConfig({
test: {
poolOptions: {
threads: {
execArgv: ['--experimental-import-meta-resolve'],
},
forks: {
execArgv: ['--experimental-import-meta-resolve'],
},
vmThreads: {
// vmThreads already enables this flag
// execArgv: ['--experimental-import-meta-resolve'],
},
},
},
})
2 changes: 2 additions & 0 deletions test/vite-node/src/require-vite-node.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require('vite-node/client')
require('vite-node/server')
15 changes: 15 additions & 0 deletions test/vite-node/test/cjs.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { execFile } from 'node:child_process'
import { fileURLToPath } from 'node:url'
import { promisify } from 'node:util'
import { it } from 'vitest'

const execFileAsync = promisify(execFile)

it('require vite-node', async () => {
// verify `import.meta.resolve` usage doesn't cause syntax error on vite-node cjs build
// since rollup replaces it with `undefined`
await execFileAsync(
'node',
[fileURLToPath(new URL('../src/require-vite-node.cjs', import.meta.url))],
)
})
Loading