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