From fde0f3c0e6e74a02e45ee0c0beb0ea774608e9a9 Mon Sep 17 00:00:00 2001 From: Bjorn Lu Date: Thu, 17 Mar 2022 17:00:35 +0800 Subject: [PATCH] chore: update es-module-lexer (#7357) --- packages/vite/LICENSE.md | 2 +- packages/vite/package.json | 2 +- .../vite/src/node/plugins/importAnalysis.ts | 6 +-- .../src/node/plugins/importAnalysisBuild.ts | 11 ++-- .../vite/src/node/ssr/ssrManifestPlugin.ts | 51 ++++++++----------- pnpm-lock.yaml | 8 +-- 6 files changed, 37 insertions(+), 43 deletions(-) diff --git a/packages/vite/LICENSE.md b/packages/vite/LICENSE.md index 1ff0b8dae73e1e..ccff3f1508a73b 100644 --- a/packages/vite/LICENSE.md +++ b/packages/vite/LICENSE.md @@ -1210,7 +1210,7 @@ Repository: git+https://github.com/guybedford/es-module-lexer.git > MIT License > ----------- > -> Copyright (C) 2018-2021 Guy Bedford +> Copyright (C) 2018-2022 Guy Bedford > > Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: > diff --git a/packages/vite/package.json b/packages/vite/package.json index 597a5c38a09c20..f67263a284dd4b 100644 --- a/packages/vite/package.json +++ b/packages/vite/package.json @@ -88,7 +88,7 @@ "debug": "^4.3.3", "dotenv": "^14.3.2", "dotenv-expand": "^5.1.0", - "es-module-lexer": "~0.9.3", + "es-module-lexer": "^0.10.1", "estree-walker": "^2.0.2", "etag": "^1.8.1", "fast-glob": "^3.2.11", diff --git a/packages/vite/src/node/plugins/importAnalysis.ts b/packages/vite/src/node/plugins/importAnalysis.ts index b906981b877493..cef523caf18311 100644 --- a/packages/vite/src/node/plugins/importAnalysis.ts +++ b/packages/vite/src/node/plugins/importAnalysis.ts @@ -381,7 +381,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin { continue } - const isDynamicImport = dynamicIndex >= 0 + const isDynamicImport = dynamicIndex > -1 // static import or valid string in dynamic import // If resolvable, let's resolve it @@ -466,8 +466,8 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin { if (isDynamicImport) { // rewrite `import('package')` to expose the default directly str().overwrite( - dynamicIndex, - end + 1, + expStart, + expEnd, `import('${url}').then(m => m.default && m.default.__esModule ? m.default : ({ ...m.default, default: m.default }))` ) } else { diff --git a/packages/vite/src/node/plugins/importAnalysisBuild.ts b/packages/vite/src/node/plugins/importAnalysisBuild.ts index 87afd8e1f12b00..71470f3757f994 100644 --- a/packages/vite/src/node/plugins/importAnalysisBuild.ts +++ b/packages/vite/src/node/plugins/importAnalysisBuild.ts @@ -142,6 +142,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin { s: start, e: end, ss: expStart, + se: expEnd, n: specifier, d: dynamicIndex } = imports[index] @@ -173,10 +174,9 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin { if (dynamicIndex > -1 && insertPreload) { needPreloadHelper = true - const dynamicEnd = source.indexOf(`)`, end) + 1 - const original = source.slice(dynamicIndex, dynamicEnd) + const original = source.slice(expStart, expEnd) const replacement = `${preloadMethod}(() => ${original},${isModernFlag}?"${preloadMarker}":void 0)` - str().overwrite(dynamicIndex, dynamicEnd, replacement) + str().overwrite(expStart, expEnd, replacement) } // Differentiate CSS imports that use the default export from those that @@ -263,7 +263,8 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin { n: name, s: start, e: end, - d: dynamicIndex + ss: expStart, + se: expEnd } = imports[index] // check the chunk being imported let url = name @@ -302,7 +303,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin { hasRemovedPureCssChunk = true } - s.overwrite(dynamicIndex, end + 1, 'Promise.resolve({})') + s.overwrite(expStart, expEnd, 'Promise.resolve({})') } } } diff --git a/packages/vite/src/node/ssr/ssrManifestPlugin.ts b/packages/vite/src/node/ssr/ssrManifestPlugin.ts index 1e00853402ba5d..69184150bb97b7 100644 --- a/packages/vite/src/node/ssr/ssrManifestPlugin.ts +++ b/packages/vite/src/node/ssr/ssrManifestPlugin.ts @@ -45,37 +45,30 @@ export function ssrManifestPlugin(config: ResolvedConfig): Plugin { } if (imports.length) { for (let index = 0; index < imports.length; index++) { - const { - s: start, - e: end, - n: name, - d: dynamicIndex - } = imports[index] - if (dynamicIndex) { - // check the chunk being imported - const url = code.slice(start, end) - const deps: string[] = [] - const ownerFilename = chunk.fileName - // literal import - trace direct imports and add to deps - const analyzed: Set = new Set() - const addDeps = (filename: string) => { - if (filename === ownerFilename) return - if (analyzed.has(filename)) return - analyzed.add(filename) - const chunk = bundle[filename] as OutputChunk | undefined - if (chunk) { - chunk.viteMetadata.importedCss.forEach((file) => { - deps.push(`/${file}`) - }) - chunk.imports.forEach(addDeps) - } + const { s: start, e: end, n: name } = imports[index] + // check the chunk being imported + const url = code.slice(start, end) + const deps: string[] = [] + const ownerFilename = chunk.fileName + // literal import - trace direct imports and add to deps + const analyzed: Set = new Set() + const addDeps = (filename: string) => { + if (filename === ownerFilename) return + if (analyzed.has(filename)) return + analyzed.add(filename) + const chunk = bundle[filename] as OutputChunk | undefined + if (chunk) { + chunk.viteMetadata.importedCss.forEach((file) => { + deps.push(`/${file}`) + }) + chunk.imports.forEach(addDeps) } - const normalizedFile = normalizePath( - join(dirname(chunk.fileName), url.slice(1, -1)) - ) - addDeps(normalizedFile) - ssrManifest[basename(name!)] = deps } + const normalizedFile = normalizePath( + join(dirname(chunk.fileName), url.slice(1, -1)) + ) + addDeps(normalizedFile) + ssrManifest[basename(name!)] = deps } } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ca7c1c32e28d99..d8246238955fc1 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -803,7 +803,7 @@ importers: debug: ^4.3.3 dotenv: ^14.3.2 dotenv-expand: ^5.1.0 - es-module-lexer: ~0.9.3 + es-module-lexer: ^0.10.1 esbuild: ^0.14.27 estree-walker: ^2.0.2 etag: ^1.8.1 @@ -880,7 +880,7 @@ importers: debug: 4.3.3 dotenv: 14.3.2 dotenv-expand: 5.1.0 - es-module-lexer: 0.9.3 + es-module-lexer: 0.10.1 estree-walker: 2.0.2 etag: 1.8.1 fast-glob: 3.2.11 @@ -4399,8 +4399,8 @@ packages: unbox-primitive: 1.0.1 dev: true - /es-module-lexer/0.9.3: - resolution: {integrity: sha512-1HQ2M2sPtxwnvOvT1ZClHyQDiggdNjURWpY2we6aMKCQiUVxTmVs2UYPLIrD84sS+kMdUwfBSylbJPwNnBrnHQ==} + /es-module-lexer/0.10.1: + resolution: {integrity: sha512-7zffMCLzbiuuRjL5FVs4BEIr6j+Istfrbwzrwg8de4ep1kSyZNJmmq5RQzyqJq4YhvOmAxZ3DBsZXDO4I5N71Q==} dev: true /es-to-primitive/1.2.1: