@octane-xplat/bamboo 0.18.0 → 0.20.0
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.md +2 -95
- package/dist/platform-output.js +1 -1
- package/dist/vite.js +2 -2
- package/package.json +65 -63
- package/src/build-environments.ts +62 -56
- package/src/config.ts +114 -113
- package/src/platform-output.ts +218 -198
- package/src/vite.ts +114 -110
- package/LICENSE +0 -21
package/src/platform-output.ts
CHANGED
|
@@ -5,208 +5,228 @@ import remapping from '@ampproject/remapping'
|
|
|
5
5
|
import type { Plugin } from 'vite'
|
|
6
6
|
|
|
7
7
|
interface PlatformCssApi {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
defer(id: string): void
|
|
9
|
+
filter(css: string, from?: string): string
|
|
10
|
+
hasPlatformConditions(css: string): boolean
|
|
11
|
+
project(css: string, from: string, previousMap?: string): { css: string; map?: string }
|
|
11
12
|
}
|
|
12
13
|
|
|
13
14
|
/** Filter only after Bamboo's validation and restore content-addressed asset names. */
|
|
14
15
|
export function platformOutput(): Plugin {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
16
|
+
let compiler: PlatformCssApi | undefined
|
|
17
|
+
let ssr = false
|
|
18
|
+
const adapted = new WeakSet<Plugin>()
|
|
19
|
+
|
|
20
|
+
return {
|
|
21
|
+
name: 'xplat-bamboo:platform-output',
|
|
22
|
+
enforce: 'post',
|
|
23
|
+
configResolved(config) {
|
|
24
|
+
ssr = Boolean(config.build.ssr)
|
|
25
|
+
compiler = config.plugins.find((plugin) => plugin.name === 'xplat-platform-css')?.api
|
|
26
|
+
if (config.command === 'build') {
|
|
27
|
+
compiler?.defer('virtual:bamboo.css')
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
transform: {
|
|
31
|
+
order: 'pre',
|
|
32
|
+
handler(code, id) {
|
|
33
|
+
if (!id.split('?')[0].endsWith('virtual:bamboo.css') || !code.includes('@platform')) {
|
|
34
|
+
return
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (!compiler) {
|
|
38
|
+
this.error(
|
|
39
|
+
'@octane-xplat/bamboo: platform conditions require xplatCss(target) from @octane-xplat/cli/vite',
|
|
40
|
+
)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (ssr || this.environment?.config.build.ssr) {
|
|
44
|
+
this.error(
|
|
45
|
+
'@octane-xplat/bamboo: platform conditions currently require a single renderer build; SSR generation is not supported',
|
|
46
|
+
)
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
options: {
|
|
51
|
+
order: 'post',
|
|
52
|
+
handler(input) {
|
|
53
|
+
const outputs = (
|
|
54
|
+
input as typeof input & { output?: { plugins?: Plugin[] } | { plugins?: Plugin[] }[] }
|
|
55
|
+
).output
|
|
56
|
+
|
|
57
|
+
for (const output of Array.isArray(outputs) ? outputs : outputs ? [outputs] : []) {
|
|
58
|
+
for (const plugin of (output.plugins ?? []).flat(Infinity) as Plugin[]) {
|
|
59
|
+
if (!plugin?.name?.startsWith('bamboocss:output-finalizer:') || adapted.has(plugin)) {
|
|
60
|
+
continue
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const hook = plugin.generateBundle
|
|
64
|
+
const handler = typeof hook === 'function' ? hook : hook?.handler
|
|
65
|
+
|
|
66
|
+
if (!handler) {
|
|
67
|
+
throw new Error(
|
|
68
|
+
'@octane-xplat/bamboo: output finalizer has no generateBundle handler',
|
|
69
|
+
)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
plugin.generateBundle = {
|
|
73
|
+
...(typeof hook === 'object' ? hook : {}),
|
|
74
|
+
async handler(options, bundle, isWrite) {
|
|
75
|
+
await handler.call(this, options, bundle, isWrite)
|
|
76
|
+
if (!compiler) {
|
|
77
|
+
return
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const renamed = new Map<string, string>()
|
|
81
|
+
|
|
82
|
+
for (const [key, asset] of Object.entries(bundle)) {
|
|
83
|
+
if (asset.type !== 'asset' || !asset.fileName.endsWith('.css')) {
|
|
84
|
+
continue
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const source =
|
|
88
|
+
typeof asset.source === 'string'
|
|
89
|
+
? asset.source
|
|
90
|
+
: new TextDecoder().decode(asset.source)
|
|
91
|
+
|
|
92
|
+
const mapKey = `${asset.fileName}.map`
|
|
93
|
+
const mapAsset = bundle[mapKey]
|
|
94
|
+
const previousMap =
|
|
95
|
+
mapAsset?.type === 'asset' ? String(mapAsset.source) : undefined
|
|
96
|
+
|
|
97
|
+
if (!compiler.hasPlatformConditions(source)) {
|
|
98
|
+
continue
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const projected = compiler.project(source, asset.fileName, previousMap)
|
|
102
|
+
let filtered = projected.css
|
|
103
|
+
|
|
104
|
+
if (filtered === source) {
|
|
105
|
+
continue
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const hash = createHash('sha256').update(filtered).digest('hex').slice(0, 16)
|
|
109
|
+
const next = asset.fileName.replace(/\.css$/, `.platform-${hash}.css`)
|
|
110
|
+
|
|
111
|
+
renamed.set(asset.fileName, next)
|
|
112
|
+
if (projected.map && previousMap) {
|
|
113
|
+
const nextMap = `${next}.map`
|
|
114
|
+
const map = JSON.parse(projected.map)
|
|
115
|
+
|
|
116
|
+
map.file = posix.basename(next)
|
|
117
|
+
this.emitFile({ type: 'asset', fileName: nextMap, source: JSON.stringify(map) })
|
|
118
|
+
delete bundle[mapKey]
|
|
119
|
+
filtered += `
|
|
109
120
|
/*# sourceMappingURL=${posix.basename(nextMap)} */`
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
this.emitFile({ type: 'asset', fileName: next, source: filtered })
|
|
124
|
+
delete bundle[key]
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
for (const file of Object.values(bundle)) {
|
|
128
|
+
const replace = (text: string) => {
|
|
129
|
+
for (const [old, next] of renamed) {
|
|
130
|
+
text = text.split(old).join(next)
|
|
131
|
+
|
|
132
|
+
const directory = posix.dirname(file.fileName)
|
|
133
|
+
|
|
134
|
+
text = text
|
|
135
|
+
.split(posix.relative(directory, old))
|
|
136
|
+
.join(posix.relative(directory, next))
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return text
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (file.type === 'chunk') {
|
|
143
|
+
const nextCode = replace(file.code)
|
|
144
|
+
|
|
145
|
+
if (nextCode !== file.code) {
|
|
146
|
+
const magic = new MagicString(file.code)
|
|
147
|
+
const replacements = new Map(renamed)
|
|
148
|
+
|
|
149
|
+
for (const [old, next] of renamed) {
|
|
150
|
+
replacements.set(
|
|
151
|
+
posix.relative(posix.dirname(file.fileName), old),
|
|
152
|
+
posix.relative(posix.dirname(file.fileName), next),
|
|
153
|
+
)
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// Scan the original code once so overlapping full/relative names
|
|
157
|
+
// cannot apply two edits to the same source-map span.
|
|
158
|
+
for (let index = 0; index < file.code.length; index++) {
|
|
159
|
+
const found = [...replacements].find(([old]) =>
|
|
160
|
+
file.code.startsWith(old, index),
|
|
161
|
+
)
|
|
162
|
+
|
|
163
|
+
if (!found) {
|
|
164
|
+
continue
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
magic.overwrite(index, index + found[0].length, found[1])
|
|
168
|
+
index += found[0].length - 1
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
file.code = magic.toString()
|
|
172
|
+
if (file.map) {
|
|
173
|
+
const combined = remapping(
|
|
174
|
+
[
|
|
175
|
+
magic.generateMap({ source: file.fileName, hires: 'boundary' }),
|
|
176
|
+
file.map,
|
|
177
|
+
] as never,
|
|
178
|
+
() => null,
|
|
179
|
+
)
|
|
180
|
+
|
|
181
|
+
const encoded = combined.toString()
|
|
182
|
+
|
|
183
|
+
file.map = Object.assign(combined, {
|
|
184
|
+
toUrl: () =>
|
|
185
|
+
`data:application/json;charset=utf-8;base64,${Buffer.from(encoded).toString('base64')}`,
|
|
186
|
+
}) as typeof file.map
|
|
187
|
+
|
|
188
|
+
const mapFile = bundle[`${file.fileName}.map`]
|
|
189
|
+
|
|
190
|
+
if (mapFile?.type === 'asset') {
|
|
191
|
+
mapFile.source = encoded
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
if (file.code.includes('//# sourceMappingURL=data:')) {
|
|
195
|
+
file.code =
|
|
196
|
+
file.code.replace(
|
|
197
|
+
/\n?\/\/# sourceMappingURL=data:application\/json[^\n]*$/,
|
|
198
|
+
'',
|
|
199
|
+
) +
|
|
200
|
+
`
|
|
182
201
|
//# sourceMappingURL=${file.map.toUrl()}`
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const refs = file as typeof file & { referencedFiles?: string[] }
|
|
207
|
+
|
|
208
|
+
if (refs.referencedFiles) {
|
|
209
|
+
refs.referencedFiles = refs.referencedFiles.map(replace)
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
const metadata = (
|
|
213
|
+
file as typeof file & { viteMetadata?: { importedCss?: Set<string> } }
|
|
214
|
+
).viteMetadata
|
|
215
|
+
|
|
216
|
+
if (metadata?.importedCss) {
|
|
217
|
+
metadata.importedCss = new Set([...metadata.importedCss].map(replace))
|
|
218
|
+
}
|
|
219
|
+
} else if (typeof file.source === 'string') {
|
|
220
|
+
file.source = replace(file.source)
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
},
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
adapted.add(plugin)
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
},
|
|
230
|
+
},
|
|
231
|
+
}
|
|
212
232
|
}
|