@eightshift/frontend-libs-tailwind 4.0.0-beta.4 → 4.0.0-beta.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/CHANGELOG.md +21 -0
- package/README.md +85 -11
- package/bin/tw-sweep.mjs +35 -14
- package/bun/browser-css.mjs +487 -0
- package/bun/build.mjs +20 -1
- package/bun/cli-supervisor.mjs +70 -14
- package/bun/cli-worker.mjs +22 -5
- package/bun/css.mjs +126 -25
- package/bun/discovery.mjs +12 -1
- package/bun/editor-native.mjs +38 -0
- package/bun/editor-pure-jsx.mjs +88 -0
- package/bun/editor.mjs +33 -0
- package/bun/eightshift-discovery.mjs +102 -26
- package/bun/flat-publication.mjs +19 -184
- package/bun/index.mjs +14 -0
- package/bun/media.mjs +16 -4
- package/bun/plugin-capabilities.mjs +1 -0
- package/bun/preset.mjs +6 -4
- package/bun/publish.mjs +7 -4
- package/bun/release.mjs +56 -4
- package/bun/shared-fonts.mjs +62 -12
- package/bun/structured-publication.mjs +66 -0
- package/bun/theme-build.mjs +119 -43
- package/bun/theme-discovery.mjs +113 -9
- package/bun/watch.mjs +32 -0
- package/bun/wordpress.mjs +49 -23
- package/eslint.config.mjs +22 -1
- package/linters/base.config.mjs +1 -0
- package/linters/tw-sweep-extract.mjs +5 -0
- package/package.json +5 -5
- package/scripts/components/link-section-editor.js +1 -0
- package/scripts/editor/editor.js +1 -0
- package/scripts/editor/registration.js +3 -0
- package/scripts/editor/store.js +7 -0
- package/test/bun-discovery.test.mjs +69 -0
- package/test/bun-publication.test.mjs +144 -0
- package/test/bun-theme-build.test.mjs +12 -0
- package/test/bun-wordpress.test.mjs +82 -0
- package/test/fixtures/theme-build-regressions.mjs +585 -0
package/bun/editor-pure-jsx.mjs
CHANGED
|
@@ -32,14 +32,17 @@ function parse(source) {
|
|
|
32
32
|
],
|
|
33
33
|
{ filename: 'module.js' },
|
|
34
34
|
);
|
|
35
|
+
|
|
35
36
|
return messages.length ? undefined : ast;
|
|
36
37
|
}
|
|
37
38
|
|
|
38
39
|
function inspect(source) {
|
|
39
40
|
const ast = parse(source);
|
|
41
|
+
|
|
40
42
|
if (!ast) {
|
|
41
43
|
return;
|
|
42
44
|
}
|
|
45
|
+
|
|
43
46
|
const factories = new Set();
|
|
44
47
|
const locals = new Set();
|
|
45
48
|
const names = [];
|
|
@@ -47,18 +50,23 @@ function inspect(source) {
|
|
|
47
50
|
if (!node) {
|
|
48
51
|
return false;
|
|
49
52
|
}
|
|
53
|
+
|
|
50
54
|
if (node.type === 'Literal') {
|
|
51
55
|
return !node.regex && (node.value === null || ['string', 'number', 'boolean'].includes(typeof node.value));
|
|
52
56
|
}
|
|
57
|
+
|
|
53
58
|
if (node.type === 'UnaryExpression') {
|
|
54
59
|
return ['+', '-', '!'].includes(node.operator) && node.argument.type === 'Literal' && pure(node.argument);
|
|
55
60
|
}
|
|
61
|
+
|
|
56
62
|
if (node.type === 'ArrayExpression') {
|
|
57
63
|
return node.elements.every((element) => element === null || pure(element));
|
|
58
64
|
}
|
|
65
|
+
|
|
59
66
|
if (node.type === 'ObjectExpression') {
|
|
60
67
|
return node.properties.every((property) => property.type === 'Property' && property.kind === 'init' && !property.method && !property.computed && !property.shorthand && pure(property.value));
|
|
61
68
|
}
|
|
69
|
+
|
|
62
70
|
return (
|
|
63
71
|
node.type === 'CallExpression' &&
|
|
64
72
|
!node.optional &&
|
|
@@ -73,15 +81,18 @@ function inspect(source) {
|
|
|
73
81
|
node.arguments.slice(1).every(pure)
|
|
74
82
|
);
|
|
75
83
|
};
|
|
84
|
+
|
|
76
85
|
for (const node of ast.body) {
|
|
77
86
|
if (node.type === 'ImportDeclaration') {
|
|
78
87
|
if (node.source.value !== 'react/jsx-runtime' || !node.specifiers.length || node.attributes?.length) {
|
|
79
88
|
return;
|
|
80
89
|
}
|
|
90
|
+
|
|
81
91
|
for (const specifier of node.specifiers) {
|
|
82
92
|
if (specifier.type !== 'ImportSpecifier' || !['jsx', 'jsxs'].includes(specifier.imported.name) || factories.has(specifier.local.name) || locals.has(specifier.local.name)) {
|
|
83
93
|
return;
|
|
84
94
|
}
|
|
95
|
+
|
|
85
96
|
factories.add(specifier.local.name);
|
|
86
97
|
}
|
|
87
98
|
} else if (node.type === 'VariableDeclaration') {
|
|
@@ -89,6 +100,7 @@ function inspect(source) {
|
|
|
89
100
|
if (declaration.id.type !== 'Identifier' || locals.has(declaration.id.name) || factories.has(declaration.id.name) || !pure(declaration.init)) {
|
|
90
101
|
return;
|
|
91
102
|
}
|
|
103
|
+
|
|
92
104
|
locals.add(declaration.id.name);
|
|
93
105
|
}
|
|
94
106
|
} else if (node.type === 'ExportNamedDeclaration' && !node.source && !node.declaration) {
|
|
@@ -96,15 +108,18 @@ function inspect(source) {
|
|
|
96
108
|
if (specifier.type !== 'ExportSpecifier' || !locals.has(specifier.local.name) || specifier.exported.type !== 'Identifier') {
|
|
97
109
|
return;
|
|
98
110
|
}
|
|
111
|
+
|
|
99
112
|
names.push(specifier.exported.name);
|
|
100
113
|
}
|
|
101
114
|
} else {
|
|
102
115
|
return;
|
|
103
116
|
}
|
|
104
117
|
}
|
|
118
|
+
|
|
105
119
|
if (!factories.size || !names.length || new Set(names).size !== names.length) {
|
|
106
120
|
return;
|
|
107
121
|
}
|
|
122
|
+
|
|
108
123
|
return { ast, names };
|
|
109
124
|
}
|
|
110
125
|
|
|
@@ -117,31 +132,42 @@ function combine({ source, ast }, index) {
|
|
|
117
132
|
if (!node || typeof node !== 'object') {
|
|
118
133
|
return;
|
|
119
134
|
}
|
|
135
|
+
|
|
120
136
|
if (node.type === 'ExportNamedDeclaration') {
|
|
121
137
|
for (const specifier of node.specifiers) {
|
|
122
138
|
names.push(`${prefix}${specifier.local.name} as ${specifier.exported.name}`);
|
|
123
139
|
}
|
|
140
|
+
|
|
124
141
|
replace(node, '');
|
|
142
|
+
|
|
125
143
|
return;
|
|
126
144
|
}
|
|
145
|
+
|
|
127
146
|
if (node.type === 'ImportSpecifier') {
|
|
128
147
|
replace(node, `${node.imported.name} as ${prefix}${node.local.name}`);
|
|
148
|
+
|
|
129
149
|
return;
|
|
130
150
|
}
|
|
151
|
+
|
|
131
152
|
if (node.type === 'VariableDeclarator') {
|
|
132
153
|
replace(node.id, `${prefix}${node.id.name}`);
|
|
133
154
|
visit(node.init);
|
|
155
|
+
|
|
134
156
|
return;
|
|
135
157
|
}
|
|
158
|
+
|
|
136
159
|
if (node.type === 'CallExpression') {
|
|
137
160
|
replace(node.callee, `${prefix}${node.callee.name}`);
|
|
138
161
|
node.arguments.forEach(visit);
|
|
162
|
+
|
|
139
163
|
return;
|
|
140
164
|
}
|
|
165
|
+
|
|
141
166
|
for (const [key, value] of Object.entries(node)) {
|
|
142
167
|
if (['parent', 'tokens', 'comments', 'loc', 'range'].includes(key)) {
|
|
143
168
|
continue;
|
|
144
169
|
}
|
|
170
|
+
|
|
145
171
|
if (Array.isArray(value)) {
|
|
146
172
|
value.forEach(visit);
|
|
147
173
|
} else {
|
|
@@ -149,10 +175,13 @@ function combine({ source, ast }, index) {
|
|
|
149
175
|
}
|
|
150
176
|
}
|
|
151
177
|
};
|
|
178
|
+
|
|
152
179
|
visit(ast);
|
|
180
|
+
|
|
153
181
|
for (const edit of edits.sort((a, b) => b.start - a.start)) {
|
|
154
182
|
source = source.slice(0, edit.start) + edit.text + source.slice(edit.end);
|
|
155
183
|
}
|
|
184
|
+
|
|
156
185
|
return `${source}\nexport { ${names.join(', ')} };\n`;
|
|
157
186
|
}
|
|
158
187
|
|
|
@@ -160,45 +189,63 @@ async function analyzeGraph({ result, metadata, directory, entries }) {
|
|
|
160
189
|
const outputs = new Map(Object.entries(metadata.outputs).map(([path, info]) => [resolve(directory, path), info]));
|
|
161
190
|
const artifacts = new Map(result.outputs.map((output) => [output.path, output]));
|
|
162
191
|
const roots = [...outputs].filter(([, info]) => info.entryPoint && entries.has(resolve(info.entryPoint))).map(([path]) => path);
|
|
192
|
+
|
|
163
193
|
if (roots.length !== entries.size) {
|
|
164
194
|
throw new Error('Ambiguous initial editor roots');
|
|
165
195
|
}
|
|
196
|
+
|
|
166
197
|
const graph = new Map();
|
|
167
198
|
const scanner = new Bun.Transpiler();
|
|
199
|
+
|
|
168
200
|
for (const [path] of outputs) {
|
|
169
201
|
if (!path.endsWith('.js')) {
|
|
170
202
|
continue;
|
|
171
203
|
}
|
|
204
|
+
|
|
172
205
|
const artifact = artifacts.get(path);
|
|
206
|
+
|
|
173
207
|
if (!artifact) {
|
|
174
208
|
throw new Error('Missing native editor artifact');
|
|
175
209
|
}
|
|
210
|
+
|
|
176
211
|
const edges = [];
|
|
212
|
+
|
|
177
213
|
for (const imported of scanner.scan(await artifact.text()).imports) {
|
|
178
214
|
if (!imported.path.startsWith('.')) {
|
|
179
215
|
continue;
|
|
180
216
|
}
|
|
217
|
+
|
|
181
218
|
const target = resolve(dirname(path), imported.path);
|
|
219
|
+
|
|
182
220
|
if (!outputs.has(target) || !['import-statement', 'dynamic-import'].includes(imported.kind)) {
|
|
183
221
|
throw new Error('Unknown native editor graph edge');
|
|
184
222
|
}
|
|
223
|
+
|
|
185
224
|
edges.push({ path: target, lazy: Number(imported.kind === 'dynamic-import') });
|
|
186
225
|
}
|
|
226
|
+
|
|
187
227
|
graph.set(path, edges);
|
|
188
228
|
}
|
|
229
|
+
|
|
189
230
|
const depths = new Map();
|
|
190
231
|
const pending = roots.map((path) => ({ path, depth: 0 }));
|
|
232
|
+
|
|
191
233
|
for (let index = 0; index < pending.length; index++) {
|
|
192
234
|
const { path, depth } = pending[index];
|
|
235
|
+
|
|
193
236
|
if ((depths.get(path) ?? Infinity) <= depth) {
|
|
194
237
|
continue;
|
|
195
238
|
}
|
|
239
|
+
|
|
196
240
|
depths.set(path, depth);
|
|
241
|
+
|
|
197
242
|
for (const edge of graph.get(path) ?? []) {
|
|
198
243
|
pending.push({ path: edge.path, depth: depth + edge.lazy });
|
|
199
244
|
}
|
|
200
245
|
}
|
|
246
|
+
|
|
201
247
|
const sourceDepths = new Map();
|
|
248
|
+
|
|
202
249
|
for (const [path, info] of outputs) {
|
|
203
250
|
for (const [source, input] of Object.entries(info.inputs ?? {})) {
|
|
204
251
|
if (input.bytesInOutput > 0 && depths.has(path)) {
|
|
@@ -207,6 +254,7 @@ async function analyzeGraph({ result, metadata, directory, entries }) {
|
|
|
207
254
|
}
|
|
208
255
|
}
|
|
209
256
|
}
|
|
257
|
+
|
|
210
258
|
return { outputs, depths, sourceDepths };
|
|
211
259
|
}
|
|
212
260
|
|
|
@@ -219,6 +267,7 @@ export async function createPureJsxGroups({ result, metadata, directory, entries
|
|
|
219
267
|
.map(([path, info]) => [resolve(path), info]),
|
|
220
268
|
);
|
|
221
269
|
const occurrences = new Map();
|
|
270
|
+
|
|
222
271
|
for (const info of outputs.values()) {
|
|
223
272
|
for (const [path, input] of Object.entries(info.inputs ?? {})) {
|
|
224
273
|
if (input.bytesInOutput > 0) {
|
|
@@ -227,42 +276,58 @@ export async function createPureJsxGroups({ result, metadata, directory, entries
|
|
|
227
276
|
}
|
|
228
277
|
}
|
|
229
278
|
}
|
|
279
|
+
|
|
230
280
|
const candidates = [];
|
|
281
|
+
|
|
231
282
|
for (const [output, info] of outputs) {
|
|
232
283
|
if (info.entryPoint || !depths.has(output) || !Number.isFinite(info.bytes) || info.bytes > maxBytes) {
|
|
233
284
|
continue;
|
|
234
285
|
}
|
|
286
|
+
|
|
235
287
|
const sources = Object.entries(info.inputs ?? {})
|
|
236
288
|
.filter(([, input]) => input.bytesInOutput > 0)
|
|
237
289
|
.map(([path]) => resolve(path));
|
|
290
|
+
|
|
238
291
|
if (sources.length !== 1) {
|
|
239
292
|
continue;
|
|
240
293
|
}
|
|
294
|
+
|
|
241
295
|
const [path] = sources;
|
|
242
296
|
const input = inputs.get(path);
|
|
297
|
+
|
|
243
298
|
if (!inDependencies(path) || !path.endsWith('.js') || occurrences.get(path) !== 1 || !input || input.bytes > maxBytes) {
|
|
244
299
|
continue;
|
|
245
300
|
}
|
|
301
|
+
|
|
246
302
|
const imports = input.imports;
|
|
303
|
+
|
|
247
304
|
if (!imports.length || imports.some((item) => item.original !== 'react/jsx-runtime' || item.kind !== 'import-statement' || item.external) || new Set(imports.map((item) => item.path)).size !== 1) {
|
|
248
305
|
continue;
|
|
249
306
|
}
|
|
307
|
+
|
|
250
308
|
const source = await readFile(path, 'utf8');
|
|
251
309
|
const bytes = Buffer.byteLength(source);
|
|
252
310
|
const proof = bytes <= maxBytes && bytes === input.bytes && inspect(source);
|
|
311
|
+
|
|
253
312
|
if (proof) {
|
|
254
313
|
candidates.push({ path, source, bytes, ...proof, runtime: imports[0].path, depth: depths.get(output) });
|
|
255
314
|
}
|
|
256
315
|
}
|
|
316
|
+
|
|
257
317
|
const buckets = new Map();
|
|
318
|
+
|
|
258
319
|
for (const candidate of candidates.sort(comparePaths)) {
|
|
259
320
|
const key = JSON.stringify([dirname(candidate.path), candidate.runtime, candidate.depth]);
|
|
321
|
+
|
|
260
322
|
if (!buckets.has(key)) {
|
|
261
323
|
buckets.set(key, []);
|
|
262
324
|
}
|
|
325
|
+
|
|
263
326
|
buckets.get(key).push(candidate);
|
|
264
327
|
}
|
|
328
|
+
|
|
265
329
|
const groups = [];
|
|
330
|
+
|
|
266
331
|
for (const bucket of buckets.values()) {
|
|
267
332
|
let members = [];
|
|
268
333
|
let bytes = 0;
|
|
@@ -275,20 +340,25 @@ export async function createPureJsxGroups({ result, metadata, directory, entries
|
|
|
275
340
|
.slice(0, 20);
|
|
276
341
|
groups.push({ members, bytes, path: resolve(dirname(members[0].path), `__eightshift-pure-${id}.js`) });
|
|
277
342
|
}
|
|
343
|
+
|
|
278
344
|
members = [];
|
|
279
345
|
bytes = 0;
|
|
280
346
|
names = new Set();
|
|
281
347
|
};
|
|
348
|
+
|
|
282
349
|
for (const member of bucket) {
|
|
283
350
|
if (bytes + member.bytes > maxBytes || member.names.some((name) => names.has(name))) {
|
|
284
351
|
flush();
|
|
285
352
|
}
|
|
353
|
+
|
|
286
354
|
members.push(member);
|
|
287
355
|
bytes += member.bytes;
|
|
288
356
|
member.names.forEach((name) => names.add(name));
|
|
289
357
|
}
|
|
358
|
+
|
|
290
359
|
flush();
|
|
291
360
|
}
|
|
361
|
+
|
|
292
362
|
const byPath = new Map(groups.flatMap((group) => group.members.map((member) => [member.path, { group, member }])));
|
|
293
363
|
const byGroup = new Map(groups.map((group) => [group.path, group]));
|
|
294
364
|
const escape = (value) => value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
@@ -301,12 +371,15 @@ export async function createPureJsxGroups({ result, metadata, directory, entries
|
|
|
301
371
|
if (!isAbsolute(importer) || !importer.endsWith('.js')) {
|
|
302
372
|
return false;
|
|
303
373
|
}
|
|
374
|
+
|
|
304
375
|
if (!consumerCache.has(importer)) {
|
|
305
376
|
const source = await readFile(importer, 'utf8');
|
|
306
377
|
consumerCache.set(importer, source.includes('require.context') ? undefined : parse(source));
|
|
307
378
|
}
|
|
379
|
+
|
|
308
380
|
const ast = consumerCache.get(importer);
|
|
309
381
|
const references = ast?.body.filter((node) => node.source?.value === specifier) ?? [];
|
|
382
|
+
|
|
310
383
|
return (
|
|
311
384
|
references.length > 0 &&
|
|
312
385
|
references.every(
|
|
@@ -316,11 +389,13 @@ export async function createPureJsxGroups({ result, metadata, directory, entries
|
|
|
316
389
|
)
|
|
317
390
|
);
|
|
318
391
|
}
|
|
392
|
+
|
|
319
393
|
const plugin = {
|
|
320
394
|
name: 'eightshift-bounded-pure-jsx',
|
|
321
395
|
setup(builder) {
|
|
322
396
|
builder.onResolve({ filter: /^eightshift-pure:/ }, ({ path }) => {
|
|
323
397
|
const target = path.slice('eightshift-pure:'.length);
|
|
398
|
+
|
|
324
399
|
if (byGroup.has(target)) {
|
|
325
400
|
return { path: target, namespace: 'eightshift-pure' };
|
|
326
401
|
}
|
|
@@ -329,48 +404,61 @@ export async function createPureJsxGroups({ result, metadata, directory, entries
|
|
|
329
404
|
if (kind !== 'import-statement') {
|
|
330
405
|
return;
|
|
331
406
|
}
|
|
407
|
+
|
|
332
408
|
const matches = inputs.get(importer)?.imports.filter((item) => item.original === path && item.kind === 'import-statement' && !item.external) ?? [];
|
|
409
|
+
|
|
333
410
|
if (matches.length !== 1) {
|
|
334
411
|
return;
|
|
335
412
|
}
|
|
413
|
+
|
|
336
414
|
const candidate = byPath.get(resolve(matches[0].path));
|
|
415
|
+
|
|
337
416
|
if (candidate && (await namedOnly(importer, path))) {
|
|
338
417
|
return { path: candidate.group.path, namespace: 'eightshift-pure' };
|
|
339
418
|
}
|
|
340
419
|
});
|
|
341
420
|
builder.onLoad({ filter: sourceFilter }, async ({ path }) => {
|
|
342
421
|
const candidate = byPath.get(path);
|
|
422
|
+
|
|
343
423
|
if (!candidate) {
|
|
344
424
|
return;
|
|
345
425
|
}
|
|
426
|
+
|
|
346
427
|
if ((await readFile(path, 'utf8')) !== candidate.member.source) {
|
|
347
428
|
throw new Error('Pure JSX source changed during compilation');
|
|
348
429
|
}
|
|
430
|
+
|
|
349
431
|
return { contents: `export { ${candidate.member.names.join(', ')} } from ${JSON.stringify(`eightshift-pure:${candidate.group.path}`)};`, loader: 'js', resolveDir: dirname(path) };
|
|
350
432
|
});
|
|
351
433
|
builder.onLoad({ filter: /.*/, namespace: 'eightshift-pure' }, async ({ path }) => {
|
|
352
434
|
const group = byGroup.get(path);
|
|
435
|
+
|
|
353
436
|
for (const member of group.members) {
|
|
354
437
|
if ((await readFile(member.path, 'utf8')) !== member.source) {
|
|
355
438
|
throw new Error('Pure JSX source changed during compilation');
|
|
356
439
|
}
|
|
357
440
|
}
|
|
441
|
+
|
|
358
442
|
return { contents: group.members.map(combine).join('\n'), loader: 'js', resolveDir: dirname(path) };
|
|
359
443
|
});
|
|
360
444
|
},
|
|
361
445
|
};
|
|
446
|
+
|
|
362
447
|
return {
|
|
363
448
|
plugin,
|
|
364
449
|
groups,
|
|
365
450
|
async verify(nextMetadata, nextResult) {
|
|
366
451
|
const next = await analyzeGraph({ result: nextResult, metadata: nextMetadata, directory, entries });
|
|
452
|
+
|
|
367
453
|
for (const [source, depth] of next.sourceDepths) {
|
|
368
454
|
if (!source.startsWith('eightshift-pure:') && !byPath.has(source) && depth < (sourceDepths.get(source) ?? Infinity)) {
|
|
369
455
|
throw new Error(`Grouping would advance non-grouped source evaluation: ${source}`);
|
|
370
456
|
}
|
|
371
457
|
}
|
|
458
|
+
|
|
372
459
|
for (const group of groups) {
|
|
373
460
|
const input = nextMetadata.inputs?.[`eightshift-pure:${group.path}`];
|
|
461
|
+
|
|
374
462
|
if (!input || !Array.isArray(input.imports) || !input.imports.length || input.imports.some((item) => item.original !== 'react/jsx-runtime' || item.path !== group.members[0].runtime || item.kind !== 'import-statement' || item.external)) {
|
|
375
463
|
throw new Error('Pure JSX runtime resolution changed during compilation');
|
|
376
464
|
}
|
package/bun/editor.mjs
CHANGED
|
@@ -17,15 +17,19 @@ export async function buildEditor({ root, directory, entry, buildOptions = {}, p
|
|
|
17
17
|
if (!['independent', 'native', 'unified'].includes(sharing)) {
|
|
18
18
|
throw new TypeError('editor.sharing must be "independent", "native" or "unified"');
|
|
19
19
|
}
|
|
20
|
+
|
|
20
21
|
if (pureJsxGroups !== false && (!pureJsxGroups || typeof pureJsxGroups !== 'object' || Array.isArray(pureJsxGroups) || Object.keys(pureJsxGroups).some((key) => key !== 'maxBytes') || !Number.isSafeInteger(pureJsxGroups.maxBytes) || pureJsxGroups.maxBytes < 1 || pureJsxGroups.maxBytes > 65536)) {
|
|
21
22
|
throw new TypeError('editor.pureJsxGroups must be false or { maxBytes: integer between 1 and 65536 }');
|
|
22
23
|
}
|
|
24
|
+
|
|
23
25
|
if (pureJsxGroups && sharing !== 'unified') {
|
|
24
26
|
throw new TypeError('editor.pureJsxGroups requires unified sharing');
|
|
25
27
|
}
|
|
28
|
+
|
|
26
29
|
if (sharedPackages !== undefined) {
|
|
27
30
|
throw new TypeError('editor.sharedPackages was removed; use native or unified sharing after checking their initialization and readiness requirements');
|
|
28
31
|
}
|
|
32
|
+
|
|
29
33
|
const inside = (path) => {
|
|
30
34
|
if (typeof path !== 'string' || !path || isAbsolute(path)) {
|
|
31
35
|
throw new TypeError('Editor output paths must be relative');
|
|
@@ -60,9 +64,11 @@ export async function buildEditor({ root, directory, entry, buildOptions = {}, p
|
|
|
60
64
|
if (!/^[$A-Z_a-z][$\w]*$/.test(scriptUrlIdentifier)) {
|
|
61
65
|
throw new TypeError('Invalid editor script URL identifier');
|
|
62
66
|
}
|
|
67
|
+
|
|
63
68
|
if (Object.keys(lazy).length) {
|
|
64
69
|
throw new TypeError('Unified editor sharing uses native imports and cannot apply lazy import rewriting');
|
|
65
70
|
}
|
|
71
|
+
|
|
66
72
|
const target = resolve(root, entry);
|
|
67
73
|
const nativeDiscovery = discovery ? eightshiftDiscovery({ ...discovery, root, outputDir: inside(discovery.outputDir ?? 'discovery') }) : undefined;
|
|
68
74
|
const {
|
|
@@ -80,23 +86,30 @@ export async function buildEditor({ root, directory, entry, buildOptions = {}, p
|
|
|
80
86
|
});
|
|
81
87
|
const paths = [];
|
|
82
88
|
await mkdir(chunkOutput, { recursive: true });
|
|
89
|
+
|
|
83
90
|
for (const output of outputs) {
|
|
84
91
|
let contents = Buffer.from(await output.arrayBuffer());
|
|
92
|
+
|
|
85
93
|
if (output.sourcemap) {
|
|
86
94
|
contents = Buffer.from(`${await output.text()}\n//# sourceMappingURL=${basename(output.sourcemap.path)}\n`);
|
|
87
95
|
}
|
|
96
|
+
|
|
88
97
|
await writeFile(output.path, contents);
|
|
89
98
|
paths.push(output.path);
|
|
90
99
|
}
|
|
100
|
+
|
|
91
101
|
let imported = relative(dirname(entryOutput), resolve(chunkOutput, names.get(target)))
|
|
92
102
|
.split(sep)
|
|
93
103
|
.join('/');
|
|
104
|
+
|
|
94
105
|
if (!imported.startsWith('.')) {
|
|
95
106
|
imported = `./${imported}`;
|
|
96
107
|
}
|
|
108
|
+
|
|
97
109
|
const source = `import ${JSON.stringify(imported)};\nconst readiness = globalThis.eightshiftEditorModules;\nif (readiness?.version !== 1 || typeof readiness.complete !== 'function') throw new Error('Unified editor requires the Eightshift module readiness integration');\nreadiness.complete(import.meta.url);\n`;
|
|
98
110
|
await writeFile(entryOutput, source);
|
|
99
111
|
paths.push(entryOutput);
|
|
112
|
+
|
|
100
113
|
return { entry: entryOutput, paths, format: 'module', readiness: 'eightshift-editor-v1', ...(grouping ? { pureJsxGroups: grouping } : {}) };
|
|
101
114
|
}
|
|
102
115
|
|
|
@@ -126,23 +139,29 @@ export async function buildEditor({ root, directory, entry, buildOptions = {}, p
|
|
|
126
139
|
|
|
127
140
|
const entries = result.outputs.filter((output) => output.kind === 'entry-point');
|
|
128
141
|
const maps = result.outputs.filter((output) => output.kind === 'sourcemap');
|
|
142
|
+
|
|
129
143
|
if (entries.length !== 1 || maps.length !== (sourcemap ? 1 : 0) || result.outputs.length !== entries.length + maps.length) {
|
|
130
144
|
throw new Error(`Unexpected editor assets: ${target}`);
|
|
131
145
|
}
|
|
132
146
|
|
|
133
147
|
const map = sourcemap ? JSON.parse(await maps[0].text()) : undefined;
|
|
148
|
+
|
|
134
149
|
if (map) {
|
|
135
150
|
map.mappings = `;${map.mappings}`;
|
|
136
151
|
}
|
|
152
|
+
|
|
137
153
|
return { source: await entries[0].text(), map };
|
|
138
154
|
};
|
|
155
|
+
|
|
139
156
|
const write = async (path, source) => {
|
|
140
157
|
await mkdir(dirname(path), { recursive: true });
|
|
141
158
|
await writeFile(path, source);
|
|
142
159
|
paths.push(path);
|
|
143
160
|
};
|
|
161
|
+
|
|
144
162
|
let entryBuild = await compile(resolve(root, entry), 'iife');
|
|
145
163
|
const entryMap = `${basename(entryOutput)}.map`;
|
|
164
|
+
|
|
146
165
|
if (sharing === 'native') {
|
|
147
166
|
if (chunks.size) {
|
|
148
167
|
const nativeDiscovery = discovery ? eightshiftDiscovery({ ...discovery, root, outputDir: inside(discovery.outputDir ?? 'discovery') }) : undefined;
|
|
@@ -154,14 +173,18 @@ export async function buildEditor({ root, directory, entry, buildOptions = {}, p
|
|
|
154
173
|
buildOptions: { ...buildOptions, loader: options.loader, sourcemap: options.sourcemap },
|
|
155
174
|
plugins: [...(nativeDiscovery ? [nativeDiscovery] : []), ...plugins],
|
|
156
175
|
});
|
|
176
|
+
|
|
157
177
|
for (const [target, name] of names) {
|
|
158
178
|
chunks.set(target, name);
|
|
159
179
|
}
|
|
180
|
+
|
|
160
181
|
// Compile against final URLs rather than rewriting code after source-map generation.
|
|
161
182
|
entryBuild = await compile(resolve(root, entry), 'iife');
|
|
183
|
+
|
|
162
184
|
if (chunks.size !== names.size) {
|
|
163
185
|
throw new Error('Editor lazy entries changed during native compilation');
|
|
164
186
|
}
|
|
187
|
+
|
|
165
188
|
for (const output of outputs) {
|
|
166
189
|
if (output.sourcemap) {
|
|
167
190
|
await write(output.path, `${await output.text()}\n//# sourceMappingURL=${basename(output.sourcemap.path)}\n`);
|
|
@@ -170,10 +193,13 @@ export async function buildEditor({ root, directory, entry, buildOptions = {}, p
|
|
|
170
193
|
}
|
|
171
194
|
}
|
|
172
195
|
}
|
|
196
|
+
|
|
173
197
|
await write(entryOutput, `((${scriptUrlIdentifier}) => {\n${entryBuild.source}\n})(document.currentScript.src);\n${entryBuild.map ? `//# sourceMappingURL=${entryMap}\n` : ''}`);
|
|
198
|
+
|
|
174
199
|
if (entryBuild.map) {
|
|
175
200
|
await write(`${entryOutput}.map`, JSON.stringify(entryBuild.map));
|
|
176
201
|
}
|
|
202
|
+
|
|
177
203
|
return { entry: entryOutput, paths };
|
|
178
204
|
}
|
|
179
205
|
|
|
@@ -182,12 +208,15 @@ export async function buildEditor({ root, directory, entry, buildOptions = {}, p
|
|
|
182
208
|
for (const [target, name] of chunks) {
|
|
183
209
|
const output = inside(`${chunkDir}/${name}`);
|
|
184
210
|
let entryPath = relative(dirname(output), entryOutput).split(sep).join('/');
|
|
211
|
+
|
|
185
212
|
if (!entryPath.startsWith('.')) {
|
|
186
213
|
entryPath = `./${entryPath}`;
|
|
187
214
|
}
|
|
215
|
+
|
|
188
216
|
const chunk = await compile(target, 'esm');
|
|
189
217
|
await write(output, `const ${scriptUrlIdentifier} = new URL(${JSON.stringify(entryPath)}, import.meta.url).href;\n${chunk.source}${chunk.map ? `\n//# sourceMappingURL=${basename(output)}.map\n` : ''}`);
|
|
190
218
|
lazyPaths.push(output);
|
|
219
|
+
|
|
191
220
|
if (chunk.map) {
|
|
192
221
|
await write(`${output}.map`, JSON.stringify(chunk.map));
|
|
193
222
|
}
|
|
@@ -195,18 +224,22 @@ export async function buildEditor({ root, directory, entry, buildOptions = {}, p
|
|
|
195
224
|
|
|
196
225
|
const digest = createHash('sha256').update(entryBuild.source);
|
|
197
226
|
const contents = new Map();
|
|
227
|
+
|
|
198
228
|
for (const path of lazyPaths.sort()) {
|
|
199
229
|
const source = await readFile(path, 'utf8');
|
|
200
230
|
contents.set(path, source);
|
|
201
231
|
digest.update(path.slice(directory.length)).update('\0').update(source).update('\0');
|
|
202
232
|
}
|
|
233
|
+
|
|
203
234
|
const version = digest.digest('hex').slice(0, 20);
|
|
204
235
|
const withVersion = (source) => source.replaceAll('?v=00000000000000000000', `?v=${version}`);
|
|
236
|
+
|
|
205
237
|
for (const [path, source] of contents) {
|
|
206
238
|
await writeFile(path, withVersion(source));
|
|
207
239
|
}
|
|
208
240
|
|
|
209
241
|
await write(entryOutput, `((${scriptUrlIdentifier}) => {\n${withVersion(entryBuild.source)}\n})(document.currentScript.src);\n${entryBuild.map ? `//# sourceMappingURL=${entryMap}\n` : ''}`);
|
|
242
|
+
|
|
210
243
|
if (entryBuild.map) {
|
|
211
244
|
await write(`${entryOutput}.map`, JSON.stringify(entryBuild.map));
|
|
212
245
|
}
|