-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
100 lines (84 loc) · 3.3 KB
/
index.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
'use strict';
// loader-utils可以解析webpack配置文件中loader传入的参数
const loaderUtils = require('loader-utils');
const fs = require('fs')
const path = require('path');
module.exports = function (source, map) {
if (this.cacheable) {
this.cacheable();
}
// Loader Options
const options = loaderUtils.getOptions(this) || {};
let enable = options.enable;
if (enable === "undefined") {
enable = true;
}
if (!enable) {
return source;
}
// inject `module.hot.accept()` support js hmr
let jsHotAccept = options.jsHotAccept || true;
let js_hmr = jsHotAccept ? 'module.hot.accept();' + "\n" : '';
if (jsHotAccept) {
// just inject js `module.hot.accept()` support js hmr
let onlyJS = options.onlyJS;
if (onlyJS === "undefined") {
onlyJS = false;
}
if (onlyJS) {
return '' +
source + "\n" +
"\n" +
'if (module.hot) {' +
js_hmr +
'}' + "\n";
}
}
// The js relative root path relative project root path, like: client/js/
let jsRootDir = options.jsRootDir;
// The template relative root path relative project root path, like: client/view/
let templateRootDir = options.templateRootDir;
// Skip if the plugin configuration didn't set `jsRootDir` or `templateRootDir`
if (!jsRootDir || !templateRootDir) {
return source;
}
// js full path, like: D:\webpack_express\client\js\home\index.js
const jsFullPath = this.resourcePath;
const context = options.context || this.rootContext || (this.options && this.options.context);
// The js relative path, like: client/js/home/
let jsPath = loaderUtils.interpolateName(this, '[path]', {
context
});
// The template name, like: index.hbs
let name = loaderUtils.interpolateName(this, options.name, {
context
});
// The js relative path 2, not equal jsPath, like: client\js\home\
let jsPath2 = jsPath.replace(/\//g, '\\');
// The js releative project path, like: ../../../
let jsRelativeRoot = jsPath.replace(/.*?\//g, '..\/');
// The project root path, like: D:\webpack_express\
let projectRoot = jsFullPath.substr(0, jsFullPath.indexOf(jsPath2));
// The template file name, relative project root path, like: client/view/home/index.hbs
let templateFileName = templateRootDir + jsPath.replace(jsRootDir, '') + name;
let nameFormat = options.nameFormat || undefined;
if (nameFormat && typeof nameFormat === 'function') {
templateFileName = nameFormat(templateFileName);
}
// The template file name relative the js, like: ../../../client/view/home/index.hbs
let relativeTemplateFileName = jsRelativeRoot + templateFileName;
// Determines whether the template file exists, if not, will not modify.
if (!fs.existsSync(path.resolve(projectRoot, templateFileName))) {
return source;
}
return '' +
source + "\n" +
"\n" +
'if (module.hot) {' +
'require("' + relativeTemplateFileName + '");' + "\n" +
'module.hot.accept("' + relativeTemplateFileName + '", function() {' + "\n" +
' window.location.reload();' + "\n" +
'});' + "\n" +
js_hmr +
'}' + "\n";
};