feat: core-hmr handling & watch ignore

This commit is contained in:
Igor Randjelovic
2021-03-11 23:05:47 +01:00
parent 0e8336f1d9
commit 6cc0ce3d57
12 changed files with 238 additions and 2 deletions

View File

@ -0,0 +1,32 @@
import { relative } from "path";
import dedent from "ts-dedent";
// note: this will bail even if module.hot appears in a comment
const MODULE_HOT_RE = /module\.hot/
export default function loader(content: string, map: any) {
if (MODULE_HOT_RE.test(content)) {
// Code already handles HMR - we don't need to do anything
return this.callback(null, content, map)
}
const opts = this.getOptions();
const relativePath = relative(
opts.appPath ?? this.rootContext,
this.resourcePath
).replace(/\\/g, '/')
const hmrCode = this.hot
? dedent`
/* NATIVESCRIPT-HOT-LOADER */
if(module.hot && global._isModuleLoadedForUI && global._isModuleLoadedForUI("./${relativePath}")) {
module.hot.accept()
}
`
: ``;
const source = `${content}\n${hmrCode}`
this.callback(null, source, map)
}