Skip to content
This repository was archived by the owner on Aug 7, 2021. It is now read-only.

Commit ebc7029

Browse files
author
vakrilov
committed
feat: universal hmr loader
1 parent 14145be commit ebc7029

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

hmr/hot-loader.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { loader } from "webpack";
2+
import { convertToUnixPath } from "../lib/utils";
3+
import { extname } from "path";
4+
5+
const extMap = {
6+
".css" : "style",
7+
".scss" : "style",
8+
".less" : "style",
9+
".js" : "script",
10+
".ts" : "script",
11+
".xml" : "markup",
12+
".html" : "markup",
13+
}
14+
15+
const loader: loader.Loader = function (source, map) {
16+
const moduleRelativePath = this.resourcePath.replace(this.rootContext, ".");
17+
const modulePath = convertToUnixPath(moduleRelativePath);
18+
const ext = extname(modulePath).toLowerCase();
19+
const typeStyle = extMap[ext] || "unknown";
20+
21+
const hotCode = `
22+
if (module.hot && global._shouldAutoAcceptModule && global._shouldAutoAcceptModule(module.id)) {
23+
console.log("AUTO ACCEPT MODULE: ", module.id, '${modulePath}')
24+
module.hot.accept();
25+
module.hot.dispose(() => {
26+
global.hmrRefresh({ type: '${typeStyle}', path: '${modulePath}' });
27+
})
28+
}`;
29+
30+
this.callback(null, `${source};${hotCode}`, map);
31+
};
32+
33+
export default loader;
34+
35+
36+

0 commit comments

Comments
 (0)