Skip to content

Commit 5ab8ca7

Browse files
committed
chore: format code
1 parent 34f2970 commit 5ab8ca7

File tree

1 file changed

+48
-42
lines changed

1 file changed

+48
-42
lines changed

src/index.ts

+48-42
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import { Compiler, Configuration } from 'webpack';
1+
import { Compiler, Configuration } from 'webpack'
22

33
type File = {
44
[key: string]: string
5-
};
5+
}
66

77
type Asset = {
88
source(): string
99
size(): number
10-
};
10+
}
1111

1212
interface Compilation {
1313
assets: { [key: string]: Asset }
@@ -16,7 +16,7 @@ interface Compilation {
1616
interface ReplaceConfig {
1717
position?: 'before' | 'after'
1818
removeTarget?: boolean
19-
target: string,
19+
target: string
2020
leaveCssFile?: boolean
2121
}
2222

@@ -26,100 +26,106 @@ interface Config {
2626
}
2727

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

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

3837
if (replaceConfig.position === 'after') {
3938
replaceValues.reverse()
4039
}
4140

42-
return html.replace(replaceConfig.target, replaceValues.join(''));
41+
return html.replace(replaceConfig.target, replaceValues.join(''))
4342
}
4443

4544
static removeLinkTag(html: string, cssFileName: string) {
4645
return html.replace(
4746
new RegExp(`<link[^>]+href=['"]${cssFileName}['"][^>]+(>|\/>|><\/link>)`),
4847
'',
49-
);
48+
)
5049
}
5150

5251
static cleanUp(html: string, replaceConfig: ReplaceConfig) {
5352
return replaceConfig.removeTarget
5453
? html.replace(replaceConfig.target, '')
55-
: html;
54+
: html
5655
}
5756

58-
private css: File = {};
57+
private css: File = {}
5958

60-
private html: File = {};
59+
private html: File = {}
6160

6261
constructor(private readonly config: Config = {}) {}
6362

6463
private filter(fileName: string): boolean {
6564
if (typeof this.config.filter === 'function') {
66-
return this.config.filter(fileName);
65+
return this.config.filter(fileName)
6766
} else {
68-
return true;
67+
return true
6968
}
7069
}
7170

7271
private prepare({ assets }: Compilation) {
73-
const isCSS = is('css');
74-
const isHTML = is('html');
75-
const { replace: replaceConfig = DEFAULT_REPLACE_CONFIG } = this.config;
72+
const isCSS = is('css')
73+
const isHTML = is('html')
74+
const { replace: replaceConfig = DEFAULT_REPLACE_CONFIG } = this.config
7675

7776
Object.keys(assets).forEach((fileName) => {
7877
if (isCSS(fileName)) {
79-
const isCurrentFileNeedsToBeInlined = this.filter(fileName);
78+
const isCurrentFileNeedsToBeInlined = this.filter(fileName)
8079
if (isCurrentFileNeedsToBeInlined) {
81-
this.css[fileName] = assets[fileName].source();
80+
this.css[fileName] = assets[fileName].source()
8281
if (!replaceConfig.leaveCssFile) {
8382
delete assets[fileName]
8483
}
8584
}
8685
} else if (isHTML(fileName)) {
87-
this.html[fileName] = assets[fileName].source();
86+
this.html[fileName] = assets[fileName].source()
8887
}
89-
});
88+
})
9089
}
9190

9291
private process({ assets }: Compilation, { output }: Configuration) {
93-
const publicPath = (output && output.publicPath) || '';
94-
const { replace: replaceConfig = DEFAULT_REPLACE_CONFIG } = this.config;
92+
const publicPath = (output && output.publicPath) || ''
93+
const { replace: replaceConfig = DEFAULT_REPLACE_CONFIG } = this.config
9594

9695
Object.keys(this.html).forEach((htmlFileName) => {
97-
let html = this.html[htmlFileName];
96+
let html = this.html[htmlFileName]
9897

9998
Object.keys(this.css).forEach((key) => {
100-
html = Plugin.addStyle(html, this.css[key], replaceConfig);
101-
html = Plugin.removeLinkTag(html, publicPath + key);
102-
});
99+
html = Plugin.addStyle(html, this.css[key], replaceConfig)
100+
html = Plugin.removeLinkTag(html, publicPath + key)
101+
})
103102

104-
html = Plugin.cleanUp(html, replaceConfig);
103+
html = Plugin.cleanUp(html, replaceConfig)
105104

106105
assets[htmlFileName] = {
107-
source() { return html },
108-
size() { return html.length },
109-
};
110-
});
106+
source() {
107+
return html
108+
},
109+
size() {
110+
return html.length
111+
},
112+
}
113+
})
111114
}
112115

113116
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-
});
117+
compiler.hooks.emit.tapAsync(
118+
'html-inline-css-webpack-plugin',
119+
(compilation: Compilation, callback: () => void) => {
120+
this.prepare(compilation)
121+
this.process(compilation, compiler.options)
122+
callback()
123+
},
124+
)
119125
}
120126
}
121127

122128
function is(filenameExtension: string) {
123-
const reg = new RegExp(`\.${filenameExtension}$`);
124-
return (fileName: string) => reg.test(fileName);
129+
const reg = new RegExp(`\.${filenameExtension}$`)
130+
return (fileName: string) => reg.test(fileName)
125131
}

0 commit comments

Comments
 (0)