From 161b0c14ea8dc1349a9b6461b29d424eee7ffb25 Mon Sep 17 00:00:00 2001 From: dpiercey Date: Mon, 20 Nov 2023 08:33:32 -0700 Subject: [PATCH] fix: avoid using this.load api unless necessary --- .changeset/curvy-bags-invent.md | 5 +++++ src/plugins/build-ssr.ts | 30 +++++++++++++++++------------- src/plugins/build-web.ts | 19 +++++++++++++++---- 3 files changed, 37 insertions(+), 17 deletions(-) create mode 100644 .changeset/curvy-bags-invent.md diff --git a/.changeset/curvy-bags-invent.md b/.changeset/curvy-bags-invent.md new file mode 100644 index 0000000..947aced --- /dev/null +++ b/.changeset/curvy-bags-invent.md @@ -0,0 +1,5 @@ +--- +"arc-vite": patch +--- + +Avoid using `this.load` unless the ast has not already been parsed for a module. diff --git a/src/plugins/build-ssr.ts b/src/plugins/build-ssr.ts index 7180c76..a76e929 100644 --- a/src/plugins/build-ssr.ts +++ b/src/plugins/build-ssr.ts @@ -82,6 +82,10 @@ export function pluginBuildSSR({ const matches = await getVirtualMatches(this, flagSets, resolved); if (matches) { const { id } = resolved; + if (!this.getModuleInfo(id)?.ast) { + await this.load(resolved); + } + adaptiveMatchesForId.set(id, matches); return { id: encodeArcProxyId(id) }; } @@ -131,22 +135,22 @@ function partsToString(parts, base, injectAttrs) { id = decodeArcProxyId(id); const adaptiveMatches = adaptiveMatchesForId.get(id); if (adaptiveMatches) { - const info = await this.load({ id }); - if (info) { - if (isCssFile(id)) { - let code = ""; - for (const { value } of adaptiveMatches.alternates) { - code += `import ${JSON.stringify(value)};\n`; - } + if (isCssFile(id)) { + let code = ""; + for (const { value } of adaptiveMatches.alternates) { + code += `import ${JSON.stringify(value)};\n`; + } - code += `import ${JSON.stringify(adaptiveMatches.default)};\n`; + code += `import ${JSON.stringify(adaptiveMatches.default)};\n`; - return { - code, - moduleSideEffects: "no-treeshake", - }; - } + return { + code, + moduleSideEffects: "no-treeshake", + }; + } + const info = this.getModuleInfo(id); + if (info) { let code = ""; let matchCode = ""; let matchCodeSep = ""; diff --git a/src/plugins/build-web.ts b/src/plugins/build-web.ts index ee6de66..a3da545 100644 --- a/src/plugins/build-web.ts +++ b/src/plugins/build-web.ts @@ -91,6 +91,11 @@ export function pluginBuildWeb({ const matches = await getVirtualMatches(this, flagSets, resolved); if (matches) { const { id } = resolved; + + if (!this.getModuleInfo(id)?.ast) { + await this.load(resolved); + } + adaptiveMatchesForId.set(id, matches); const adaptiveImportsForImporter = @@ -212,10 +217,11 @@ export function pluginBuildWeb({ importer, resolvedAdaptiveImports, ] of adaptiveImporters) { - const info = await this.load({ id: importer }); + const ast = (this.getModuleInfo(importer)?.ast || + (await this.load({ id: importer })) + .ast) as unknown as estree.Program; - for (const child of (info.ast as unknown as estree.Program) - .body) { + for (const child of ast.body) { if (child.type === "ImportDeclaration") { const id = resolvedAdaptiveImports.get( child.source.value as string, @@ -289,7 +295,12 @@ export function pluginBuildWeb({ if (isArcProxyId(id)) { id = decodeArcProxyId(id); - const info = await this.load({ id }); + + if (isCssFile(id)) { + return { code: "" }; + } + + const info = this.getModuleInfo(id); if (info) { let code = ""; let syntheticNamedExports: boolean | string = false;