Skip to content

Commit

Permalink
feat: add hmr support
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey committed Sep 14, 2021
1 parent 8f5b4b6 commit de097f0
Showing 1 changed file with 24 additions and 17 deletions.
41 changes: 24 additions & 17 deletions src/loader/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ export default async function (
try {
const baseConfig = {
sourceMaps,
hot: this.hot,
fileSystem: this.fs,
writeVersionComment: false,
runtimeId: pluginOptions.runtimeId,
Expand Down Expand Up @@ -165,7 +166,7 @@ export default async function (

return done(
null,
code + referenceMissingDeps(this, resourcePath, meta),
code + getTrailingContent(this, resourcePath, meta),
(map as unknown) as string
);
}
Expand All @@ -187,7 +188,7 @@ export default async function (

return done(
null,
mwpPrefix + code + referenceMissingDeps(this, resourcePath, meta)
mwpPrefix + code + getTrailingContent(this, resourcePath, meta)
);
}

Expand All @@ -202,44 +203,50 @@ export default async function (

return done(
null,
code + referenceMissingDeps(this, resourcePath, meta),
code + getTrailingContent(this, resourcePath, meta),
(map as unknown) as string
);
} catch (err) {
done(err);
}
}

function referenceMissingDeps(
function getTrailingContent(
ctx: webpack.loader.LoaderContext,
resource: string,
meta: MarkoMeta
) {
let result = "";

if (meta.watchFiles) {
for (const watchFile of meta.watchFiles) {
ctx.addDependency(watchFile);
}
}

if (!ctx._compiler.watchMode) {
return "";
}
if (ctx._compiler.watchMode) {
const missingDeps = [];
for (const watchFile of WATCH_MISSING_FILES) {
if (!watchFile.has(meta)) {
missingDeps.push(watchFile.basename);
}
}

const missingDeps = [];
for (const watchFile of WATCH_MISSING_FILES) {
if (!watchFile.has(meta)) {
missingDeps.push(watchFile.basename);
if (missingDeps.length) {
const templateFileName = getBasenameWithoutExt(resource);
result += `\nrequire.context(".", false, /\\${path.sep}${
templateFileName === "index"
? ""
: `${escapeRegExp(templateFileName)}\\.`
}(?:${missingDeps.join("|")})\\.[^\\${path.sep}]+$/)`;
}
}

if (missingDeps.length) {
const templateFileName = getBasenameWithoutExt(resource);
return `require.context(".", false, /\\${path.sep}${
templateFileName === "index" ? "" : `${escapeRegExp(templateFileName)}\\.`
}(?:${missingDeps.join("|")})\\.[^\\${path.sep}]+$/)`;
if (ctx.hot) {
result += "\nif (import.meta.webpackHot) import.meta.webpackHot.accept()";
}

return "";
return result;
}

function getCompiler(ctx: webpack.loader.LoaderContext) {
Expand Down

0 comments on commit de097f0

Please sign in to comment.