Skip to content

Commit 5d07d7c

Browse files
authoredApr 4, 2023
perf: skip es-module-lexer if have no dynamic imports (#12732)
1 parent fedb080 commit 5d07d7c

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed
 

‎packages/vite/src/node/plugins/dynamicImportVars.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { dynamicImportToGlob } from '@rollup/plugin-dynamic-import-vars'
77
import type { KnownAsTypeMap } from 'types/importGlob'
88
import type { Plugin } from '../plugin'
99
import type { ResolvedConfig } from '../config'
10+
import { CLIENT_ENTRY } from '../constants'
1011
import {
1112
createFilter,
1213
normalizePath,
@@ -20,6 +21,11 @@ import { toAbsoluteGlob } from './importMetaGlob'
2021
export const dynamicImportHelperId = '\0vite/dynamic-import-helper'
2122

2223
const relativePathRE = /^\.{1,2}\//
24+
// fast path to check if source contains a dynamic import. we check for a
25+
// trailing slash too as a dynamic import statement can have comments between
26+
// the `import` and the `(`.
27+
const hasDynamicImportRE = /\bimport\s*[(/]/
28+
2329
interface DynamicImportRequest {
2430
as?: keyof KnownAsTypeMap
2531
}
@@ -162,7 +168,11 @@ export function dynamicImportVarsPlugin(config: ResolvedConfig): Plugin {
162168
},
163169

164170
async transform(source, importer) {
165-
if (!filter(importer)) {
171+
if (
172+
!filter(importer) ||
173+
importer === CLIENT_ENTRY ||
174+
!hasDynamicImportRE.test(source)
175+
) {
166176
return
167177
}
168178

0 commit comments

Comments
 (0)
Please sign in to comment.