Skip to content

Commit

Permalink
fix: revert virtual adaptive module support
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey committed Dec 5, 2023
1 parent 9f1fd2e commit c82db74
Show file tree
Hide file tree
Showing 19 changed files with 198 additions and 687 deletions.
5 changes: 5 additions & 0 deletions .changeset/tidy-teachers-bathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"arc-vite": patch
---

Reverts virtual adaptive module support for now since it was not properly working and dramatically increased the complexity of this module and caused a few regressions that were difficult to resolve.
11 changes: 0 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import type { Plugin } from "vite";
import { pluginBuildSSR } from "./plugins/build-ssr";
import { pluginBuildWeb } from "./plugins/build-web";
import { pluginServe } from "./plugins/serve";
import type { FlagSet } from "./utils/flags";
import { type Options, getInternalPluginOptions } from "./utils/options";

export { createFlagSets, hasFlags } from "./utils/flags";
Expand All @@ -30,13 +29,3 @@ declare module "arc-server" {
body?: string;
};
}

declare module "rollup" {
interface CustomPluginOptions {
arcSourceId?: string;
arcSourceCode?: string;
arcScanIds?: string[];
arcFlagSet?: FlagSet;
arcFS?: typeof import("fs");
}
}
203 changes: 86 additions & 117 deletions src/plugins/build-ssr.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
import path from "path";
import type { Plugin } from "vite";
import { getArcFS } from "../utils/arc-fs";
import { ensureArcPluginIsFirst } from "../utils/ensure-arc-plugin-is-first";
import { decodeFileName, encodeFileName } from "../utils/filename-encoding";
import type { Plugin, Rollup } from "vite";
import { isAssetFile, isGlobalCSSFile } from "../utils/file-types";
import { indexToId } from "../utils/index-to-id";
import { isCssFile } from "../utils/is-css-file";
import { type Matches, clearCache } from "../utils/matches";
import { clearCache, getMatches, type Matches } from "../utils/matches";
import { type InternalPluginOptions } from "../utils/options";
import { toPosix } from "../utils/to-posix";
import {
decodeArcVirtualMatch,
getVirtualMatches,
isArcVirtualMatch,
} from "../utils/virtual-matches";
interface ProxyMeta {
resolved: Rollup.ResolvedId;
matches: Matches;
}

const virtualArcServerModuleId = "\0arc-server-virtual";
const arcPrefix = "\0arc-";
const arcSuffix = ".mjs";
const arcJsSuffix = ".mjs";
const arcProxyPrefix = `${arcPrefix}proxy:`;

