From 8bd0678e7d425e45cb1edbda982ee05dc61706a1 Mon Sep 17 00:00:00 2001 From: bluwy Date: Tue, 4 Apr 2023 14:54:33 +0800 Subject: [PATCH 1/2] perf: skip es-module-lexer if have no dynamic imports --- packages/vite/src/node/plugins/dynamicImportVars.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/vite/src/node/plugins/dynamicImportVars.ts b/packages/vite/src/node/plugins/dynamicImportVars.ts index 7933618d49da08..c9d786919d7abb 100644 --- a/packages/vite/src/node/plugins/dynamicImportVars.ts +++ b/packages/vite/src/node/plugins/dynamicImportVars.ts @@ -20,6 +20,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 +167,7 @@ export function dynamicImportVarsPlugin(config: ResolvedConfig): Plugin { }, async transform(source, importer) { - if (!filter(importer)) { + if (!filter(importer) || !hasDynamicImportRE.test(source)) { return } From 1522d9f1633713a68b09d30ebfff683bc48dda7d Mon Sep 17 00:00:00 2001 From: bluwy Date: Tue, 4 Apr 2023 14:58:08 +0800 Subject: [PATCH 2/2] perf: also skip client entry --- packages/vite/src/node/plugins/dynamicImportVars.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/vite/src/node/plugins/dynamicImportVars.ts b/packages/vite/src/node/plugins/dynamicImportVars.ts index c9d786919d7abb..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, @@ -167,7 +168,11 @@ export function dynamicImportVarsPlugin(config: ResolvedConfig): Plugin { }, async transform(source, importer) { - if (!filter(importer) || !hasDynamicImportRE.test(source)) { + if ( + !filter(importer) || + importer === CLIENT_ENTRY || + !hasDynamicImportRE.test(source) + ) { return }