Skip to content

feat: introduce styleTagFactory #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ module.exports = {
## Config
```typescript
interface Config {
filter?(fileName: string): boolean
filter?: (fileName: string) => boolean
styleTagFactory?: (params: { style: string }) => string
leaveCSSFile?: boolean
replace?: {
target: string
Expand All @@ -65,15 +66,33 @@ interface Config {

### filter(optional)
```typescript
filter?(fileName: string): boolean
filter?: (fileName: string) => boolean
```
Return `true` to make current file internal, otherwise ignore current file. Include both css file and html file name.
##### example
```typescript
...
new HTMLInlineCSSWebpackPlugin({
filter(fileName) {
return fileName.includes('main');
},
}),
...
```

### styleTagFactory(optional)
```typescript
styleTagFactory?: (params: { style: string }) => string
```

Used to customize the style tag.

##### example
```typescript
...
new HTMLInlineCSSWebpackPlugin({
filter(fileName) {
return fileName.includes('main');
styleTagFactory({ style }) {
return `<style type="text/css">${style}</style>`;
},
}),
...
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "html-inline-css-webpack-plugin",
"version": "1.9.1",
"version": "1.10.0",
"description": "☄️ A webpack plugin for convert external stylesheet to embedded stylesheet, aka document stylesheet",
"main": "./build/index.js",
"types": "./build/index.d.ts",
Expand Down
20 changes: 17 additions & 3 deletions src/core/base-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { Config, DEFAULT_REPLACE_CONFIG, FileCache } from '../types'
import {
Config,
StyleTagFactory,
DEFAULT_REPLACE_CONFIG,
FileCache,
} from '../types'
import { isCSS, escapeRegExp } from '../utils'

interface Asset {
Expand All @@ -17,6 +22,13 @@ export class BasePlugin {
return this.config.replace || DEFAULT_REPLACE_CONFIG
}

protected get styleTagFactory(): StyleTagFactory {
return (
this.config.styleTagFactory ||
(({ style }) => `<style type="text/css">${style}</style>`)
)
}

constructor(protected readonly config: Config = {}) {}

protected prepare({ assets }: Compilation) {
Expand Down Expand Up @@ -75,8 +87,10 @@ export class BasePlugin {
htmlFileName: string
style: string
}) {
const styleString = `<style type="text/css">${style}</style>`
const replaceValues = [styleString, this.replaceConfig.target]
const replaceValues = [
this.styleTagFactory({ style }),
this.replaceConfig.target,
]

if (this.replaceConfig.position === 'after') {
replaceValues.reverse()
Expand Down
3 changes: 3 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ export interface ReplaceConfig {
target: string
}

export type StyleTagFactory = (params: { style: string }) => string

export const TAP_KEY_PREFIX = 'html-inline-css-webpack-plugin'

export const DEFAULT_REPLACE_CONFIG: ReplaceConfig = {
Expand All @@ -14,6 +16,7 @@ export interface Config {
filter?(fileName: string): boolean
leaveCSSFile?: boolean
replace?: ReplaceConfig
styleTagFactory?: StyleTagFactory
}

export interface FileCache {
Expand Down