-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
28 lines (23 loc) · 867 Bytes
/
index.js
File metadata and controls
28 lines (23 loc) · 867 Bytes
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
const fs = require('fs');
function DynamicPublicPathPlugin(options) {
this.publicPath = options.publicPath;
this.outputPath = options.outputPath;
}
DynamicPublicPathPlugin.prototype.apply = function(compiler) {
compiler.plugin('done', params => {
Object.keys(params.compilation.assets)
.filter(name => name.includes('.js') && !name.includes('.js.map'))
.forEach(name => {
fs.readFile(`${__dirname}/${this.outputPath}/${name}`, (err, data) => {
if (err) throw err;
const newBundle = data
.toString()
.replace('__webpack_require__.p = ""', `__webpack_require__.p = ${this.publicPath}`);
fs.writeFile(`${__dirname}/${this.outputPath}/${name}`, newBundle, err => {
if (err) throw err
});
});
});
});
};
module.exports = DynamicPublicPathPlugin;