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

fix(runner): resolve and normalize absolute path in fetchModule #18361

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion packages/vite/src/module-runner/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function normalizeAbsoluteUrl(url: string, root: string): string {
// /root/id.js -> /id.js
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the addition of resolveId in fetchModule makes this irrelevant. The only downside is the additional call to the server if the entry point is not normalized, but it was already imported.

If we move this to the environment, then maybe this whole function can be removed, which also makes it so the root is not required anymore

Copy link
Collaborator Author

@hi-ogawa hi-ogawa Oct 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I moved everything to fetchModule including file:// handling. It looks like this works, but the behavior of file:// might be actually different from browser and v5 since Astro is relying on userland plugin #17369 (comment)

(though In my opinion, supporting file:// out of the box seems nice to have anyways)

// C:/root/id.js -> /id.js
// 1 is to keep the leading slash
url = url.slice(root.length - 1)
// url = url.slice(root.length - 1)
}

return url
Expand Down
7 changes: 4 additions & 3 deletions packages/vite/src/node/ssr/fetchModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export async function fetchModule(

url = unwrapId(url)

let mod = await environment.moduleGraph.getModuleByUrl(url)
let mod = await environment.moduleGraph.ensureEntryFromUrl(url)
const cached = !!mod?.transformResult

// if url is already cached, we can just confirm it's also cached on the server
Expand All @@ -116,15 +116,16 @@ export async function fetchModule(
}

// module entry should be created by transformRequest
mod ??= await environment.moduleGraph.getModuleByUrl(url)
const modById = environment.moduleGraph.getModuleById(mod.id!)

if (!mod) {
if (!modById) {
throw new Error(
`[vite] cannot find module '${url}' ${
importer ? ` imported from '${importer}'` : ''
}.`,
)
}
mod = modById

if (options.inlineSourceMap !== false) {
result = inlineSourceMap(mod, result, options.startOffset)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ export const initialize = async () => {
dynamicRelative: await import(nameRelative),
dynamicAbsolute: await import(nameAbsolute),
dynamicAbsoluteExtension: await import(nameAbsoluteExtension),
dynamicAbsoluteFull: await import(path.join(import.meta.dirname, "simple.js")),
dynamicAbsoluteFull: await import(path.join(
hi-ogawa marked this conversation as resolved.
Show resolved Hide resolved
...process.platform === 'win32' ? ['/@fs'] : [],
import.meta.dirname, "simple.js"
)),
Copy link
Collaborator Author

@hi-ogawa hi-ogawa Oct 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After the change, for Windows, import("C:/...") doesn't work but probably this behavior hasn't changed from v5? (Also node doesn't support it either as they require import("file://...") for windows path)

I think I added this test case in #17422 saying it's broken in v6, but maybe it was already broken on v5 if I tested it on Windows 😅

static: staticModule,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -248,3 +248,41 @@ describe('optimize-deps', async () => {
expect(mod.default.hello()).toMatchInlineSnapshot(`"world"`)
})
})

describe('resolveId absolute path entry', async () => {
const it = await createModuleRunnerTester({
plugins: [
{
name: 'test-resolevId',
enforce: 'pre',
resolveId(source) {
if (
source ===
posix.join(this.environment.config.root, 'fixtures/basic.js')
) {
return '\0virtual:basic'
}
},
load(id) {
if (id === '\0virtual:basic') {
return `export const name = "virtual:basic"`
}
},
},
],
})

it('ssrLoadModule', async ({ server }) => {
const mod = await server.ssrLoadModule(
posix.join(server.config.root, 'fixtures/basic.js'),
)
expect(mod.name).toMatchInlineSnapshot(`"virtual:basic"`)
})

it('runner', async ({ server, runner }) => {
const mod = await runner.import(
posix.join(server.config.root, 'fixtures/basic.js'),
)
expect(mod.name).toMatchInlineSnapshot(`"virtual:basic"`)
})
})
1 change: 1 addition & 0 deletions packages/vite/src/node/ssr/runtime/__tests__/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export async function createModuleRunnerTester(
}
},
},
...(config.plugins ?? []),
],
...config,
})
Expand Down