Skip to content

Commit

Permalink
fix: avoid using this.load api unless necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey committed Nov 20, 2023
1 parent 798c045 commit 161b0c1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/curvy-bags-invent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"arc-vite": patch
---

Avoid using `this.load` unless the ast has not already been parsed for a module.
30 changes: 17 additions & 13 deletions src/plugins/build-ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) };
}
Expand Down Expand Up @@ -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 = "";
Expand Down
19 changes: 15 additions & 4 deletions src/plugins/build-web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 161b0c1

Please sign in to comment.