Skip to content

Commit

Permalink
fix: improve hmr support (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey authored May 25, 2023
1 parent 07fbf34 commit e3015db
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 34 deletions.
5 changes: 5 additions & 0 deletions .changeset/fluffy-timers-sleep.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@marko/vite": patch
---

Improve HMR support by ensuring that client and server compiled modules have different resolved ids.
78 changes: 44 additions & 34 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ const queryReg = /\?marko-.+$/;
const browserEntryQuery = "?marko-browser-entry";
const serverEntryQuery = "?marko-server-entry";
const virtualFileQuery = "?marko-virtual";
const browserQuery = "?marko-browser";
const manifestFileName = "manifest.json";
const markoExt = ".marko";
const htmlExt = ".html";
Expand Down Expand Up @@ -140,7 +141,8 @@ export default function markoPlugin(opts: Options = {}): vite.Plugin[] {
let store: BuildStore;
let CJSTemplates: Set<string> | undefined;
let basePath = "/";
const entrySources = new Map<string, string>();
const entryIds = new Set<string>();
const cachedSources = new Map<string, string>();
const transformWatchFiles = new Map<string, string[]>();
const transformOptionalFiles = new Map<string, string[]>();

Expand Down Expand Up @@ -321,8 +323,10 @@ export default function markoPlugin(opts: Options = {}): vite.Plugin[] {
ssrConfig.hot = domConfig.hot = true;
devServer = _server;
devServer.watcher.on("all", (type, filename) => {
cachedSources.delete(filename);

if (type === "unlink") {
entrySources.delete(filename);
entryIds.delete(filename);
transformWatchFiles.delete(filename);
transformOptionalFiles.delete(filename);
}
Expand Down Expand Up @@ -362,10 +366,9 @@ export default function markoPlugin(opts: Options = {}): vite.Plugin[] {
) as ServerManifest;
inputOptions.input = toHTMLEntries(root, serverManifest.entries);
for (const entry in serverManifest.entrySources) {
entrySources.set(
normalizePath(path.resolve(root, entry)),
serverManifest.entrySources[entry]
);
const id = normalizePath(path.resolve(root, entry));
entryIds.add(id);
cachedSources.set(id, serverManifest.entrySources[entry]);
}
} catch (err) {
this.error(
Expand Down Expand Up @@ -410,6 +413,8 @@ export default function markoPlugin(opts: Options = {}): vite.Plugin[] {
this.getModuleInfo(importer)?.isEntry
) {
importeeQuery = browserEntryQuery;
} else if (linked && !ssr && !importeeQuery && isMarkoFile(importee)) {
importeeQuery = browserQuery;
}

if (importeeQuery) {
Expand Down Expand Up @@ -450,11 +455,12 @@ export default function markoPlugin(opts: Options = {}): vite.Plugin[] {
return null;
},
async load(id) {
switch (getMarkoQuery(id)) {
const query = getMarkoQuery(id);
switch (query) {
case serverEntryQuery: {
const fileName = id.slice(0, -serverEntryQuery.length);
const fileName = id.slice(0, -query.length);
let entryData: string;
entrySources.set(fileName, "");
entryIds.add(fileName);

if (isBuild) {
const relativeFileName = path.posix.relative(root, fileName);
Expand Down Expand Up @@ -485,11 +491,12 @@ export default function markoPlugin(opts: Options = {}): vite.Plugin[] {
basePathVar: isBuild ? basePathVar : undefined,
});
}
case browserEntryQuery: {
// The goal below is to use the original source content
// for all browser entries rather than load the content
// from disk again. This is to support virtual Marko entry files.
return entrySources.get(id.slice(0, -browserEntryQuery.length));
case browserEntryQuery:
case browserQuery: {
// The goal below is to cached source content when in linked mode
// to avoid loading from disk for both server and browser builds.
// This is to support virtual Marko entry files.
return cachedSources.get(id.slice(0, -query.length)) || null;
}
}

Expand Down Expand Up @@ -522,28 +529,31 @@ export default function markoPlugin(opts: Options = {}): vite.Plugin[] {
return null;
}

if (CJSTemplates?.has(id)) {
return createEsmWrapper(
id,
getExportIdentifiers(
(
await compiler.compile(source, id, {
cache,
ast: true,
code: false,
output: "source",
sourceMaps: false,
})
).ast
)
);
}
if (isSSR) {
if (linked) {
cachedSources.set(id, source);

if (isSSR && linked && entrySources.has(id)) {
entrySources.set(id, source);
if (serverManifest && entryIds.has(id)) {
serverManifest.entrySources[path.posix.relative(root, id)] =
source;
}
}

if (serverManifest) {
serverManifest.entrySources[path.posix.relative(root, id)] = source;
if (CJSTemplates?.has(id)) {
return createEsmWrapper(
id,
getExportIdentifiers(
(
await compiler.compile(source, id, {
cache,
ast: true,
code: false,
output: "source",
sourceMaps: false,
})
).ast
)
);
}
}

Expand Down

0 comments on commit e3015db

Please sign in to comment.