Skip to content

Commit d355a0f

Browse files
committed
feat: add html/js/css HandlerOptions
1 parent 2b2da0a commit d355a0f

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

packages/unplugin-tailwindcss-mangle/src/index.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import path from 'path'
99
import fs from 'fs'
1010
import { getOptions } from './options'
1111

12+
export { defaultMangleClassFilter } from 'tailwindcss-mangle-shared'
13+
1214
// cache map
1315
const outputCachedMap = new Map<
1416
string,
@@ -20,7 +22,7 @@ const outputCachedMap = new Map<
2022
>()
2123

2224
export const unplugin = createUnplugin((options: Options | undefined = {}, meta) => {
23-
const { classGenerator, getCachedClassSet, isInclude, classMapOutputOptions } = getOptions(options)
25+
const { classGenerator, getCachedClassSet, isInclude, classMapOutputOptions, htmlHandlerOptions, jsHandlerOptions, cssHandlerOptions } = getOptions(options)
2426

2527
return {
2628
name: pluginName,
@@ -39,6 +41,7 @@ export const unplugin = createUnplugin((options: Options | undefined = {}, meta)
3941
const [file, asset] = groupedEntries.html[i] as [string, OutputAsset]
4042
if (isInclude(file)) {
4143
asset.source = htmlHandler(asset.source.toString(), {
44+
...htmlHandlerOptions,
4245
classGenerator,
4346
runtimeSet
4447
})
@@ -50,6 +53,7 @@ export const unplugin = createUnplugin((options: Options | undefined = {}, meta)
5053
const [file, chunk] = groupedEntries.js[i] as [string, OutputChunk]
5154
if (isInclude(file)) {
5255
const code = jsHandler(chunk.code, {
56+
...jsHandlerOptions,
5357
runtimeSet,
5458
classGenerator
5559
}).code
@@ -65,6 +69,7 @@ export const unplugin = createUnplugin((options: Options | undefined = {}, meta)
6569
const [file, css] = groupedEntries.css[i] as [string, OutputAsset]
6670
if (isInclude(file)) {
6771
css.source = cssHandler(css.source.toString(), {
72+
...cssHandlerOptions,
6873
classGenerator,
6974
runtimeSet
7075
})
@@ -168,6 +173,7 @@ export const unplugin = createUnplugin((options: Options | undefined = {}, meta)
168173
const [file, asset] = groupedEntries.html[i]
169174
if (isInclude(file)) {
170175
const html = htmlHandler(asset.source().toString(), {
176+
...htmlHandlerOptions,
171177
classGenerator,
172178
runtimeSet
173179
})
@@ -182,6 +188,7 @@ export const unplugin = createUnplugin((options: Options | undefined = {}, meta)
182188
const [file, chunk] = groupedEntries.js[i]
183189
if (isInclude(file)) {
184190
const code = jsHandler(chunk.source().toString(), {
191+
...jsHandlerOptions,
185192
runtimeSet,
186193
classGenerator
187194
}).code
@@ -198,6 +205,7 @@ export const unplugin = createUnplugin((options: Options | undefined = {}, meta)
198205
const [file, css] = groupedEntries.css[i]
199206
if (isInclude(file)) {
200207
const newCss = cssHandler(css.source().toString(), {
208+
...cssHandlerOptions,
201209
classGenerator,
202210
runtimeSet
203211
})
@@ -212,6 +220,7 @@ export const unplugin = createUnplugin((options: Options | undefined = {}, meta)
212220
html.forEach((asset, file) => {
213221
if (isInclude(file)) {
214222
const html = htmlHandler((asset as sources.Source).source().toString(), {
223+
...htmlHandlerOptions,
215224
classGenerator,
216225
runtimeSet
217226
})
@@ -226,6 +235,7 @@ export const unplugin = createUnplugin((options: Options | undefined = {}, meta)
226235
if (isInclude(file)) {
227236
const rawCode = (chunk as sources.Source).source().toString()
228237
const code = jsHandler(rawCode, {
238+
...jsHandlerOptions,
229239
runtimeSet,
230240
classGenerator
231241
}).code
@@ -241,6 +251,7 @@ export const unplugin = createUnplugin((options: Options | undefined = {}, meta)
241251
css.forEach((style, file) => {
242252
if (isInclude(file)) {
243253
const newCss = cssHandler((style as sources.Source).source().toString(), {
254+
...cssHandlerOptions,
244255
classGenerator,
245256
runtimeSet
246257
})

packages/unplugin-tailwindcss-mangle/src/options.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { Options, ClassSetOutputOptions, ClassMapOutputOptions } from './types'
22
import { TailwindcssPatcher } from 'tailwindcss-patch'
33
import { ClassGenerator } from 'tailwindcss-mangle-core'
4+
import type { IHtmlHandlerOptions, IJsHandlerOptions, ICssHandlerOptions } from 'tailwindcss-mangle-core'
45
import { createGlobMatcher, defaultMangleClassFilter, cacheDump } from './utils'
56

67
export function getOptions(options: Options | undefined = {}) {
@@ -59,6 +60,9 @@ export function getOptions(options: Options | undefined = {}) {
5960
isInclude,
6061
classSetOutputOptions,
6162
classMapOutputOptions,
62-
twPatcher
63+
twPatcher,
64+
jsHandlerOptions: <IJsHandlerOptions>(options.jsHandlerOptions ?? {}),
65+
htmlHandlerOptions: <IHtmlHandlerOptions>(options.htmlHandlerOptions ?? {}),
66+
cssHandlerOptions: <ICssHandlerOptions>(options.cssHandlerOptions ?? {})
6367
}
6468
}

packages/unplugin-tailwindcss-mangle/src/types.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import type { ClassGenerator } from 'tailwindcss-mangle-core'
1+
import type {
2+
ClassGenerator,
3+
IHtmlHandlerOptions, // as InternalHtmlHandlerOptions,
4+
IJsHandlerOptions, // as InternalJsHandlerOptions,
5+
ICssHandlerOptions // as InternalCssHandlerOptions
6+
} from 'tailwindcss-mangle-core'
27

38
export interface IClassGeneratorOptions {
49
reserveClassName?: (string | RegExp)[]
@@ -15,10 +20,6 @@ export interface IHandlerOptions {
1520
classGenerator: ClassGenerator
1621
}
1722

18-
export interface ICssHandlerOptions extends IHandlerOptions {
19-
scene?: 'loader' | 'process'
20-
}
21-
2223
export interface ClassSetOutputOptions {
2324
filename: string
2425
dir?: string
@@ -36,4 +37,7 @@ export interface Options {
3637
include?: string[]
3738
classSetOutput?: boolean | ClassSetOutputOptions
3839
classMapOutput?: boolean | ClassMapOutputOptions
40+
htmlHandlerOptions?: IHtmlHandlerOptions
41+
jsHandlerOptions?: IJsHandlerOptions
42+
cssHandlerOptions?: ICssHandlerOptions
3943
}

0 commit comments

Comments
 (0)