Skip to content

Commit 539bacb

Browse files
committed
feat: introduce styleTagFactory
Used to customize the style tag. Fix runjuu#31
1 parent a288bfa commit 539bacb

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed

README.md

+23-4
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ module.exports = {
5353
## Config
5454
```typescript
5555
interface Config {
56-
filter?(fileName: string): boolean
56+
filter?: (fileName: string) => boolean
57+
styleTagFactory?: (params: { style: string }) => string
5758
leaveCSSFile?: boolean
5859
replace?: {
5960
target: string
@@ -65,15 +66,33 @@ interface Config {
6566

6667
### filter(optional)
6768
```typescript
68-
filter?(fileName: string): boolean
69+
filter?: (fileName: string) => boolean
6970
```
7071
Return `true` to make current file internal, otherwise ignore current file. Include both css file and html file name.
72+
##### example
73+
```typescript
74+
...
75+
new HTMLInlineCSSWebpackPlugin({
76+
filter(fileName) {
77+
return fileName.includes('main');
78+
},
79+
}),
80+
...
81+
```
82+
83+
### styleTagFactory(optional)
84+
```typescript
85+
styleTagFactory?: (params: { style: string }) => string
86+
```
87+
88+
Used to customize the style tag.
89+
7190
##### example
7291
```typescript
7392
...
7493
new HTMLInlineCSSWebpackPlugin({
75-
filter(fileName) {
76-
return fileName.includes('main');
94+
styleTagFactory({ style }) {
95+
return `<style type="text/css">${style}</style>`;
7796
},
7897
}),
7998
...

src/core/base-plugin.ts

+17-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { Config, DEFAULT_REPLACE_CONFIG, FileCache } from '../types'
1+
import {
2+
Config,
3+
StyleTagFactory,
4+
DEFAULT_REPLACE_CONFIG,
5+
FileCache,
6+
} from '../types'
27
import { isCSS, escapeRegExp } from '../utils'
38

49
interface Asset {
@@ -17,6 +22,13 @@ export class BasePlugin {
1722
return this.config.replace || DEFAULT_REPLACE_CONFIG
1823
}
1924

25+
protected get styleTagFactory(): StyleTagFactory {
26+
return (
27+
this.config.styleTagFactory ||
28+
(({ style }) => `<style type="text/css">${style}</style>`)
29+
)
30+
}
31+
2032
constructor(protected readonly config: Config = {}) {}
2133

2234
protected prepare({ assets }: Compilation) {
@@ -75,8 +87,10 @@ export class BasePlugin {
7587
htmlFileName: string
7688
style: string
7789
}) {
78-
const styleString = `<style type="text/css">${style}</style>`
79-
const replaceValues = [styleString, this.replaceConfig.target]
90+
const replaceValues = [
91+
this.styleTagFactory({ style }),
92+
this.replaceConfig.target,
93+
]
8094

8195
if (this.replaceConfig.position === 'after') {
8296
replaceValues.reverse()

src/types.ts

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ export interface ReplaceConfig {
44
target: string
55
}
66

7+
export type StyleTagFactory = (params: { style: string }) => string
8+
79
export const TAP_KEY_PREFIX = 'html-inline-css-webpack-plugin'
810

911
export const DEFAULT_REPLACE_CONFIG: ReplaceConfig = {
@@ -14,6 +16,7 @@ export interface Config {
1416
filter?(fileName: string): boolean
1517
leaveCSSFile?: boolean
1618
replace?: ReplaceConfig
19+
styleTagFactory?: StyleTagFactory
1720
}
1821

1922
export interface FileCache {

0 commit comments

Comments
 (0)