-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathtemplate-loader.js
37 lines (33 loc) · 1.5 KB
/
template-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
/* eslint-env commonjs */
const loaderUtils = require(`loader-utils`);
const helpers = require(`./loader-helpers`);
// Used in non-HMR mode, do nothing
module.exports = (source) => source;
module.exports.pitch = function (request) {
const options = helpers.getOptions(this);
const moduleId = loaderUtils.stringifyRequest(this, `!!${request}`);
const elemName = helpers.getElemName(this.resourcePath, options);
if (!options.hot) {
return `module.exports = require(${moduleId});`;
}
// hot reloading of templates works by exporting a wrapped closure function that references templateFn
// when we receive a new module update, we swap templateFn reference to new template function
// this means components that use the template don't need to change their function reference
// we just need to call their .update()
return `
const templateModule = require(${moduleId});
const getTemplateFn = (mod) => mod.__esModule ? mod.template : mod;
let templateFn = getTemplateFn(templateModule);
module.hot.accept(${moduleId}, () => {
templateFn = getTemplateFn(require(${moduleId}))
const updatePanelElems = require('panel/hot/update-panel-elems');
updatePanelElems('${elemName}', elem => true);
});
const wrappedTemplateFn = function() {return templateFn.apply(this, arguments)};
if (templateModule.__esModule) {
module.exports = {...templateModule, __esModule: true, template: wrappedTemplateFn};
} else {
module.exports = wrappedTemplateFn;
}
`;
};