// TODO: with some tweaks this plugin might work in a test env.
Expand All @@ -26,8 +22,9 @@ export function pluginBuildSSR({
flagSets,
forceFlagSet,
}: InternalPluginOptions): Plugin {
const adaptiveMatchesForId = new Map<string, Matches>();
let root: string;
let proxyModuleId = 0;
let metaForProxy = new Map<string, ProxyMeta>();
return {
name: "arc-vite:build-ssr",
enforce: "pre",
Expand All @@ -41,9 +38,6 @@ export function pluginBuildSSR({
apply(config, { command }) {
return command === "build" && !!config.build?.ssr;
},
config(config) {
ensureArcPluginIsFirst(config.plugins!);
},
configResolved(config) {
root = config.root;
},
Expand All @@ -63,14 +57,6 @@ export function pluginBuildSSR({
}

if (isArcId(importer)) {
if (isArcVirtualMatch(importer)) {
return this.resolve(
source,
this.getModuleInfo(importer)?.meta.arcSourceId,
options,
);
}

return source;
}

Expand All @@ -79,15 +65,12 @@ export function pluginBuildSSR({
skipSelf: true,
});
if (resolved && !resolved.external) {
const matches = await getVirtualMatches(this, flagSets, resolved);
const { id } = resolved;
const matches = getMatches(id, flagSets);
if (matches) {
const { id } = resolved;
if (!this.getModuleInfo(id)?.ast) {
await this.load(resolved);
}

adaptiveMatchesForId.set(id, matches);
return { id: encodeArcProxyId(id) };
const proxyId = nextProxyId();
metaForProxy.set(proxyId, { resolved, matches });
return proxyId;
}
}

Expand Down Expand Up @@ -132,106 +115,96 @@ function partsToString(parts, base, injectAttrs) {
}

if (isArcProxyId(id)) {
id = decodeArcProxyId(id);
const adaptiveMatches = adaptiveMatchesForId.get(id);
if (adaptiveMatches) {
if (isCssFile(id)) {
let code = "";
for (const { value } of adaptiveMatches.alternates) {
code += `import ${JSON.stringify(value)};\n`;
}

code += `import ${JSON.stringify(adaptiveMatches.default)};\n`;
const { resolved, matches } = metaForProxy.get(id)!;
let code = "";

return {
code,
moduleSideEffects: "no-treeshake",
};
if (isGlobalCSSFile(resolved.id)) {
for (const { value } of matches.alternates) {
code += `import ${JSON.stringify(value)};\n`;
}

const info = this.getModuleInfo(id);
if (info) {
let code = "";
let matchCode = "";
let matchCodeSep = "";
let i = 0;
code += `import ${JSON.stringify(matches.default)};\n`;

return {
code,
moduleSideEffects: "no-treeshake",
};
}

for (const { flags, value } of adaptiveMatches.alternates) {
const adaptedImportId = `_${indexToId(i++)}`;
code += `import * as ${adaptedImportId} from ${JSON.stringify(
value,
)};\n`;
let matchCode = "";
let matchCodeSep = "";
let i = 0;

matchCode +=
matchCodeSep +
flags.map((flag) => `f.${flag}`).join("&&") +
"?" +
adaptedImportId;
for (const { flags, value } of matches.alternates) {
const adaptedImportId = `_${indexToId(i++)}`;
code += `import * as ${adaptedImportId} from ${JSON.stringify(
value,
)};\n`;

matchCodeSep = ":";
}
matchCode +=
matchCodeSep +
flags.map((flag) => `f.${flag}`).join("&&") +
"?" +
adaptedImportId;

matchCodeSep = ":";
}

const defaultId = `_${indexToId(i)}`;
code += `import * as ${defaultId} from ${JSON.stringify(
adaptiveMatches.default,
)};\n`;
matchCode += `:${defaultId}`;
const defaultId = `_${indexToId(i)}`;
code += `import * as ${defaultId} from ${JSON.stringify(
matches.default,
)};\n`;
matchCode += `:${defaultId}`;

let syntheticNamedExports: string | boolean = false;
let hasNamedExports = false;
let hasDefaultExport = false;

if (isAssetFile(resolved.id)) {
hasDefaultExport = true;
} else {
let info = this.getModuleInfo(resolved.id);
if (!info?.ast) {
info = await this.load(resolved);
}

let syntheticNamedExports: string | boolean = false;
if (info.exports?.length) {
const hasNamedExports =
info.exports.length >= (info.hasDefaultExport ? 2 : 1);
if (info.exports) {
hasDefaultExport = info.hasDefaultExport === true;
hasNamedExports = info.exports.length >= (hasDefaultExport ? 2 : 1);
}
}

if (hasNamedExports || info.hasDefaultExport) {
const proxyCode = `/*@__PURE__*/createAdaptiveProxy({default:${defaultId},match(f){return ${matchCode}}})`;
code += `import createAdaptiveProxy from "arc-server/proxy";\n`;
if (hasNamedExports || hasDefaultExport) {
const proxyCode = `/*@__PURE__*/createAdaptiveProxy({default:${defaultId},match(f){return ${matchCode}}})`;
code += `import createAdaptiveProxy from "arc-server/proxy";\n`;

if (hasNamedExports) {
syntheticNamedExports = "_";
code += `export const _ = ${proxyCode};\n`;
if (hasNamedExports) {
syntheticNamedExports = "_";
code += `export const _ = ${proxyCode};\n`;

if (info.hasDefaultExport) {
code += "export default _.default;\n";
}
} else {
code += `export default ${proxyCode}.default;\n`;
}
}
} else {
code += "export {};\n";
if (hasDefaultExport) {
code += "export default _.default;\n";
}

return {
code,
syntheticNamedExports,
moduleSideEffects: "no-treeshake",
};
} else {
code += `export default ${proxyCode}.default;\n`;
}
} else {
code += "export {};\n";
}
} else if (isArcVirtualMatch(id)) {
const [arcSourceId, arcFlagSet] = decodeArcVirtualMatch(id);
const { meta, moduleSideEffects, syntheticNamedExports } =
this.getModuleInfo(arcSourceId)!;
const code = meta.arcSourceCode as string;
const arcFS = getArcFS(arcFlagSet);

return {
code,
moduleSideEffects,
syntheticNamedExports,
meta: {
...meta,
arcSourceId,
arcFlagSet,
arcFS,
},
moduleSideEffects: "no-treeshake",
};
}

return null;
},
buildEnd() {
closeBundle() {
clearCache();
adaptiveMatchesForId.clear();
proxyModuleId = 0;
metaForProxy = new Map();
},
generateBundle(outputOptions, bundle, isWrite) {
if (!isWrite) {
Expand Down Expand Up @@ -264,6 +237,10 @@ function partsToString(parts, base, injectAttrs) {
store.write({ serverEntryFiles });
},
};

function nextProxyId() {
return arcProxyPrefix + (proxyModuleId++).toString(36) + arcJsSuffix;
}
}

function isArcId(id: string) {
Expand All @@ -273,11 +250,3 @@ function isArcId(id: string) {
function isArcProxyId(id: string) {
return id.startsWith(arcProxyPrefix);
}

function encodeArcProxyId(id: string) {
return arcProxyPrefix + encodeFileName(id) + arcSuffix;
}

function decodeArcProxyId(id: string) {
return decodeFileName(id.slice(arcProxyPrefix.length, -arcSuffix.length));
}
Loading

0 comments on commit c82db74

Please sign in to comment.