Skip to content

Commit 4f15081

Browse files
committed
Fix: buffer replace issue
1 parent d630a4e commit 4f15081

File tree

4 files changed

+68
-49
lines changed

4 files changed

+68
-49
lines changed

build/index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { Compiler } from 'webpack';
1+
import { Compiler } from "webpack";
22
interface ReplaceConfig {
3-
position?: 'before' | 'after';
3+
position?: "before" | "after";
44
removeTarget?: boolean;
55
target: string;
66
leaveCssFile?: boolean;

build/index.js

Lines changed: 19 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,62 @@
1-
import { Compiler, Configuration } from 'webpack';
1+
import { Compiler, Configuration } from "webpack";
22

33
type File = {
4-
[key: string]: string
4+
[key: string]: string;
55
};
66

77
type Asset = {
8-
source(): string
9-
size(): number
8+
source(): string;
9+
size(): number;
1010
};
1111

1212
interface Compilation {
13-
assets: { [key: string]: Asset }
13+
assets: { [key: string]: Asset };
1414
}
1515

1616
interface ReplaceConfig {
17-
position?: 'before' | 'after'
18-
removeTarget?: boolean
19-
target: string,
20-
leaveCssFile?: boolean
17+
position?: "before" | "after";
18+
removeTarget?: boolean;
19+
target: string;
20+
leaveCssFile?: boolean;
2121
}
2222

2323
interface Config {
24-
filter?(fileName: string): boolean
25-
replace?: ReplaceConfig
24+
filter?(fileName: string): boolean;
25+
replace?: ReplaceConfig;
2626
}
2727

2828
const DEFAULT_REPLACE_CONFIG: ReplaceConfig = {
29-
target: '</head>'
29+
target: "</head>"
3030
};
3131

32-
export default class Plugin
33-
{
32+
export default class Plugin {
3433
static addStyle(html: string, style: string, replaceConfig: ReplaceConfig) {
3534
const styleString = `<style type="text/css">${style}</style>`;
3635
const replaceValues = [styleString, replaceConfig.target];
3736

38-
if (replaceConfig.position === 'after') {
39-
replaceValues.reverse()
37+
if (replaceConfig.position === "after") {
38+
replaceValues.reverse();
4039
}
4140

42-
return html.replace(replaceConfig.target, replaceValues.join(''));
41+
html = String(html);
42+
43+
return html.replace(replaceConfig.target, replaceValues.join(""));
4344
}
4445

4546
static removeLinkTag(html: string, cssFileName: string) {
47+
html = String(html);
48+
4649
return html.replace(
4750
new RegExp(`<link[^>]+href=['"]${cssFileName}['"][^>]+(>|\/>|><\/link>)`),
48-
'',
51+
""
4952
);
5053
}
5154

5255
static cleanUp(html: string, replaceConfig: ReplaceConfig) {
56+
html = String(html);
57+
5358
return replaceConfig.removeTarget
54-
? html.replace(replaceConfig.target, '')
59+
? html.replace(replaceConfig.target, "")
5560
: html;
5661
}
5762

@@ -62,25 +67,25 @@ export default class Plugin
6267
constructor(private readonly config: Config = {}) {}
6368

6469
private filter(fileName: string): boolean {
65-
if (typeof this.config.filter === 'function') {
70+
if (typeof this.config.filter === "function") {
6671
return this.config.filter(fileName);
6772
} else {
6873
return true;
6974
}
7075
}
7176

7277
private prepare({ assets }: Compilation) {
73-
const isCSS = is('css');
74-
const isHTML = is('html');
78+
const isCSS = is("css");
79+
const isHTML = is("html");
7580
const { replace: replaceConfig = DEFAULT_REPLACE_CONFIG } = this.config;
7681

77-
Object.keys(assets).forEach((fileName) => {
82+
Object.keys(assets).forEach(fileName => {
7883
if (isCSS(fileName)) {
7984
const isCurrentFileNeedsToBeInlined = this.filter(fileName);
8085
if (isCurrentFileNeedsToBeInlined) {
8186
this.css[fileName] = assets[fileName].source();
8287
if (!replaceConfig.leaveCssFile) {
83-
delete assets[fileName]
88+
delete assets[fileName];
8489
}
8590
}
8691
} else if (isHTML(fileName)) {
@@ -90,32 +95,39 @@ export default class Plugin
9095
}
9196

9297
private process({ assets }: Compilation, { output }: Configuration) {
93-
const publicPath = (output && output.publicPath) || '';
98+
const publicPath = (output && output.publicPath) || "";
9499
const { replace: replaceConfig = DEFAULT_REPLACE_CONFIG } = this.config;
95100

96-
Object.keys(this.html).forEach((htmlFileName) => {
101+
Object.keys(this.html).forEach(htmlFileName => {
97102
let html = this.html[htmlFileName];
98103

99-
Object.keys(this.css).forEach((key) => {
104+
Object.keys(this.css).forEach(key => {
100105
html = Plugin.addStyle(html, this.css[key], replaceConfig);
101106
html = Plugin.removeLinkTag(html, publicPath + key);
102107
});
103108

104109
html = Plugin.cleanUp(html, replaceConfig);
105110

106111
assets[htmlFileName] = {
107-
source() { return html },
108-
size() { return html.length },
112+
source() {
113+
return html;
114+
},
115+
size() {
116+
return html.length;
117+
}
109118
};
110119
});
111120
}
112121

113122
apply(compiler: Compiler) {
114-
compiler.hooks.emit.tapAsync('html-inline-css-webpack-plugin', (compilation: Compilation, callback: () => void) => {
115-
this.prepare(compilation);
116-
this.process(compilation, compiler.options);
117-
callback();
118-
});
123+
compiler.hooks.emit.tapAsync(
124+
"html-inline-css-webpack-plugin",
125+
(compilation: Compilation, callback: () => void) => {
126+
this.prepare(compilation);
127+
this.process(compilation, compiler.options);
128+
callback();
129+
}
130+
);
119131
}
120132
}
121133

0 commit comments

Comments
 (0)