|
1 | 1 | import { createUnplugin } from 'unplugin' |
2 | | -import { Context, cssHandler, preProcessJs, preProcessRawCode, vueHandler } from '@tailwindcss-mangle/core' |
3 | | -import type { MangleUserConfig } from '@tailwindcss-mangle/config' |
4 | | -import MagicString from 'magic-string' |
5 | | -import { pluginName } from '@/constants' |
| 2 | +import factory from './factory' |
6 | 3 |
|
7 | | -export const unplugin = createUnplugin((options?: MangleUserConfig) => { |
8 | | - const ctx = new Context() |
| 4 | +const unplugin = createUnplugin(factory) |
9 | 5 |
|
10 | | - return { |
11 | | - name: pluginName, |
12 | | - enforce: 'pre', |
13 | | - |
14 | | - async buildStart() { |
15 | | - await ctx.initConfig({ |
16 | | - mangleOptions: options, |
17 | | - }) |
18 | | - }, |
19 | | - transformInclude(id) { |
20 | | - return !id.includes('node_modules') |
21 | | - }, |
22 | | - async transform(code, id) { |
23 | | - const s = new MagicString(code) |
24 | | - // 直接忽略 css 文件,因为此时 tailwindcss 还没有展开 |
25 | | - if (/\.[jt]sx?$/.test(id)) { |
26 | | - return preProcessJs({ |
27 | | - code: s, |
28 | | - ctx, |
29 | | - id, |
30 | | - }) |
31 | | - } |
32 | | - else if (/\.vue/.test(id)) { |
33 | | - return vueHandler(code, { |
34 | | - ctx, |
35 | | - }) |
36 | | - } |
37 | | - else if (/\.css/.test(id)) { |
38 | | - const { css } = await cssHandler(code, { ctx, file: id }) |
39 | | - return css |
40 | | - } |
41 | | - else { |
42 | | - return preProcessRawCode({ |
43 | | - code, |
44 | | - ctx, |
45 | | - id, |
46 | | - }) |
47 | | - } |
48 | | - }, |
49 | | - // vite: { |
50 | | - // generateBundle: { |
51 | | - // async handler(options, bundle) { |
52 | | - // const groupedEntries = getGroupedEntries(Object.entries(bundle)) |
53 | | - |
54 | | - // if (Array.isArray(groupedEntries.css) && groupedEntries.css.length > 0) { |
55 | | - // for (let i = 0; i < groupedEntries.css.length; i++) { |
56 | | - // const [file, cssSource] = groupedEntries.css[i] as [string, OutputAsset] |
57 | | - |
58 | | - // const { css } = await cssHandler(cssSource.source.toString(), { |
59 | | - // file, |
60 | | - // ctx, |
61 | | - // }) |
62 | | - // cssSource.source = css |
63 | | - // } |
64 | | - // } |
65 | | - // }, |
66 | | - // }, |
67 | | - // }, |
68 | | - // webpack(compiler) { |
69 | | - // const { Compilation, sources } = compiler.webpack |
70 | | - // const { ConcatSource } = sources |
71 | | - |
72 | | - // compiler.hooks.compilation.tap(pluginName, (compilation) => { |
73 | | - // compilation.hooks.processAssets.tapPromise( |
74 | | - // { |
75 | | - // name: pluginName, |
76 | | - // stage: Compilation.PROCESS_ASSETS_STAGE_SUMMARIZE, |
77 | | - // }, |
78 | | - // async (assets) => { |
79 | | - // const groupedEntries = getGroupedEntries(Object.entries(assets)) |
80 | | - |
81 | | - // if (groupedEntries.js.length > 0) { |
82 | | - // for (let i = 0; i < groupedEntries.js.length; i++) { |
83 | | - // const [file, chunk] = groupedEntries.js[i] |
84 | | - |
85 | | - // const code = jsHandler(chunk.source().toString(), { |
86 | | - // ctx, |
87 | | - // }).code |
88 | | - // if (code) { |
89 | | - // const source = new ConcatSource(code) |
90 | | - // compilation.updateAsset(file, source) |
91 | | - // } |
92 | | - // } |
93 | | - // } |
94 | | - |
95 | | - // if (groupedEntries.css.length > 0) { |
96 | | - // for (let i = 0; i < groupedEntries.css.length; i++) { |
97 | | - // const [file, cssSource] = groupedEntries.css[i] |
98 | | - |
99 | | - // const { css } = await cssHandler(cssSource.source().toString(), { |
100 | | - // file, |
101 | | - // ctx, |
102 | | - // }) |
103 | | - |
104 | | - // const source = new ConcatSource(css) |
105 | | - |
106 | | - // compilation.updateAsset(file, source) |
107 | | - // } |
108 | | - // } |
109 | | - |
110 | | - // if (groupedEntries.html.length > 0) { |
111 | | - // for (let i = 0; i < groupedEntries.html.length; i++) { |
112 | | - // const [file, asset] = groupedEntries.html[i] |
113 | | - |
114 | | - // const html = htmlHandler(asset.source().toString(), { |
115 | | - // ctx, |
116 | | - // }) |
117 | | - // const source = new ConcatSource(html) |
118 | | - // compilation.updateAsset(file, source) |
119 | | - // } |
120 | | - // } |
121 | | - // }, |
122 | | - // ) |
123 | | - // }) |
124 | | - // }, |
125 | | - // async writeBundle() { |
126 | | - // if (ctx.options.classMapOutput?.enable) { |
127 | | - // const opts = ctx.options.classMapOutput as Required<ClassMapOutputOptions> |
128 | | - // const entries = Object.entries(ctx.classGenerator.newClassMap) |
129 | | - // if (entries.length > 0 && opts) { |
130 | | - // await ensureDir(dirname(opts.filename)) |
131 | | - // const output = JSON.stringify( |
132 | | - // entries.map((x) => { |
133 | | - // return { |
134 | | - // origin: x[0], |
135 | | - // replacement: x[1].name, |
136 | | - // usedBy: [...x[1].usedBy], |
137 | | - // } |
138 | | - // }), |
139 | | - // null, |
140 | | - // opts.loose ? 2 : 0, |
141 | | - // ) |
142 | | - // await fs.writeFile(opts.filename, output, 'utf8') |
143 | | - // console.log(`✨ ${opts.filename} generated!`) |
144 | | - // } |
145 | | - // } |
146 | | - // }, |
147 | | - } |
148 | | -}) |
| 6 | +export default unplugin |
0 commit comments