forked from drogjh/webpack-runtime-public-path-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
41 lines (36 loc) · 1.38 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
/**
* Created by GROOT on 2017/4/11.
*/
function RuntimePublicPath(options) {
this.options = options;
this._name = 'WebpackRuntimePublicPathPlugin';
}
function buf(path, source) {
var buf = [];
buf.push(source);
buf.push('');
buf.push('// Dynamic assets path override (webpack-runtime-public-path-plugin)');
buf.push('__webpack_require__.p = (' + path + ') || __webpack_require__.p;');
return buf.join('\n');
}
RuntimePublicPath.prototype.apply = function (compiler) {
var runtimePublicPathStr = this.options && this.options.runtimePublicPath;
if (!runtimePublicPathStr) {
console.error('RuntimePublicPath: no output.runtimePublicPath is specified. This plugin will do nothing.');
return;
}
if (compiler.hooks && compiler.hooks.thisCompilation) {
compiler.hooks.thisCompilation.tap(this._name, function (compilation) {
compilation.mainTemplate.hooks.requireExtensions.tap('require-extensions', function (source, chunk, hash) {
return buf(runtimePublicPathStr, source)
});
});
} else {
compiler.plugin('this-compilation', function (compilation) {
compilation.mainTemplate.plugin('require-extensions', function (source, chunk, hash) {
return buf(runtimePublicPathStr, source)
});
});
}
};
module.exports = RuntimePublicPath;