Skip to content

Commit

Permalink
fix: resolve dynamicly imported modules to be the same as statically (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Mar 9, 2022
1 parent 78e66ff commit 094d23e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 13 deletions.
18 changes: 18 additions & 0 deletions examples/vue/test/imports.test.ts
@@ -0,0 +1,18 @@
describe('import vue components', () => {
test('normal imports as expected', async() => {
const cmp = await import('../components/Hello.vue')
expect(cmp).toBeDefined()
})

test('template string imports as expected', async() => {
// eslint-disable-next-line quotes
const cmp = await import(`../components/Hello.vue`)
expect(cmp).toBeDefined()
})

test('dynamic imports as expected', async() => {
const name = 'Hello'
const cmp = await import(`../components/${name}.vue`)
expect(cmp).toBeDefined()
})
})
1 change: 1 addition & 0 deletions examples/vue/tsconfig.json
@@ -1,6 +1,7 @@
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"moduleResolution": "node",
}
}
8 changes: 4 additions & 4 deletions packages/vite-node/src/client.ts
@@ -1,7 +1,7 @@
import { createRequire } from 'module'
import { fileURLToPath, pathToFileURL } from 'url'
import vm from 'vm'
import { dirname, isAbsolute, resolve } from 'pathe'
import { dirname, extname, isAbsolute, resolve } from 'pathe'
import { isNodeBuiltin } from 'mlly'
import { isPrimitive, normalizeId, slash, toFilePath } from './utils'
import type { ModuleCache, ViteNodeRunnerOptions } from './types'
Expand Down Expand Up @@ -57,7 +57,7 @@ export class ViteNodeRunner {
// and wasn't transformed by Vite
if (this.shouldResolveId(dep)) {
const resolvedDep = await this.options.resolveId(dep, id)
dep = resolvedDep?.id || dep
dep = resolvedDep?.id?.replace(this.root, '') || dep
}
if (callstack.includes(dep)) {
if (!this.moduleCache.get(dep)?.exports)
Expand Down Expand Up @@ -141,10 +141,10 @@ export class ViteNodeRunner {
}

shouldResolveId(dep: string) {
if (isNodeBuiltin(dep))
if (isNodeBuiltin(dep) || dep in (this.options.requestStubs || DEFAULT_REQUEST_STUBS))
return false

return !isAbsolute(dep)
return !isAbsolute(dep) || !extname(dep)
}

/**
Expand Down
18 changes: 9 additions & 9 deletions test/core/test/imports.test.ts
@@ -1,30 +1,30 @@
import { expect, test } from 'vitest'

test('dynamic relative import works', async() => {
const { timeout } = await import('./../src/timeout')
const stringTimeoutMod = await import('./../src/timeout')

const timeoutPath = './../src/timeout'
const { timeout: dynamicTimeout } = await import(timeoutPath)
const variableTimeoutMod = await import(timeoutPath)

expect(timeout).toBe(dynamicTimeout)
expect(stringTimeoutMod).toBe(variableTimeoutMod)
})

test('dynamic aliased import works', async() => {
const { timeout } = await import('./../src/timeout')
const stringTimeoutMod = await import('./../src/timeout')

const timeoutPath = '@/timeout'
const { timeout: dynamicTimeout } = await import(timeoutPath)
const variableTimeoutMod = await import(timeoutPath)

expect(timeout).toBe(dynamicTimeout)
expect(stringTimeoutMod).toBe(variableTimeoutMod)
})

test('dynamic absolute import works', async() => {
const { timeout } = await import('./../src/timeout')
const stringTimeoutMod = await import('./../src/timeout')

const timeoutPath = '/src/timeout'
const { timeout: dynamicTimeout } = await import(timeoutPath)
const variableTimeoutMod = await import(timeoutPath)

expect(timeout).toBe(dynamicTimeout)
expect(stringTimeoutMod).toBe(variableTimeoutMod)
})

test('data with dynamic import works', async() => {
Expand Down

0 comments on commit 094d23e

Please sign in to comment.