|
1 | 1 | import { createUnplugin } from 'unplugin' |
2 | | -import { setGlobalPrefix } from 'baiwusanyu-utils' |
| 2 | +import { normalizePath, setGlobalPrefix } from 'baiwusanyu-utils' |
3 | 3 | import MagicString from 'magic-string' |
4 | 4 | import { NAME } from '@unplugin-vue-ce/utils' |
5 | 5 | import { injectVueRuntime } from './src/inject/inject-vue-runtime' |
6 | 6 | import { atomicCSSPreset, virtualTailwind, virtualUno } from './src/atomic-css' |
7 | 7 |
|
8 | 8 | export const unVueCESubStyle = (): any => { |
9 | 9 | setGlobalPrefix(`[${NAME}]:`) |
| 10 | + // just vite |
10 | 11 | return [ |
11 | 12 | { |
12 | 13 | name: `${NAME}:sub-style:pre`, |
13 | 14 | enforce: 'pre', |
14 | | - resolveId(id: string) { |
15 | | - if (atomicCSSPreset[id as keyof typeof atomicCSSPreset]) |
16 | | - return `\0${id}` |
| 15 | + transformInclude(id: string) { |
| 16 | + return !id.endsWith('.html') |
17 | 17 | }, |
18 | | - load(id: string) { |
19 | | - if (id === `\0${virtualTailwind}` || id === `\0${virtualUno}`) { |
20 | | - return { |
21 | | - code: '', |
| 18 | + vite: { |
| 19 | + resolveId(id: string) { |
| 20 | + if (atomicCSSPreset[id as keyof typeof atomicCSSPreset]) |
| 21 | + return `\0${id}` |
| 22 | + }, |
| 23 | + load(id: string) { |
| 24 | + if (id === `\0${virtualTailwind}` || id === `\0${virtualUno}`) { |
| 25 | + return { |
| 26 | + code: '', |
| 27 | + } |
22 | 28 | } |
23 | | - } |
24 | | - }, |
25 | | - transformInclude() { |
26 | | - return true |
27 | | - }, |
28 | | - async transform(code: string, id: string) { |
29 | | - const mgcStr = new MagicString(code) |
30 | | - if (id.endsWith('.vue') && code.includes(virtualTailwind)) |
31 | | - mgcStr.prependRight(mgcStr.length(), atomicCSSPreset[virtualTailwind]) |
| 29 | + }, |
| 30 | + transform(code: string, id: string) { |
| 31 | + const mgcStr = new MagicString(code) |
| 32 | + if (id.endsWith('.vue') && code.includes(virtualTailwind)) |
| 33 | + mgcStr.prependRight(mgcStr.length(), atomicCSSPreset[virtualTailwind]) |
32 | 34 |
|
33 | | - if (id.endsWith('.vue') && code.includes(virtualUno)) |
34 | | - mgcStr.prependRight(mgcStr.length(), atomicCSSPreset[virtualUno]) |
| 35 | + if (id.endsWith('.vue') && code.includes(virtualUno)) |
| 36 | + mgcStr.prependRight(mgcStr.length(), atomicCSSPreset[virtualUno]) |
35 | 37 |
|
36 | | - return { |
37 | | - code: mgcStr.toString(), |
38 | | - get map() { |
39 | | - return mgcStr.generateMap({ |
40 | | - source: id, |
41 | | - includeContent: true, |
42 | | - hires: true, |
43 | | - }) |
44 | | - }, |
45 | | - } |
| 38 | + return { |
| 39 | + code: mgcStr.toString(), |
| 40 | + get map() { |
| 41 | + return mgcStr.generateMap({ |
| 42 | + source: id, |
| 43 | + includeContent: true, |
| 44 | + hires: true, |
| 45 | + }) |
| 46 | + }, |
| 47 | + } |
| 48 | + }, |
46 | 49 | }, |
47 | 50 | }, |
48 | 51 | { |
49 | 52 | name: `${NAME}:sub-style:post`, |
50 | 53 | enforce: 'post', |
51 | | - transformInclude() { |
52 | | - return true |
| 54 | + transformInclude(id: string) { |
| 55 | + return !id.endsWith('.html') |
53 | 56 | }, |
54 | 57 | async transform(code: string, id: string) { |
| 58 | + const formatId = normalizePath(id) |
55 | 59 | const mgcStr = new MagicString(code) |
56 | | - |
57 | | - // build only |
58 | | - if (id.includes('@vue/runtime-dom/dist/runtime-dom.esm-bundler.js')) |
| 60 | + // build only / webpack dev |
| 61 | + if (formatId.includes('@vue/runtime-dom/dist/runtime-dom.esm-bundler.js')) |
59 | 62 | injectVueRuntime(mgcStr) |
60 | 63 |
|
61 | | - // build only |
62 | | - if (id.includes('@vue/runtime-core/dist/runtime-core.esm-bundler.js')) |
| 64 | + // build only / webpack dev |
| 65 | + if (formatId.includes('@vue/runtime-core/dist/runtime-core.esm-bundler.js')) |
63 | 66 | injectVueRuntime(mgcStr) |
64 | 67 |
|
65 | | - // dev only |
66 | | - if (id.includes('.vite/deps/vue.js') |
67 | | - || (id.includes('.vite/deps/chunk') && code.includes('__isVue'))) |
| 68 | + // vite dev only |
| 69 | + if (formatId.includes('.vite/deps/vue.js') |
| 70 | + || (formatId.includes('.vite/deps/chunk') && code.includes('__isVue'))) |
68 | 71 | injectVueRuntime(mgcStr) |
69 | 72 |
|
70 | 73 | return { |
|
0 commit comments