-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathInlineResourcePlugin.js
More file actions
30 lines (23 loc) · 933 Bytes
/
InlineResourcePlugin.js
File metadata and controls
30 lines (23 loc) · 933 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
29
30
import { resolve } from 'path';
import { readFileSync, writeFileSync } from 'fs';
function InlineResourcePlugin(options = {}) {
this.options = options;
}
InlineResourcePlugin.prototype.apply = function (compiler) {
let done = statsData => {
if (!statsData.hasErrors())
inline(this.options);
};
compiler.hooks.done.tap({ name: 'InlineResourcePlugin' }, done);
};
function inline(options) {
for (let entry of options.files) {
let htmlPath = resolve(entry.htmlFile),
htmlContents = readFileSync(htmlPath, 'utf8'),
resourceName = entry.resourceFile.split('/').pop(),
resourceContents = readFileSync(resolve(entry.resourceFile), 'utf8').replaceAll('"', "'");
htmlContents = htmlContents.replace(`"666 ${resourceName} 666"`, `\`${resourceContents}\``);
writeFileSync(htmlPath, htmlContents);
}
}
export default InlineResourcePlugin;