Skip to content

Commit b5625d5

Browse files
committed
refactor: native filter
1 parent 8841807 commit b5625d5

File tree

5 files changed

+27
-24
lines changed

5 files changed

+27
-24
lines changed

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@
6868
"dependencies": {
6969
"lightningcss": "^1.29.3",
7070
"magic-string": "^0.30.17",
71-
"unplugin": "^2.3.2",
72-
"unplugin-utils": "^0.2.4"
71+
"unplugin": "^2.3.2"
7372
},
7473
"devDependencies": {
7574
"@sxzz/eslint-config": "^6.1.2",
@@ -93,8 +92,8 @@
9392
"engines": {
9493
"node": ">=18.12.0"
9594
},
96-
"prettier": "@sxzz/prettier-config",
9795
"resolutions": {
9896
"@types/rollup-plugin-css-only>rollup": "-"
99-
}
97+
},
98+
"prettier": "@sxzz/prettier-config"
10099
}

pnpm-lock.yaml

Lines changed: 0 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/core/options.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { TransformOptions } from 'lightningcss'
2-
import type { FilterPattern } from 'unplugin-utils'
2+
import type { FilterPattern } from 'unplugin'
33

44
export type Options = {
55
include?: FilterPattern

src/index.ts

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import path from 'node:path'
22
import { createUnplugin, type UnpluginInstance } from 'unplugin'
3-
import { createFilter } from 'unplugin-utils'
43
import { resolveOption, type Options } from './core/options'
54
import { transformCss, transformCssModule } from './core/transform'
65

76
const plugin: UnpluginInstance<Options | undefined, false> = createUnplugin(
87
(rawOptions = {}) => {
98
const options = resolveOption(rawOptions)
10-
const filter = createFilter(options.include, options.exclude)
119

1210
const transformedFiles = new Map<string, string>()
1311

@@ -16,27 +14,37 @@ const plugin: UnpluginInstance<Options | undefined, false> = createUnplugin(
1614
name,
1715
enforce: options.enforce,
1816

19-
transformInclude(id) {
20-
return filter(id)
21-
},
22-
2317
resolveId(id, importer) {
2418
if (id.endsWith('.module_built.css')) return id
2519
if (id.endsWith('.module.css')) {
2620
return `${path.resolve(path.dirname(importer || ''), id)}?css_module`
2721
}
2822
},
2923

30-
transform(code, id) {
31-
return transformCss(id, code, options.options)
24+
transform: {
25+
filter: {
26+
id: {
27+
include: options.include,
28+
exclude: options.exclude,
29+
},
30+
},
31+
handler(code, id) {
32+
return transformCss(id, code, options.options)
33+
},
3234
},
3335

34-
async load(id) {
35-
if (id.endsWith('.module_built.css')) {
36-
const code = transformedFiles.get(id)!
37-
return { id, code }
38-
}
39-
if (id.endsWith('?css_module')) {
36+
load: {
37+
filter: {
38+
id: {
39+
include: [/\.module_built\.css$/, /\?css_module$/],
40+
},
41+
},
42+
async handler(id) {
43+
if (id.endsWith('.module_built.css')) {
44+
const code = transformedFiles.get(id)!
45+
return { id, code }
46+
}
47+
4048
const {
4149
code,
4250
map,
@@ -48,7 +56,7 @@ const plugin: UnpluginInstance<Options | undefined, false> = createUnplugin(
4856
code: `import "${compiledId}";\n${exports}`,
4957
map,
5058
}
51-
}
59+
},
5260
},
5361
}
5462
},

tsdown.config.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { defineConfig } from 'tsdown'
22

33
export default defineConfig({
44
entry: ['./src/*.ts'],
5-
format: ['esm'],
65
target: 'node18.12',
76
clean: true,
87
dts: true,

0 commit comments

Comments
 (0)