Skip to content

Commit

Permalink
fix: avoid using Vite's load api (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey authored Sep 23, 2022
1 parent 7483689 commit 5640120
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 23 deletions.
5 changes: 5 additions & 0 deletions .changeset/little-rivers-jog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@marko/vite": patch
---

Avoid using `.load` api since it seems to cause an issue when there are many entry Marko files.
44 changes: 21 additions & 23 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ interface ServerManifest {
entries: {
[entryId: string]: string;
};
entrySources: {
[entryId: string]: string;
};
chunksNeedingAssets: string[];
}

Expand Down Expand Up @@ -125,7 +128,7 @@ export default function markoPlugin(opts: Options = {}): vite.Plugin[] {
let devServer: vite.ViteDevServer;
let registeredTag: string | false = false;
let serverManifest: ServerManifest | undefined;
const devEntryFileSources = new Map<string, string>();
const entrySources = new Map<string, string>();
const transformWatchFiles = new Map<string, string[]>();
const transformOptionalFiles = new Map<string, string[]>();

Expand Down Expand Up @@ -216,7 +219,7 @@ export default function markoPlugin(opts: Options = {}): vite.Plugin[] {
devServer = _server;
devServer.watcher.on("all", (type, filename) => {
if (type === "unlink") {
devEntryFileSources.delete(filename);
entrySources.delete(filename);
}

for (const [id, files] of transformWatchFiles) {
Expand Down Expand Up @@ -249,6 +252,12 @@ export default function markoPlugin(opts: Options = {}): vite.Plugin[] {
await fs.promises.readFile(serverMetaFile, "utf-8")
) as ServerManifest;
inputOptions.input = toHTMLEntries(root, serverManifest.entries);
for (const entry in serverManifest.entrySources) {
entrySources.set(
path.resolve(root, entry),
serverManifest.entrySources[entry]
);
}
} catch (err) {
this.error(
`You must run the "ssr" build before the "browser" build.`
Expand Down Expand Up @@ -333,18 +342,19 @@ export default function markoPlugin(opts: Options = {}): vite.Plugin[] {
case serverEntryQuery: {
const fileName = id.slice(0, -serverEntryQuery.length);
let entryData: string;
entrySources.set(fileName, "");

if (isBuild) {
const relativeFileName = path.posix.relative(root, fileName);
const entryId = toEntryId(relativeFileName);
serverManifest ??= {
entries: {},
entrySources: {},
chunksNeedingAssets: [],
};
serverManifest.entries[entryId] = relativeFileName;
entryData = JSON.stringify(entryId);
} else {
devEntryFileSources.set(fileName, "");
entryData = JSON.stringify(
await generateDocManifest(
await devServer.transformIndexHtml(
Expand All @@ -364,24 +374,8 @@ export default function markoPlugin(opts: Options = {}): vite.Plugin[] {
case browserEntryQuery: {
// The goal below is to use the original source content
// for all browser entries rather than load the content
// from disk again.
const realId = id.slice(0, -browserEntryQuery.length);
if (isBuild) {
// In build mode we can actually use the `this.load` api.
// https://github.com/vitejs/vite/issues/6810
const {
meta: { source },
} = await this.load({
id: realId,
resolveDependencies: false,
});

return source;
}

// Otherwise in dev mode we store the source content
// when we discover the server entry.
return devEntryFileSources.get(realId);
// from disk again. This is to support virtual Marko entry files.
return entrySources.get(id.slice(0, -browserEntryQuery.length));
}
}

Expand Down Expand Up @@ -413,8 +407,12 @@ export default function markoPlugin(opts: Options = {}): vite.Plugin[] {
return null;
}

if (ssr && !isBuild && devEntryFileSources.has(id)) {
devEntryFileSources.set(id, source);
if (ssr && entrySources.has(id)) {
entrySources.set(id, source);

if (serverManifest) {
serverManifest.entrySources[path.relative(root, id)] = source;
}
}

const compiled = await compiler.compile(
Expand Down

0 comments on commit 5640120

Please sign in to comment.