From 5d07d7c921f8cc6c9ca24926209066519b8b1f6b Mon Sep 17 00:00:00 2001 From: Bjorn Lu Date: Tue, 4 Apr 2023 17:22:13 +0800 Subject: [PATCH] perf: skip es-module-lexer if have no dynamic imports (#12732) --- packages/vite/src/node/plugins/dynamicImportVars.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/packages/vite/src/node/plugins/dynamicImportVars.ts b/packages/vite/src/node/plugins/dynamicImportVars.ts index 7933618d49da08..45a27c6f466431 100644 --- a/packages/vite/src/node/plugins/dynamicImportVars.ts +++ b/packages/vite/src/node/plugins/dynamicImportVars.ts @@ -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, @@ -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 } @@ -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 }