From de097f0fe508fa8320433f27c29e867d45d2a323 Mon Sep 17 00:00:00 2001 From: Dylan Piercey Date: Tue, 14 Sep 2021 11:23:05 -0700 Subject: [PATCH] feat: add hmr support --- src/loader/index.ts | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/src/loader/index.ts b/src/loader/index.ts index c82c815..b4a27cc 100644 --- a/src/loader/index.ts +++ b/src/loader/index.ts @@ -114,6 +114,7 @@ export default async function ( try { const baseConfig = { sourceMaps, + hot: this.hot, fileSystem: this.fs, writeVersionComment: false, runtimeId: pluginOptions.runtimeId, @@ -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 ); } @@ -187,7 +188,7 @@ export default async function ( return done( null, - mwpPrefix + code + referenceMissingDeps(this, resourcePath, meta) + mwpPrefix + code + getTrailingContent(this, resourcePath, meta) ); } @@ -202,7 +203,7 @@ export default async function ( return done( null, - code + referenceMissingDeps(this, resourcePath, meta), + code + getTrailingContent(this, resourcePath, meta), (map as unknown) as string ); } catch (err) { @@ -210,36 +211,42 @@ export default async function ( } } -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) {