From 00d072c72280d443aa6f076feee89ab013bd0036 Mon Sep 17 00:00:00 2001 From: Runjuu Date: Tue, 12 Jan 2021 20:40:38 +0800 Subject: [PATCH 1/2] feat: introduce `styleTagFactory` Used to customize the style tag. Fix #31 --- README.md | 27 +++++++++++++++++++++++---- src/core/base-plugin.ts | 20 +++++++++++++++++--- src/types.ts | 3 +++ 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index aec761c..4fc3231 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 ``; }, }), ... diff --git a/src/core/base-plugin.ts b/src/core/base-plugin.ts index 0c09a44..9dbb986 100644 --- a/src/core/base-plugin.ts +++ b/src/core/base-plugin.ts @@ -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 { @@ -17,6 +22,13 @@ export class BasePlugin { return this.config.replace || DEFAULT_REPLACE_CONFIG } + protected get styleTagFactory(): StyleTagFactory { + return ( + this.config.styleTagFactory || + (({ style }) => ``) + ) + } + constructor(protected readonly config: Config = {}) {} protected prepare({ assets }: Compilation) { @@ -75,8 +87,10 @@ export class BasePlugin { htmlFileName: string style: string }) { - const styleString = `` - const replaceValues = [styleString, this.replaceConfig.target] + const replaceValues = [ + this.styleTagFactory({ style }), + this.replaceConfig.target, + ] if (this.replaceConfig.position === 'after') { replaceValues.reverse() diff --git a/src/types.ts b/src/types.ts index 4b9660e..ffb8ca2 100644 --- a/src/types.ts +++ b/src/types.ts @@ -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 = { @@ -14,6 +16,7 @@ export interface Config { filter?(fileName: string): boolean leaveCSSFile?: boolean replace?: ReplaceConfig + styleTagFactory?: StyleTagFactory } export interface FileCache { From d6c94fc07027c85f924d3c1b347da8b3d7c3db41 Mon Sep 17 00:00:00 2001 From: Runjuu Date: Tue, 12 Jan 2021 22:13:53 +0800 Subject: [PATCH 2/2] release 1.10.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index d54576b..e36325f 100644 --- a/package.json +++ b/package.json @@ -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",