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(module-runner): delay function eval until module runner instantiation #18480

Merged
merged 1 commit into from
Oct 28, 2024
Merged
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
4 changes: 2 additions & 2 deletions packages/vite/src/module-runner/esmEvaluator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
AsyncFunction,
asyncFunctionDeclarationPaddingLineCount,
getAsyncFunctionDeclarationPaddingLineCount,
} from '../shared/utils'
import {
ssrDynamicImportKey,
Expand All @@ -12,7 +12,7 @@ import {
import type { ModuleEvaluator, ModuleRunnerContext } from './types'

export class ESModulesEvaluator implements ModuleEvaluator {
startOffset = asyncFunctionDeclarationPaddingLineCount
startOffset = getAsyncFunctionDeclarationPaddingLineCount()

async runInlinedModule(
context: ModuleRunnerContext,
Expand Down
13 changes: 9 additions & 4 deletions packages/vite/src/shared/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,14 @@ export function withTrailingSlash(path: string): string {
export const AsyncFunction = async function () {}.constructor as typeof Function

// https://github.com/nodejs/node/issues/43047#issuecomment-1564068099
export const asyncFunctionDeclarationPaddingLineCount =
/** #__PURE__ */ (() => {
let asyncFunctionDeclarationPaddingLineCount: number | undefined

export function getAsyncFunctionDeclarationPaddingLineCount(): number {
if (typeof asyncFunctionDeclarationPaddingLineCount === 'undefined') {
const body = '/*code*/'
const source = new AsyncFunction('a', 'b', body).toString()
return source.slice(0, source.indexOf(body)).split('\n').length - 1
})()
asyncFunctionDeclarationPaddingLineCount =
source.slice(0, source.indexOf(body)).split('\n').length - 1
}
return asyncFunctionDeclarationPaddingLineCount
}