forked from typestruck/merochat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInlineStylePlugin.js
30 lines (23 loc) · 895 Bytes
/
InlineStylePlugin.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
import { resolve } from 'path';
import { readFileSync, writeFileSync } from 'fs';
function InlineStylePlugin(options = {}) {
this.options = options;
}
InlineStylePlugin.prototype.apply = function (compiler) {
let done = statsData => {
if (!statsData.hasErrors())
inline(this.options);
};
compiler.hooks.done.tap({ name: 'InlineStylePlugin' }, done);
};
function inline(options) {
for (let entry of options.files) {
let htmlPath = resolve(entry.htmlFile),
htmlContents = readFileSync(htmlPath, 'utf8'),
styleName = entry.styleFile.split('/').pop(),
styleContents = readFileSync(resolve(entry.styleFile), 'utf8').replaceAll('"', "'");
htmlContents = htmlContents.replace(`<% ${styleName} %>`, `${styleContents}`);
writeFileSync(htmlPath, htmlContents);
}
}
export default InlineStylePlugin;