vite-plugin-taro 0.6.2 → 0.6.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +8 -3
- package/README.md +8 -3
- package/dist/node/plugins/wx/dev/dev-host.d.ts +5 -4
- package/dist/node/plugins/wx/dev/dev-host.js +19 -43
- package/dist/node/plugins/wx/dev/plugins.d.ts +6 -15
- package/dist/node/plugins/wx/dev/plugins.js +16 -70
- package/dist/node/plugins/wx/dev/react-refresh.d.ts +9 -3
- package/dist/node/plugins/wx/dev/react-refresh.js +35 -7
- package/dist/node/plugins/wx/dev/wx-dev-options.d.ts +2 -3
- package/dist/node/plugins/wx/dev/wx-dev-options.js +6 -7
- package/dist/node/plugins/wx/plugins.js +5 -11
- package/dist/node/plugins/wx/resolve/resolver.js +4 -4
- package/dist/node/plugins/wx/resolve/{specialize-bootstrap.d.ts → specialize-app-capsule.d.ts} +2 -2
- package/dist/node/plugins/wx/resolve/{specialize-bootstrap.js → specialize-app-capsule.js} +2 -2
- package/dist/node/plugins/wx/styles/plugins.d.ts +141 -3
- package/dist/node/plugins/wx/styles/plugins.js +401 -89
- package/dist/node/utils/vite.d.ts +4 -14
- package/dist/node/utils/vite.js +7 -40
- package/dist/runtime/wx/amphibious/bootstrap.d.ts +0 -2
- package/dist/runtime/wx/amphibious/bootstrap.js +0 -2
- package/dist/runtime/wx/capsule/app.d.ts +2 -2
- package/dist/runtime/wx/capsule/app.js +2 -2
- package/dist/runtime/wx/dev/dev-runtime.js +55 -79
- package/package.json +4 -3
- package/src/node/plugins/wx/dev/dev-host.ts +22 -44
- package/src/node/plugins/wx/dev/plugins.ts +17 -73
- package/src/node/plugins/wx/dev/react-refresh.ts +36 -7
- package/src/node/plugins/wx/dev/wx-dev-options.ts +7 -10
- package/src/node/plugins/wx/plugins.ts +5 -11
- package/src/node/plugins/wx/resolve/resolver.ts +4 -4
- package/src/node/plugins/wx/resolve/{specialize-bootstrap.ts → specialize-app-capsule.ts} +2 -2
- package/src/node/plugins/wx/styles/plugins.ts +518 -93
- package/src/node/utils/vite.ts +13 -58
- package/src/runtime/wx/amphibious/bootstrap.ts +0 -5
- package/src/runtime/wx/capsule/app.ts +5 -2
- package/src/runtime/wx/dev/dev-runtime.ts +71 -120
- package/src/runtime/wx/wechat.d.ts +1 -2
- package/dist/node/plugins/wx/dev/create-style-capture.d.ts +0 -54
- package/dist/node/plugins/wx/dev/create-style-capture.js +0 -173
- package/dist/node/plugins/wx/styles/transform-wx-style.d.ts +0 -8
- package/dist/node/plugins/wx/styles/transform-wx-style.js +0 -9
- package/dist/node/plugins/wx/styles/utils.d.ts +0 -39
- package/dist/node/plugins/wx/styles/utils.js +0 -95
- package/src/node/plugins/wx/dev/create-style-capture.ts +0 -248
- package/src/node/plugins/wx/styles/transform-wx-style.ts +0 -11
- package/src/node/plugins/wx/styles/utils.ts +0 -119
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Creates an auxiliary style request without replacing the physical Rolldown graph module.
|
|
3
|
-
*
|
|
4
|
-
* `weapp-vite-sidecar` is an upstream protocol marker, not a cache-busting nonce. `weapp-tailwindcss` detects the query
|
|
5
|
-
* key, strips the complete query when resolving the physical CSS pipeline file, and excludes this synthetic request from
|
|
6
|
-
* transformed-source candidate collection. That lets the request use the latest candidate state without feeding generated
|
|
7
|
-
* CSS back into Tailwind's source memory. The descriptive `style` value is stable; upstream treats the presence of the key
|
|
8
|
-
* as the protocol contract.
|
|
9
|
-
*/
|
|
10
|
-
export declare function createTailwindSidecarId(rootId: string): string;
|
|
11
|
-
/** Selects imported style modules whose Vite development output owns a runtime CSS payload. */
|
|
12
|
-
export declare function isGlobalStyleRequest(id: string): boolean;
|
|
13
|
-
/**
|
|
14
|
-
* Creates the ordered style plan for one completed HMR transaction.
|
|
15
|
-
*
|
|
16
|
-
* `entryIds` carries semantic cascade ownership from the WX resolver: the App capsule first, followed by Page capsules in
|
|
17
|
-
* configured route order. Each entry is traversed depth-first. Static imports retain source order and precede dynamic imports;
|
|
18
|
-
* dynamic branches are included because WX cannot inject lazy CSS at browser runtime. A style follows its dependencies,
|
|
19
|
-
* matching evaluation order.
|
|
20
|
-
*
|
|
21
|
-
* Graph IDs may contain queries while `hasStyle` reads physical IDs. Normalization lets aliases share one captured style.
|
|
22
|
-
* JavaScript and non-runtime CSS requests remain traversal nodes but do not enter the plan. All mutable traversal state is
|
|
23
|
-
* transaction-local, so import additions and removals need no invalidation bookkeeping. Complexity is O(modules + edges),
|
|
24
|
-
* with O(modules + styles) temporary memory.
|
|
25
|
-
*/
|
|
26
|
-
export declare function createGraphStylePlan(entryIds: readonly string[], getModuleInfo: (moduleId: string) => Readonly<{
|
|
27
|
-
importedIds: readonly string[];
|
|
28
|
-
dynamicallyImportedIds: readonly string[];
|
|
29
|
-
}> | null, hasStyle: (styleId: string) => boolean): readonly string[];
|
|
30
|
-
/** Renders one immutable graph plan after any Tailwind roots in that same plan have been refreshed. */
|
|
31
|
-
export declare function composeGraphStyleCss(styleIds: readonly string[], getStyleCss: (styleId: string) => string): string;
|
|
32
|
-
/**
|
|
33
|
-
* Extracts Vite's final CSS payload from the development module without evaluating its browser HMR code.
|
|
34
|
-
*
|
|
35
|
-
* Remove this parser when either Vite exposes a supported plugin-container API that returns final CSS directly, or
|
|
36
|
-
* `weapp-tailwindcss` exposes generated root CSS after candidate updates. Until then the sidecar receives Vite's JavaScript
|
|
37
|
-
* style module, so this function isolates the version-specific `__vite__css` serialization contract.
|
|
38
|
-
*/
|
|
39
|
-
export declare function extractViteCss(moduleCode: string, rootId: string): string;
|
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
import { isCSSRequest } from 'vite';
|
|
2
|
-
import { normalizeModuleId } from '../../../utils/modules.js';
|
|
3
|
-
const nonRuntimeStyleQueries = ['direct', 'inline', 'inline-css', 'raw', 'style-attr', 'transform-only', 'url'];
|
|
4
|
-
/**
|
|
5
|
-
* Creates an auxiliary style request without replacing the physical Rolldown graph module.
|
|
6
|
-
*
|
|
7
|
-
* `weapp-vite-sidecar` is an upstream protocol marker, not a cache-busting nonce. `weapp-tailwindcss` detects the query
|
|
8
|
-
* key, strips the complete query when resolving the physical CSS pipeline file, and excludes this synthetic request from
|
|
9
|
-
* transformed-source candidate collection. That lets the request use the latest candidate state without feeding generated
|
|
10
|
-
* CSS back into Tailwind's source memory. The descriptive `style` value is stable; upstream treats the presence of the key
|
|
11
|
-
* as the protocol contract.
|
|
12
|
-
*/
|
|
13
|
-
export function createTailwindSidecarId(rootId) {
|
|
14
|
-
return `${rootId}?weapp-vite-sidecar=style`;
|
|
15
|
-
}
|
|
16
|
-
/** Selects imported style modules whose Vite development output owns a runtime CSS payload. */
|
|
17
|
-
export function isGlobalStyleRequest(id) {
|
|
18
|
-
if (!isCSSRequest(id)) {
|
|
19
|
-
return false;
|
|
20
|
-
}
|
|
21
|
-
const queryStart = id.indexOf('?');
|
|
22
|
-
if (queryStart < 0) {
|
|
23
|
-
return true;
|
|
24
|
-
}
|
|
25
|
-
const fragmentStart = id.indexOf('#', queryStart);
|
|
26
|
-
const query = id.slice(queryStart + 1, fragmentStart < 0 ? undefined : fragmentStart);
|
|
27
|
-
const parameters = new URLSearchParams(query);
|
|
28
|
-
return (!parameters.has('weapp-vite-sidecar') && nonRuntimeStyleQueries.every((parameter) => !parameters.has(parameter)));
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* Creates the ordered style plan for one completed HMR transaction.
|
|
32
|
-
*
|
|
33
|
-
* `entryIds` carries semantic cascade ownership from the WX resolver: the App capsule first, followed by Page capsules in
|
|
34
|
-
* configured route order. Each entry is traversed depth-first. Static imports retain source order and precede dynamic imports;
|
|
35
|
-
* dynamic branches are included because WX cannot inject lazy CSS at browser runtime. A style follows its dependencies,
|
|
36
|
-
* matching evaluation order.
|
|
37
|
-
*
|
|
38
|
-
* Graph IDs may contain queries while `hasStyle` reads physical IDs. Normalization lets aliases share one captured style.
|
|
39
|
-
* JavaScript and non-runtime CSS requests remain traversal nodes but do not enter the plan. All mutable traversal state is
|
|
40
|
-
* transaction-local, so import additions and removals need no invalidation bookkeeping. Complexity is O(modules + edges),
|
|
41
|
-
* with O(modules + styles) temporary memory.
|
|
42
|
-
*/
|
|
43
|
-
export function createGraphStylePlan(entryIds, getModuleInfo, hasStyle) {
|
|
44
|
-
// These local collections collapse cycles, shared modules, and aliases without retaining derived topology between batches.
|
|
45
|
-
const visitedModuleIds = new Set();
|
|
46
|
-
const visitedStyleIds = new Set();
|
|
47
|
-
const styleIds = [];
|
|
48
|
-
const visit = (moduleId) => {
|
|
49
|
-
if (visitedModuleIds.has(moduleId)) {
|
|
50
|
-
return;
|
|
51
|
-
}
|
|
52
|
-
visitedModuleIds.add(moduleId);
|
|
53
|
-
const moduleInfo = getModuleInfo(moduleId);
|
|
54
|
-
if (!moduleInfo) {
|
|
55
|
-
return;
|
|
56
|
-
}
|
|
57
|
-
moduleInfo.importedIds.forEach(visit);
|
|
58
|
-
moduleInfo.dynamicallyImportedIds.forEach(visit);
|
|
59
|
-
const styleId = normalizeModuleId(moduleId);
|
|
60
|
-
if (hasStyle(styleId) && !visitedStyleIds.has(styleId)) {
|
|
61
|
-
visitedStyleIds.add(styleId);
|
|
62
|
-
styleIds.push(styleId);
|
|
63
|
-
}
|
|
64
|
-
};
|
|
65
|
-
entryIds.forEach(visit);
|
|
66
|
-
return styleIds;
|
|
67
|
-
}
|
|
68
|
-
/** Renders one immutable graph plan after any Tailwind roots in that same plan have been refreshed. */
|
|
69
|
-
export function composeGraphStyleCss(styleIds, getStyleCss) {
|
|
70
|
-
return styleIds.map(getStyleCss).join('\n');
|
|
71
|
-
}
|
|
72
|
-
/**
|
|
73
|
-
* Extracts Vite's final CSS payload from the development module without evaluating its browser HMR code.
|
|
74
|
-
*
|
|
75
|
-
* Remove this parser when either Vite exposes a supported plugin-container API that returns final CSS directly, or
|
|
76
|
-
* `weapp-tailwindcss` exposes generated root CSS after candidate updates. Until then the sidecar receives Vite's JavaScript
|
|
77
|
-
* style module, so this function isolates the version-specific `__vite__css` serialization contract.
|
|
78
|
-
*/
|
|
79
|
-
export function extractViteCss(moduleCode, rootId) {
|
|
80
|
-
const assignmentPrefix = 'const __vite__css = ';
|
|
81
|
-
const assignmentStart = moduleCode.indexOf(assignmentPrefix);
|
|
82
|
-
if (assignmentStart < 0) {
|
|
83
|
-
throw new Error(`Vite CSS transform for ${rootId} did not expose __vite__css`);
|
|
84
|
-
}
|
|
85
|
-
// Vite serializes the payload with JSON.stringify on one assignment line. Parse that literal so quotes, escapes, and
|
|
86
|
-
// embedded CSS newlines are decoded by JSON rather than by a second, subtly different unescaping implementation.
|
|
87
|
-
const valueStart = assignmentStart + assignmentPrefix.length;
|
|
88
|
-
const lineEnd = moduleCode.indexOf('\n', valueStart);
|
|
89
|
-
const serializedCss = moduleCode.slice(valueStart, lineEnd < 0 ? moduleCode.length : lineEnd);
|
|
90
|
-
const css = JSON.parse(serializedCss);
|
|
91
|
-
if (typeof css !== 'string') {
|
|
92
|
-
throw new Error(`Vite CSS transform for ${rootId} exposed a non-string __vite__css value`);
|
|
93
|
-
}
|
|
94
|
-
return css;
|
|
95
|
-
}
|
|
@@ -1,248 +0,0 @@
|
|
|
1
|
-
import type { GetModuleInfo, Plugin } from 'rolldown'
|
|
2
|
-
import { isCSSRequest } from 'vite'
|
|
3
|
-
import { normalizeModuleId } from '../../../utils/modules.ts'
|
|
4
|
-
import { transformWxStyle } from '../styles/transform-wx-style.ts'
|
|
5
|
-
import {
|
|
6
|
-
composeGraphStyleCss,
|
|
7
|
-
createGraphStylePlan,
|
|
8
|
-
createTailwindSidecarId,
|
|
9
|
-
extractViteCss,
|
|
10
|
-
isGlobalStyleRequest
|
|
11
|
-
} from '../styles/utils.ts'
|
|
12
|
-
import { globalWxssFileName, writeHmrFile } from './hmr-files.ts'
|
|
13
|
-
|
|
14
|
-
/** Final CSS extracted from Vite's transformed style module, before the shared WX compatibility pass. */
|
|
15
|
-
type ProcessedStyle = Readonly<{
|
|
16
|
-
css: string
|
|
17
|
-
/** Marks roots whose generated utilities must be refreshed when JavaScript changes Tailwind candidates. */
|
|
18
|
-
isTailwindRoot: boolean
|
|
19
|
-
}>
|
|
20
|
-
|
|
21
|
-
/** Minimal structural view of DevEngine output; style reconciliation does not own chunks or other asset metadata. */
|
|
22
|
-
type CompleteOutputFile =
|
|
23
|
-
| Readonly<{ type: 'asset'; fileName: string; source: string | Uint8Array }>
|
|
24
|
-
| Readonly<{ type: 'chunk'; fileName: string }>
|
|
25
|
-
|
|
26
|
-
export type StyleCaptureAction =
|
|
27
|
-
| Readonly<{ kind: 'capture-graph'; getModuleInfo: GetModuleInfo }>
|
|
28
|
-
| Readonly<{ kind: 'capture-style'; id: string; style: ProcessedStyle }>
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Captures final Vite CSS and composes the graph projection with its durable WXSS publisher.
|
|
32
|
-
*
|
|
33
|
-
* Rolldown remains authoritative for topology. Plugin hooks emit typed actions so the host serializes capture mutations with
|
|
34
|
-
* output and HMR publication; the projection and publisher below each own only one mutable concern.
|
|
35
|
-
*
|
|
36
|
-
* Complete-build path:
|
|
37
|
-
* final transform captures → DevEngine output write → graph reconciliation → App build rotation
|
|
38
|
-
*
|
|
39
|
-
* Incremental path:
|
|
40
|
-
* final transform captures → optional Tailwind root refresh → graph rendering → WXSS write → JavaScript patch publication
|
|
41
|
-
*
|
|
42
|
-
* Both paths render the same ordered App/Page graph projection. This prevents complete builds and HMR from implementing two
|
|
43
|
-
* subtly different CSS ownership policies, while keeping physical byte equality and atomic writes out of graph state.
|
|
44
|
-
*/
|
|
45
|
-
export function createStyleCapture({
|
|
46
|
-
applicationEntryIds,
|
|
47
|
-
outDir,
|
|
48
|
-
emit,
|
|
49
|
-
transformTailwindRoot
|
|
50
|
-
}: {
|
|
51
|
-
applicationEntryIds: readonly string[]
|
|
52
|
-
outDir: string
|
|
53
|
-
emit: (action: StyleCaptureAction) => void
|
|
54
|
-
transformTailwindRoot: (rootId: string, requestId: string) => Promise<Readonly<{ code: string }> | null>
|
|
55
|
-
}): Readonly<{
|
|
56
|
-
captureGraph: (reader: GetModuleInfo) => void
|
|
57
|
-
captureStyle: (id: string, style: ProcessedStyle) => void
|
|
58
|
-
plugin: Plugin
|
|
59
|
-
publishChanged: (changedIds: readonly string[]) => Promise<void>
|
|
60
|
-
reconcileComplete: (output: readonly CompleteOutputFile[]) => Promise<void>
|
|
61
|
-
}> {
|
|
62
|
-
const projection = createStyleProjection({
|
|
63
|
-
applicationEntryIds: applicationEntryIds,
|
|
64
|
-
transformTailwindRoot: transformTailwindRoot
|
|
65
|
-
})
|
|
66
|
-
const publication = createStylePublication(outDir)
|
|
67
|
-
|
|
68
|
-
const plugin: Plugin = {
|
|
69
|
-
name: 'vpt:wx-dev-style-capture',
|
|
70
|
-
buildStart() {
|
|
71
|
-
// Capture a live reader rather than a graph snapshot. Rolldown updates the capability as imports change, and a later
|
|
72
|
-
// complete generation replaces it through the host action queue before that generation can publish output.
|
|
73
|
-
emit({ kind: 'capture-graph', getModuleInfo: (moduleId) => this.getModuleInfo(moduleId) })
|
|
74
|
-
},
|
|
75
|
-
transform(code, id) {
|
|
76
|
-
if (!isGlobalStyleRequest(id)) {
|
|
77
|
-
return
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
// This host plugin runs after Vite's CSS transform, so `code` is the JavaScript wrapper containing final PostCSS and
|
|
81
|
-
// CSS-Module output. Capturing source CSS instead would lose generated class names and framework transformations.
|
|
82
|
-
const css = extractViteCss(code, id)
|
|
83
|
-
emit({
|
|
84
|
-
kind: 'capture-style',
|
|
85
|
-
id: normalizeModuleId(id),
|
|
86
|
-
style: {
|
|
87
|
-
css: css,
|
|
88
|
-
isTailwindRoot: css.includes('weapp-tailwindcss vite-generated-css:')
|
|
89
|
-
}
|
|
90
|
-
})
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
return {
|
|
95
|
-
captureGraph: projection.captureGraph,
|
|
96
|
-
captureStyle: projection.captureStyle,
|
|
97
|
-
plugin: plugin,
|
|
98
|
-
async publishChanged(changedIds) {
|
|
99
|
-
// A CSS edit already carries updated processed bytes. Any non-CSS edit can alter both imports and Tailwind class
|
|
100
|
-
// candidates, so it requires a fresh graph plan and Tailwind-root generation even when no .css ID changed directly.
|
|
101
|
-
const styleChanged = changedIds.some(isGlobalStyleRequest)
|
|
102
|
-
const candidatesChanged = changedIds.some((id) => !isCSSRequest(id))
|
|
103
|
-
if (!styleChanged && !candidatesChanged) {
|
|
104
|
-
return
|
|
105
|
-
}
|
|
106
|
-
await publication.publish(await projection.render(candidatesChanged))
|
|
107
|
-
},
|
|
108
|
-
async reconcileComplete(output) {
|
|
109
|
-
// Bundled development can omit CSS Modules from the compiler asset even with cssCodeSplit disabled. Observe the
|
|
110
|
-
// physical output first, then reconcile the same graph projection used by incremental HMR before App rotation.
|
|
111
|
-
publication.observeOutput(output)
|
|
112
|
-
await publication.publish(await projection.render(false))
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
/**
|
|
118
|
-
* Owns the live graph capability and final transformed bytes for every observed style module.
|
|
119
|
-
*
|
|
120
|
-
* Rendering is O(V + E + C): graph planning visits each reachable module and edge once, then composition and WX conversion
|
|
121
|
-
* process C CSS bytes once. The only persistent memory is one processed byte string per style identity observed by the server.
|
|
122
|
-
*/
|
|
123
|
-
function createStyleProjection({
|
|
124
|
-
applicationEntryIds,
|
|
125
|
-
transformTailwindRoot
|
|
126
|
-
}: {
|
|
127
|
-
applicationEntryIds: readonly string[]
|
|
128
|
-
transformTailwindRoot: (rootId: string, requestId: string) => Promise<Readonly<{ code: string }> | null>
|
|
129
|
-
}): Readonly<{
|
|
130
|
-
captureGraph: (reader: GetModuleInfo) => void
|
|
131
|
-
captureStyle: (id: string, style: ProcessedStyle) => void
|
|
132
|
-
render: (refreshTailwind: boolean) => Promise<string>
|
|
133
|
-
}> {
|
|
134
|
-
/*
|
|
135
|
-
* This mutable capability is rebound by buildStart for each complete generation. Rolldown keeps the function live across
|
|
136
|
-
* incremental graph edits, so rendering uses authoritative topology without maintaining a shadow graph.
|
|
137
|
-
*/
|
|
138
|
-
let getModuleInfo: GetModuleInfo | undefined
|
|
139
|
-
/*
|
|
140
|
-
* This mutable projection stores final CSS bytes that Rolldown does not retain. Successful transforms replace one entry;
|
|
141
|
-
* unreachable entries can remain because every render filters them through the current graph plan. Its size is bounded by
|
|
142
|
-
* style module identities observed during this server lifecycle.
|
|
143
|
-
*/
|
|
144
|
-
const processedStyles = new Map<string, ProcessedStyle>()
|
|
145
|
-
|
|
146
|
-
return {
|
|
147
|
-
captureGraph(reader) {
|
|
148
|
-
getModuleInfo = reader
|
|
149
|
-
},
|
|
150
|
-
captureStyle(id, style) {
|
|
151
|
-
processedStyles.set(id, style)
|
|
152
|
-
},
|
|
153
|
-
async render(refreshTailwind) {
|
|
154
|
-
if (!getModuleInfo) {
|
|
155
|
-
throw new Error('WX style graph is unavailable before publication')
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
// App first and configured Pages afterward define one deterministic global cascade. The plan also removes stale map
|
|
159
|
-
// entries implicitly: styles no longer reachable from these roots never enter composition.
|
|
160
|
-
const styleIds = createGraphStylePlan(applicationEntryIds, getModuleInfo, (styleId) =>
|
|
161
|
-
processedStyles.has(styleId)
|
|
162
|
-
)
|
|
163
|
-
if (refreshTailwind) {
|
|
164
|
-
await refreshTailwindStyles(styleIds, processedStyles, transformTailwindRoot)
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
// Compose browser-facing transformed CSS first, then run one whole-file WX pass. Transforming modules independently
|
|
168
|
-
// would change cross-module cascade behavior and duplicate compatibility work.
|
|
169
|
-
const css = composeGraphStyleCss(styleIds, (styleId) => requireProcessedStyle(processedStyles, styleId).css)
|
|
170
|
-
return (await transformWxStyle(css)).css
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
/**
|
|
176
|
-
* Owns the physical WXSS frontier independently from graph capture and rendering.
|
|
177
|
-
*
|
|
178
|
-
* DevEngine writes complete output itself; incremental HMR does not. Observing complete output before publishing the projection
|
|
179
|
-
* gives both writers one byte frontier, so equality avoids redundant filesystem notifications without pretending the host owns
|
|
180
|
-
* the compiler's write transaction.
|
|
181
|
-
*/
|
|
182
|
-
function createStylePublication(outDir: string): Readonly<{
|
|
183
|
-
observeOutput: (output: readonly CompleteOutputFile[]) => void
|
|
184
|
-
publish: (wxss: string) => Promise<void>
|
|
185
|
-
}> {
|
|
186
|
-
/*
|
|
187
|
-
* This mutable value mirrors bytes durable on disk. Complete output adopts the compiler's external write before graph
|
|
188
|
-
* reconciliation; host publication advances it only after an atomic write succeeds or byte equality proves none is needed.
|
|
189
|
-
*/
|
|
190
|
-
let publishedWxss: string | undefined
|
|
191
|
-
|
|
192
|
-
return {
|
|
193
|
-
observeOutput(output) {
|
|
194
|
-
// Missing WXSS means DevEngine intentionally reused the existing physical asset. Do not reset the frontier: doing so
|
|
195
|
-
// would force an identical rewrite and a spurious DevTools style event on every omitted complete generation.
|
|
196
|
-
const style = output.find(
|
|
197
|
-
(file): file is Extract<CompleteOutputFile, { type: 'asset' }> =>
|
|
198
|
-
file.type === 'asset' && file.fileName === globalWxssFileName
|
|
199
|
-
)
|
|
200
|
-
if (style) {
|
|
201
|
-
publishedWxss = typeof style.source === 'string' ? style.source : new TextDecoder().decode(style.source)
|
|
202
|
-
}
|
|
203
|
-
},
|
|
204
|
-
async publish(wxss) {
|
|
205
|
-
if (wxss !== publishedWxss) {
|
|
206
|
-
// writeHmrFile uses atomic replacement; advance the frontier only after durability so a failed write leaves the
|
|
207
|
-
// last known physical generation available for the next reconciliation attempt.
|
|
208
|
-
await writeHmrFile(outDir, globalWxssFileName, wxss)
|
|
209
|
-
}
|
|
210
|
-
publishedWxss = wxss
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
/**
|
|
216
|
-
* Regenerates all reachable Tailwind roots and commits the cache only when every transform succeeds.
|
|
217
|
-
*
|
|
218
|
-
* Roots run concurrently because they are independent derivations of the same candidate generation. Results remain local until
|
|
219
|
-
* Promise.all fulfills; one failed root therefore preserves every prior root together instead of publishing a mixed generation.
|
|
220
|
-
*/
|
|
221
|
-
async function refreshTailwindStyles(
|
|
222
|
-
styleIds: readonly string[],
|
|
223
|
-
processedStyles: Map<string, ProcessedStyle>,
|
|
224
|
-
transformRoot: (rootId: string, requestId: string) => Promise<Readonly<{ code: string }> | null>
|
|
225
|
-
): Promise<void> {
|
|
226
|
-
const roots = styleIds.filter((styleId) => requireProcessedStyle(processedStyles, styleId).isTailwindRoot)
|
|
227
|
-
const refreshedStyles = await Promise.all(
|
|
228
|
-
roots.map(async (rootId) => {
|
|
229
|
-
const requestId = createTailwindSidecarId(rootId)
|
|
230
|
-
const result = await transformRoot(rootId, requestId)
|
|
231
|
-
if (!result) {
|
|
232
|
-
throw new Error(`Tailwind sidecar transform produced no result: ${requestId}`)
|
|
233
|
-
}
|
|
234
|
-
return [rootId, { css: extractViteCss(result.code, requestId), isTailwindRoot: true }] as const
|
|
235
|
-
})
|
|
236
|
-
)
|
|
237
|
-
refreshedStyles.forEach(([rootId, style]) => {
|
|
238
|
-
processedStyles.set(rootId, style)
|
|
239
|
-
})
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
function requireProcessedStyle(processedStyles: ReadonlyMap<string, ProcessedStyle>, styleId: string): ProcessedStyle {
|
|
243
|
-
const style = processedStyles.get(styleId)
|
|
244
|
-
if (!style) {
|
|
245
|
-
throw new Error(`WX style plan references uncaptured CSS: ${styleId}`)
|
|
246
|
-
}
|
|
247
|
-
return style
|
|
248
|
-
}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { createStyleHandler } from '@weapp-tailwindcss/postcss'
|
|
2
|
-
|
|
3
|
-
export const wxStyleOptions = {
|
|
4
|
-
cssCalc: false,
|
|
5
|
-
autoprefixer: false,
|
|
6
|
-
rem2rpx: true,
|
|
7
|
-
px2rpx: true
|
|
8
|
-
} as const
|
|
9
|
-
|
|
10
|
-
/** Shared finalization policy for complete builds and host-owned style HMR. */
|
|
11
|
-
export const transformWxStyle = createStyleHandler(wxStyleOptions)
|
|
@@ -1,119 +0,0 @@
|
|
|
1
|
-
import { isCSSRequest } from 'vite'
|
|
2
|
-
import { normalizeModuleId } from '../../../utils/modules.ts'
|
|
3
|
-
|
|
4
|
-
const nonRuntimeStyleQueries = ['direct', 'inline', 'inline-css', 'raw', 'style-attr', 'transform-only', 'url'] as const
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Creates an auxiliary style request without replacing the physical Rolldown graph module.
|
|
8
|
-
*
|
|
9
|
-
* `weapp-vite-sidecar` is an upstream protocol marker, not a cache-busting nonce. `weapp-tailwindcss` detects the query
|
|
10
|
-
* key, strips the complete query when resolving the physical CSS pipeline file, and excludes this synthetic request from
|
|
11
|
-
* transformed-source candidate collection. That lets the request use the latest candidate state without feeding generated
|
|
12
|
-
* CSS back into Tailwind's source memory. The descriptive `style` value is stable; upstream treats the presence of the key
|
|
13
|
-
* as the protocol contract.
|
|
14
|
-
*/
|
|
15
|
-
export function createTailwindSidecarId(rootId: string): string {
|
|
16
|
-
return `${rootId}?weapp-vite-sidecar=style`
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/** Selects imported style modules whose Vite development output owns a runtime CSS payload. */
|
|
20
|
-
export function isGlobalStyleRequest(id: string): boolean {
|
|
21
|
-
if (!isCSSRequest(id)) {
|
|
22
|
-
return false
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
const queryStart = id.indexOf('?')
|
|
26
|
-
if (queryStart < 0) {
|
|
27
|
-
return true
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
const fragmentStart = id.indexOf('#', queryStart)
|
|
31
|
-
const query = id.slice(queryStart + 1, fragmentStart < 0 ? undefined : fragmentStart)
|
|
32
|
-
const parameters = new URLSearchParams(query)
|
|
33
|
-
|
|
34
|
-
return (
|
|
35
|
-
!parameters.has('weapp-vite-sidecar') && nonRuntimeStyleQueries.every((parameter) => !parameters.has(parameter))
|
|
36
|
-
)
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Creates the ordered style plan for one completed HMR transaction.
|
|
41
|
-
*
|
|
42
|
-
* `entryIds` carries semantic cascade ownership from the WX resolver: the App capsule first, followed by Page capsules in
|
|
43
|
-
* configured route order. Each entry is traversed depth-first. Static imports retain source order and precede dynamic imports;
|
|
44
|
-
* dynamic branches are included because WX cannot inject lazy CSS at browser runtime. A style follows its dependencies,
|
|
45
|
-
* matching evaluation order.
|
|
46
|
-
*
|
|
47
|
-
* Graph IDs may contain queries while `hasStyle` reads physical IDs. Normalization lets aliases share one captured style.
|
|
48
|
-
* JavaScript and non-runtime CSS requests remain traversal nodes but do not enter the plan. All mutable traversal state is
|
|
49
|
-
* transaction-local, so import additions and removals need no invalidation bookkeeping. Complexity is O(modules + edges),
|
|
50
|
-
* with O(modules + styles) temporary memory.
|
|
51
|
-
*/
|
|
52
|
-
export function createGraphStylePlan(
|
|
53
|
-
entryIds: readonly string[],
|
|
54
|
-
getModuleInfo: (
|
|
55
|
-
moduleId: string
|
|
56
|
-
) => Readonly<{ importedIds: readonly string[]; dynamicallyImportedIds: readonly string[] }> | null,
|
|
57
|
-
hasStyle: (styleId: string) => boolean
|
|
58
|
-
): readonly string[] {
|
|
59
|
-
// These local collections collapse cycles, shared modules, and aliases without retaining derived topology between batches.
|
|
60
|
-
const visitedModuleIds = new Set<string>()
|
|
61
|
-
const visitedStyleIds = new Set<string>()
|
|
62
|
-
const styleIds: string[] = []
|
|
63
|
-
|
|
64
|
-
const visit = (moduleId: string): void => {
|
|
65
|
-
if (visitedModuleIds.has(moduleId)) {
|
|
66
|
-
return
|
|
67
|
-
}
|
|
68
|
-
visitedModuleIds.add(moduleId)
|
|
69
|
-
|
|
70
|
-
const moduleInfo = getModuleInfo(moduleId)
|
|
71
|
-
if (!moduleInfo) {
|
|
72
|
-
return
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
moduleInfo.importedIds.forEach(visit)
|
|
76
|
-
moduleInfo.dynamicallyImportedIds.forEach(visit)
|
|
77
|
-
|
|
78
|
-
const styleId = normalizeModuleId(moduleId)
|
|
79
|
-
if (hasStyle(styleId) && !visitedStyleIds.has(styleId)) {
|
|
80
|
-
visitedStyleIds.add(styleId)
|
|
81
|
-
styleIds.push(styleId)
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
entryIds.forEach(visit)
|
|
86
|
-
return styleIds
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/** Renders one immutable graph plan after any Tailwind roots in that same plan have been refreshed. */
|
|
90
|
-
export function composeGraphStyleCss(styleIds: readonly string[], getStyleCss: (styleId: string) => string): string {
|
|
91
|
-
return styleIds.map(getStyleCss).join('\n')
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* Extracts Vite's final CSS payload from the development module without evaluating its browser HMR code.
|
|
96
|
-
*
|
|
97
|
-
* Remove this parser when either Vite exposes a supported plugin-container API that returns final CSS directly, or
|
|
98
|
-
* `weapp-tailwindcss` exposes generated root CSS after candidate updates. Until then the sidecar receives Vite's JavaScript
|
|
99
|
-
* style module, so this function isolates the version-specific `__vite__css` serialization contract.
|
|
100
|
-
*/
|
|
101
|
-
export function extractViteCss(moduleCode: string, rootId: string): string {
|
|
102
|
-
const assignmentPrefix = 'const __vite__css = '
|
|
103
|
-
const assignmentStart = moduleCode.indexOf(assignmentPrefix)
|
|
104
|
-
if (assignmentStart < 0) {
|
|
105
|
-
throw new Error(`Vite CSS transform for ${rootId} did not expose __vite__css`)
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
// Vite serializes the payload with JSON.stringify on one assignment line. Parse that literal so quotes, escapes, and
|
|
109
|
-
// embedded CSS newlines are decoded by JSON rather than by a second, subtly different unescaping implementation.
|
|
110
|
-
const valueStart = assignmentStart + assignmentPrefix.length
|
|
111
|
-
const lineEnd = moduleCode.indexOf('\n', valueStart)
|
|
112
|
-
const serializedCss = moduleCode.slice(valueStart, lineEnd < 0 ? moduleCode.length : lineEnd)
|
|
113
|
-
const css: unknown = JSON.parse(serializedCss)
|
|
114
|
-
if (typeof css !== 'string') {
|
|
115
|
-
throw new Error(`Vite CSS transform for ${rootId} exposed a non-string __vite__css value`)
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
return css
|
|
119
|
-
}
|