Skip to content

Commit

Permalink
perf: skip es-module-lexer if have no dynamic imports (#12732)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Apr 4, 2023
1 parent fedb080 commit 5d07d7c
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion packages/vite/src/node/plugins/dynamicImportVars.ts
Expand Up @@ -7,6 +7,7 @@ import { dynamicImportToGlob } from '@rollup/plugin-dynamic-import-vars'
import type { KnownAsTypeMap } from 'types/importGlob'
import type { Plugin } from '../plugin'
import type { ResolvedConfig } from '../config'
import { CLIENT_ENTRY } from '../constants'
import {
createFilter,
normalizePath,
Expand All @@ -20,6 +21,11 @@ import { toAbsoluteGlob } from './importMetaGlob'
export const dynamicImportHelperId = '\0vite/dynamic-import-helper'

const relativePathRE = /^\.{1,2}\//
// fast path to check if source contains a dynamic import. we check for a
// trailing slash too as a dynamic import statement can have comments between
// the `import` and the `(`.
const hasDynamicImportRE = /\bimport\s*[(/]/

interface DynamicImportRequest {
as?: keyof KnownAsTypeMap
}
Expand Down Expand Up @@ -162,7 +168,11 @@ export function dynamicImportVarsPlugin(config: ResolvedConfig): Plugin {
},

async transform(source, importer) {
if (!filter(importer)) {
if (
!filter(importer) ||
importer === CLIENT_ENTRY ||
!hasDynamicImportRE.test(source)
) {
return
}

Expand Down

0 comments on commit 5d07d7c

Please sign in to comment.