-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
Copy pathmodule-loader.js
59 lines (51 loc) · 1.84 KB
/
module-loader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const path = require('path')
const loaderUtils = require('loader-utils')
const apiPath = path.join(__dirname, 'api.js')
module.exports = function () {
const { activationUrl, ips, hmr } = loaderUtils.getOptions(this) || {}
const hmrCode = `
if (module.hot) {
module.hot.dispose(function() {
stopAnimation();
// wait for the module to install
setTimeout(() => {
// depressed warning: "unexpected require from disposed module"
module.hot.active = true;
compilation.ready(__webpack_require__(module.id));
module.hot.active = false;
}, 0);
});
module.hot.accept(function() {
// error handle
});
}
`.trim()
const refreshCode = `
window.onbeforeunload = function() {
stopAnimation();
}
`.trim()
return `
//############################ NOTICE ############################//
// //
// This is a placeholder module generated by //
// lazy-compile-webpack-plugin. //
// https://github.com/liximomo/lazy-compile-webpack-plugin //
// //
// The real content is in the corresponding hot-update file. //
// You won't see this after the page refresh. //
// //
//################################################################//
// @activationUrl ${activationUrl}
// modified from https://matthewrayfield.com/articles/animating-urls-with-javascript-and-emojis
var api = require(${loaderUtils.stringifyRequest(this, `!!${apiPath}`)});
var stopAnimation = api.startAnimation();
var compilation = api.compile(${JSON.stringify(ips)}, '${activationUrl}');
${hmr ? hmrCode : refreshCode}
module.exports = {
then(resolve) {
resolve(compilation)
}
}
`.trim()
}