@eightshift/frontend-libs-tailwind 4.0.0-beta.3 → 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 +30 -1
- package/README.md +149 -12
- 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 +171 -0
- package/bun/cli-worker.mjs +57 -0
- package/bun/cli.mjs +3 -49
- 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 +17 -5
- 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 +140 -0
- package/bun/ssr.mjs +26 -12
- package/bun/structured-publication.mjs +66 -0
- package/bun/theme-build.mjs +130 -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
|
@@ -0,0 +1,487 @@
|
|
|
1
|
+
import { createHash, randomUUID } from 'node:crypto';
|
|
2
|
+
import { link, readFile, realpath, unlink } from 'node:fs/promises';
|
|
3
|
+
import { basename, dirname, isAbsolute, relative, resolve } from 'node:path';
|
|
4
|
+
import { transform } from 'lightningcss';
|
|
5
|
+
import postcss from 'postcss';
|
|
6
|
+
import valueParser from 'postcss-value-parser';
|
|
7
|
+
import { processCss } from './css.mjs';
|
|
8
|
+
import { allowPureJsxGrouping } from './plugin-capabilities.mjs';
|
|
9
|
+
|
|
10
|
+
export const browserCssRuntime = 'eightshift:browser-css-runtime';
|
|
11
|
+
|
|
12
|
+
/** Find stylesheet inputs that the conventional compiler emits unconditionally. */
|
|
13
|
+
export async function collectConventionalCss({ root, files }) {
|
|
14
|
+
const included = new Set();
|
|
15
|
+
const pending = await Promise.all(files.map((entry) => realpath(resolve(root, typeof entry === 'string' ? entry : entry.file))));
|
|
16
|
+
|
|
17
|
+
while (pending.length) {
|
|
18
|
+
const path = pending.pop();
|
|
19
|
+
|
|
20
|
+
if (included.has(path)) {
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
included.add(path);
|
|
25
|
+
const tree = postcss.parse(await readFile(path, 'utf8'), { from: path });
|
|
26
|
+
|
|
27
|
+
for (const rule of tree.nodes) {
|
|
28
|
+
if (rule.type !== 'atrule' || rule.name.toLowerCase() !== 'import') {
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const nodes = valueParser(rule.params).nodes.filter((node) => node.type !== 'space' && node.type !== 'comment');
|
|
33
|
+
|
|
34
|
+
if (nodes.length !== 1) {
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const node = nodes[0];
|
|
39
|
+
const url =
|
|
40
|
+
node.type === 'string'
|
|
41
|
+
? node.value
|
|
42
|
+
: node.type === 'function' && node.value.toLowerCase() === 'url'
|
|
43
|
+
? valueParser
|
|
44
|
+
.stringify(node.nodes)
|
|
45
|
+
.trim()
|
|
46
|
+
.replace(/^(['"])(.*)\1$/, '$2')
|
|
47
|
+
: '';
|
|
48
|
+
|
|
49
|
+
if (!url || /^(?:[a-z][a-z\d+.-]*:|\/|#)/i.test(url)) {
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const pathname = url.split(/[?#]/, 1)[0];
|
|
54
|
+
|
|
55
|
+
try {
|
|
56
|
+
const target = await realpath(Bun.resolveSync(pathname === 'tailwindcss' ? 'tailwindcss/index.css' : pathname, dirname(path)));
|
|
57
|
+
|
|
58
|
+
if (/\.css$/i.test(target) && !included.has(target)) {
|
|
59
|
+
pending.push(target);
|
|
60
|
+
}
|
|
61
|
+
} catch {
|
|
62
|
+
// The Tailwind compiler may resolve package-specific imports differently.
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return included;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const runtimeSource = `
|
|
71
|
+
const registryKey = Symbol.for('eightshift.browser-css.v1');
|
|
72
|
+
const styles = globalThis[registryKey] ??= new Map();
|
|
73
|
+
export function loadStylesheet(href) {
|
|
74
|
+
const url = new URL(href, globalThis.location.href).href;
|
|
75
|
+
if (styles.has(url)) return styles.get(url);
|
|
76
|
+
const pending = new Promise((resolve, reject) => {
|
|
77
|
+
const link = document.createElement('link');
|
|
78
|
+
link.rel = 'stylesheet';
|
|
79
|
+
link.href = url;
|
|
80
|
+
link.onload = () => resolve();
|
|
81
|
+
link.onerror = () => {
|
|
82
|
+
link.remove();
|
|
83
|
+
reject(new Error('Failed to load stylesheet: ' + url));
|
|
84
|
+
};
|
|
85
|
+
document.head.appendChild(link);
|
|
86
|
+
}).catch((error) => {
|
|
87
|
+
if (styles.get(url) === pending) styles.delete(url);
|
|
88
|
+
throw error;
|
|
89
|
+
});
|
|
90
|
+
styles.set(url, pending);
|
|
91
|
+
return pending;
|
|
92
|
+
}
|
|
93
|
+
`;
|
|
94
|
+
|
|
95
|
+
/** Compile JS-imported CSS and attach it to the module that imports it. */
|
|
96
|
+
export function browserCssPlugin({ root, outdir, conventionalCss, extraCssByFile, responsiveFiles, retainedStyles }) {
|
|
97
|
+
const compiled = new Map();
|
|
98
|
+
const compositions = new Map();
|
|
99
|
+
const cssImports = new Map();
|
|
100
|
+
|
|
101
|
+
return allowPureJsxGrouping({
|
|
102
|
+
name: 'eightshift-browser-css',
|
|
103
|
+
setup(builder) {
|
|
104
|
+
const directory = resolve(builder.config.outdir ?? '');
|
|
105
|
+
const surface = ['frontend', 'admin'].find((name) => directory === resolve(outdir, name));
|
|
106
|
+
|
|
107
|
+
if (!surface) {
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const compileFile = (path) => {
|
|
112
|
+
const cacheKey = `${surface}\0${path}`;
|
|
113
|
+
let pending = compiled.get(cacheKey);
|
|
114
|
+
|
|
115
|
+
if (!pending) {
|
|
116
|
+
pending = compileStylesheet({ root, outdir, path, isModule: /\.module\.css$/i.test(path), responsiveFile: responsiveFiles[surface], retainedStyles, compileFile, conventionalCss: conventionalCss.get(surface), cssImports });
|
|
117
|
+
compiled.set(cacheKey, pending);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return pending;
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
builder.onResolve({ filter: /^eightshift:browser-css-runtime$/ }, () => ({ path: browserCssRuntime, namespace: 'eightshift-browser-css-runtime' }));
|
|
124
|
+
builder.onLoad({ filter: /.*/, namespace: 'eightshift-browser-css-runtime' }, () => ({ contents: runtimeSource, loader: 'js' }));
|
|
125
|
+
builder.onResolve({ filter: /\.css(?:[?#].*)?$/ }, async (args) => {
|
|
126
|
+
if (!['import-statement', 'dynamic-import', 'require-call'].includes(args.kind) || !['file', 'eightshift-browser-css'].includes(args.namespace)) {
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
let file;
|
|
131
|
+
|
|
132
|
+
try {
|
|
133
|
+
file = await realpath(Bun.resolveSync(args.path, args.resolveDir));
|
|
134
|
+
} catch (error) {
|
|
135
|
+
if (error.code === 'ENOENT' || error.code === 'MODULE_NOT_FOUND') {
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
throw error;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (args.kind === 'require-call') {
|
|
143
|
+
throw new Error(`CSS imported through require() cannot be loaded before execution: ${args.path}`);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return { path: file, namespace: 'eightshift-browser-css' };
|
|
147
|
+
});
|
|
148
|
+
builder.onLoad({ filter: /.*/, namespace: 'eightshift-browser-css' }, async ({ path }) => {
|
|
149
|
+
const isModule = /\.module\.css$/i.test(path);
|
|
150
|
+
|
|
151
|
+
if (!isModule && conventionalCss.get(surface)?.has(path)) {
|
|
152
|
+
return { contents: 'export default {};', loader: 'js' };
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const groups = surface === 'frontend' && !isModule ? extraCssByFile.get(path) : undefined;
|
|
156
|
+
|
|
157
|
+
if (groups?.length === 1 && groups[0].files.length === 1) {
|
|
158
|
+
const href = groups[0].href;
|
|
159
|
+
|
|
160
|
+
return { contents: `import { loadStylesheet } from ${JSON.stringify(browserCssRuntime)};\nawait loadStylesheet(new URL(${JSON.stringify(href)}, import.meta.url).href);\nexport default {};`, loader: 'js', resolveDir: dirname(path) };
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const result = await compileFile(path);
|
|
164
|
+
const imports = [];
|
|
165
|
+
const dependencies = new Map();
|
|
166
|
+
const mappings = result.exports ?? {};
|
|
167
|
+
const targets = new Set();
|
|
168
|
+
|
|
169
|
+
for (const item of Object.values(mappings)) {
|
|
170
|
+
for (const reference of item.composes) {
|
|
171
|
+
if (reference.type === 'dependency') {
|
|
172
|
+
targets.add(await realpath(Bun.resolveSync(reference.specifier, dirname(path))));
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
compositions.set(path, targets);
|
|
178
|
+
const pendingTargets = [...targets];
|
|
179
|
+
const visited = new Set();
|
|
180
|
+
|
|
181
|
+
while (pendingTargets.length) {
|
|
182
|
+
const target = pendingTargets.pop();
|
|
183
|
+
|
|
184
|
+
if (target === path) {
|
|
185
|
+
throw new Error(`Circular CSS module composition in ${path}`);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
if (visited.has(target)) {
|
|
189
|
+
continue;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
visited.add(target);
|
|
193
|
+
pendingTargets.push(...(compositions.get(target) ?? []));
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const byName = new Map(Object.entries(mappings).map(([key, value]) => [value.name, key]));
|
|
197
|
+
const values = new Map();
|
|
198
|
+
const expression = (key, visiting = new Set()) => {
|
|
199
|
+
if (values.has(key)) {
|
|
200
|
+
return values.get(key);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
if (visiting.has(key)) {
|
|
204
|
+
throw new Error(`Circular CSS module composition in ${path}: ${key}`);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const item = mappings[key];
|
|
208
|
+
|
|
209
|
+
if (!item) {
|
|
210
|
+
throw new Error(`Unknown CSS module composition in ${path}: ${key}`);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
visiting.add(key);
|
|
214
|
+
const parts = [JSON.stringify(item.name)];
|
|
215
|
+
|
|
216
|
+
for (const reference of item.composes) {
|
|
217
|
+
if (reference.type === 'dependency') {
|
|
218
|
+
const key = `${reference.specifier}\0${reference.name}`;
|
|
219
|
+
let variable = dependencies.get(key);
|
|
220
|
+
|
|
221
|
+
if (!variable) {
|
|
222
|
+
variable = `dependency${dependencies.size}`;
|
|
223
|
+
dependencies.set(key, variable);
|
|
224
|
+
imports.push(`import { ${JSON.stringify(reference.name)} as ${variable} } from ${JSON.stringify(reference.specifier)};`);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
parts.push(variable);
|
|
228
|
+
} else if (reference.type === 'local' && byName.has(reference.name)) {
|
|
229
|
+
parts.push(expression(byName.get(reference.name), visiting));
|
|
230
|
+
} else {
|
|
231
|
+
parts.push(JSON.stringify(reference.name));
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
visiting.delete(key);
|
|
236
|
+
const value = parts.join(' + " " + ');
|
|
237
|
+
values.set(key, value);
|
|
238
|
+
|
|
239
|
+
return value;
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
const names = Object.keys(mappings);
|
|
243
|
+
const declarations = names.map((name, index) => `const value${index} = ${expression(name)};`);
|
|
244
|
+
const namedExports = names.flatMap((name, index) => (name === 'default' ? [] : [`export { value${index} as ${JSON.stringify(name)} };`]));
|
|
245
|
+
const mapping = names.map((name, index) => `[${JSON.stringify(name)}]: value${index}`).join(',');
|
|
246
|
+
const css = result.name ? `import { loadStylesheet } from ${JSON.stringify(browserCssRuntime)};\nawait loadStylesheet(new URL(${JSON.stringify(`../css/${result.name}`)}, import.meta.url).href);` : '';
|
|
247
|
+
|
|
248
|
+
return { contents: [...imports, css, ...declarations, ...namedExports, `export default {${mapping}};`].join('\n'), loader: 'js', resolveDir: dirname(path) };
|
|
249
|
+
});
|
|
250
|
+
},
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
async function prepareScopedImports({ path, compileFile, conventionalCss, cssImports }) {
|
|
255
|
+
const tree = postcss.parse(await readFile(path, 'utf8'), { from: path });
|
|
256
|
+
const rules = [];
|
|
257
|
+
tree.walkAtRules('import', (rule) => {
|
|
258
|
+
if (rule.parent !== tree) {
|
|
259
|
+
throw new Error(`Nested CSS imports are not supported when preserving CSS module scope: ${path}`);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
rules.push(rule);
|
|
263
|
+
});
|
|
264
|
+
const lastImport = rules.length ? tree.index(rules.at(-1)) : -1;
|
|
265
|
+
const ordered = tree.nodes.filter((node, index) => rules.includes(node) || (index < lastImport && node.type === 'atrule' && node.name.toLowerCase() === 'layer' && !node.nodes));
|
|
266
|
+
const imports = [];
|
|
267
|
+
const targets = new Set();
|
|
268
|
+
|
|
269
|
+
for (const rule of ordered) {
|
|
270
|
+
if (rule.name.toLowerCase() === 'layer') {
|
|
271
|
+
imports.push({ layer: rule.toString() });
|
|
272
|
+
rule.remove();
|
|
273
|
+
continue;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
const parsed = valueParser(rule.params);
|
|
277
|
+
const node = parsed.nodes.find((item) => item.type !== 'space' && item.type !== 'comment');
|
|
278
|
+
const url =
|
|
279
|
+
node?.type === 'string'
|
|
280
|
+
? node.value
|
|
281
|
+
: node?.type === 'function' && node.value.toLowerCase() === 'url'
|
|
282
|
+
? valueParser
|
|
283
|
+
.stringify(node.nodes)
|
|
284
|
+
.trim()
|
|
285
|
+
.replace(/^(['"])(.*)\1$/, '$2')
|
|
286
|
+
: undefined;
|
|
287
|
+
|
|
288
|
+
if (!url) {
|
|
289
|
+
throw new Error(`Cannot resolve CSS import in ${path}: ${rule.params}`);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
if (/^(?:[a-z][a-z\d+.-]*:|\/|#)/i.test(url)) {
|
|
293
|
+
imports.push({ params: rule.params });
|
|
294
|
+
rule.remove();
|
|
295
|
+
continue;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
const suffixIndex = url.search(/[?#]/);
|
|
299
|
+
const pathname = suffixIndex < 0 ? url : url.slice(0, suffixIndex);
|
|
300
|
+
const suffix = suffixIndex < 0 ? '' : url.slice(suffixIndex);
|
|
301
|
+
|
|
302
|
+
if (parsed.nodes.some((item) => item.type === 'function' && ['source', 'prefix', 'theme'].includes(item.value.toLowerCase()))) {
|
|
303
|
+
throw new Error(`Import the configured Tailwind entry separately and use @reference when a stylesheet imports CSS modules: ${path}`);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
const specifier = pathname === 'tailwindcss' ? 'tailwindcss/index.css' : pathname;
|
|
307
|
+
const target = await realpath(Bun.resolveSync(specifier, dirname(path)));
|
|
308
|
+
|
|
309
|
+
if (!target.endsWith('.css')) {
|
|
310
|
+
throw new Error(`CSS import must resolve to a stylesheet: ${url}`);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
const skip = !/\.module\.css$/i.test(target) && conventionalCss?.has(target);
|
|
314
|
+
|
|
315
|
+
if (!skip) {
|
|
316
|
+
targets.add(target);
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
imports.push({ parsed, node, target, suffix, skip });
|
|
320
|
+
rule.replaceWith(postcss.atRule({ name: 'reference', params: JSON.stringify(pathname) }));
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
cssImports.set(path, targets);
|
|
324
|
+
const pending = [...targets];
|
|
325
|
+
const visited = new Set();
|
|
326
|
+
|
|
327
|
+
while (pending.length) {
|
|
328
|
+
const target = pending.pop();
|
|
329
|
+
|
|
330
|
+
if (target === path) {
|
|
331
|
+
throw new Error(`Circular CSS import in ${path}`);
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
if (visited.has(target)) {
|
|
335
|
+
continue;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
visited.add(target);
|
|
339
|
+
pending.push(...(cssImports.get(target) ?? []));
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
const statements = [];
|
|
343
|
+
|
|
344
|
+
for (const item of imports) {
|
|
345
|
+
if (item.layer) {
|
|
346
|
+
statements.push(`${item.layer};`);
|
|
347
|
+
continue;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
if (item.params) {
|
|
351
|
+
statements.push(`@import ${item.params};`);
|
|
352
|
+
continue;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
if (item.skip) {
|
|
356
|
+
continue;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
const result = await compileFile(item.target);
|
|
360
|
+
|
|
361
|
+
if (!result.name) {
|
|
362
|
+
continue;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
const href = `./${result.name}${item.suffix}`;
|
|
366
|
+
|
|
367
|
+
if (item.node.type === 'string') {
|
|
368
|
+
item.node.value = href;
|
|
369
|
+
} else {
|
|
370
|
+
item.node.nodes = [{ type: 'string', quote: '"', value: href }];
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
statements.push(`@import ${item.parsed.toString()};`);
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
return { content: tree.toString(), imports: statements.length ? `${statements.join('\n')}\n` : '' };
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
async function compileStylesheet({ root, outdir, path, isModule, responsiveFile, retainedStyles, compileFile, conventionalCss, cssImports }) {
|
|
380
|
+
const temporary = resolve(outdir, 'css', `.browser-${randomUUID()}.css`);
|
|
381
|
+
|
|
382
|
+
try {
|
|
383
|
+
let content;
|
|
384
|
+
let imported = '';
|
|
385
|
+
|
|
386
|
+
if (isModule) {
|
|
387
|
+
const prepared = await prepareScopedImports({ path, compileFile, conventionalCss, cssImports });
|
|
388
|
+
content = prepared.content;
|
|
389
|
+
imported = prepared.imports;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
const compile = (source) =>
|
|
393
|
+
processCss({
|
|
394
|
+
projectDir: root,
|
|
395
|
+
outputFile: temporary,
|
|
396
|
+
files: [
|
|
397
|
+
{
|
|
398
|
+
file: path,
|
|
399
|
+
...(source === undefined ? {} : { content: source }),
|
|
400
|
+
scan: 'auto',
|
|
401
|
+
...(responsiveFile ? { scanFiles: [responsiveFile] } : {}),
|
|
402
|
+
sources: [
|
|
403
|
+
{ pattern: '.bun-pilot/**', negated: true },
|
|
404
|
+
{ pattern: 'public/**', negated: true },
|
|
405
|
+
{ pattern: 'es-tw-*-classes.txt', negated: true },
|
|
406
|
+
],
|
|
407
|
+
},
|
|
408
|
+
],
|
|
409
|
+
production: true,
|
|
410
|
+
});
|
|
411
|
+
const result = await compile(content);
|
|
412
|
+
|
|
413
|
+
if (!isModule && result.dependencies.some((file) => /\.module\.css$/i.test(file))) {
|
|
414
|
+
const prepared = await prepareScopedImports({ path, compileFile, conventionalCss, cssImports });
|
|
415
|
+
imported = prepared.imports;
|
|
416
|
+
await compile(prepared.content);
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
let bytes = await readFile(temporary);
|
|
420
|
+
let exports;
|
|
421
|
+
|
|
422
|
+
if (isModule) {
|
|
423
|
+
let identity = relative(root, path).replaceAll('\\', '/');
|
|
424
|
+
|
|
425
|
+
if (identity.startsWith('../') || isAbsolute(identity)) {
|
|
426
|
+
const modules = identity.indexOf('/node_modules/');
|
|
427
|
+
|
|
428
|
+
if (modules !== -1) {
|
|
429
|
+
identity = identity.slice(modules + '/node_modules/'.length);
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
const name = createHash('sha256').update(identity).update('\0').update(bytes).digest('hex').slice(0, 20);
|
|
434
|
+
const result = transform({ filename: `${name}-${basename(path)}`, code: bytes, minify: true, cssModules: true });
|
|
435
|
+
bytes = result.code;
|
|
436
|
+
exports = result.exports;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
if (imported) {
|
|
440
|
+
bytes = Buffer.concat([Buffer.from(imported), bytes]);
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
if (!bytes.toString().trim()) {
|
|
444
|
+
return { exports };
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
const hash = createHash('sha256').update(bytes).digest('hex');
|
|
448
|
+
const name = `browser-${hash}.css`;
|
|
449
|
+
const destination = resolve(outdir, 'css', name);
|
|
450
|
+
await writeFileIfMissing(destination, bytes);
|
|
451
|
+
retainedStyles.add(name);
|
|
452
|
+
|
|
453
|
+
return { name, exports };
|
|
454
|
+
} finally {
|
|
455
|
+
try {
|
|
456
|
+
await unlink(temporary);
|
|
457
|
+
} catch (error) {
|
|
458
|
+
if (error.code !== 'ENOENT') {
|
|
459
|
+
throw error;
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
async function writeFileIfMissing(path, bytes) {
|
|
466
|
+
const temporary = `${path}.${randomUUID()}`;
|
|
467
|
+
|
|
468
|
+
try {
|
|
469
|
+
await Bun.write(temporary, bytes);
|
|
470
|
+
|
|
471
|
+
try {
|
|
472
|
+
await link(temporary, path);
|
|
473
|
+
} catch (error) {
|
|
474
|
+
if (error.code !== 'EEXIST' || !(await readFile(path)).equals(bytes)) {
|
|
475
|
+
throw error;
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
} finally {
|
|
479
|
+
try {
|
|
480
|
+
await unlink(temporary);
|
|
481
|
+
} catch (error) {
|
|
482
|
+
if (error.code !== 'ENOENT') {
|
|
483
|
+
throw error;
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
}
|
package/bun/build.mjs
CHANGED
|
@@ -6,6 +6,18 @@ import { generateResponsiveClasses } from './responsive.mjs';
|
|
|
6
6
|
import { buildEditor } from './editor.mjs';
|
|
7
7
|
import { wordpressPlugin } from './wordpress.mjs';
|
|
8
8
|
|
|
9
|
+
export function validateEntryPublicPaths(paths, entries) {
|
|
10
|
+
if (!paths || typeof paths !== 'object' || Array.isArray(paths)) {
|
|
11
|
+
throw new TypeError('entryPublicPaths must be an object');
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
for (const [name, path] of Object.entries(paths)) {
|
|
15
|
+
if (!Object.hasOwn(entries, name) || typeof path !== 'string' || !path.startsWith('/') || path.startsWith('//') || !path.endsWith('/') || /[\\?#\s\x00-\x1f]/.test(path) || path.split('/').some((part) => part === '.' || part === '..')) {
|
|
16
|
+
throw new TypeError(`Invalid entryPublicPaths entry: ${name}`);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
9
21
|
const markerName = '.eightshift-bun-build';
|
|
10
22
|
const markerContents = 'eightshift-bun-build-v1\n';
|
|
11
23
|
|
|
@@ -67,9 +79,11 @@ export async function clearBuildDirectory(directory, root) {
|
|
|
67
79
|
|
|
68
80
|
try {
|
|
69
81
|
const info = await lstat(directory);
|
|
82
|
+
|
|
70
83
|
if (!info.isDirectory() || info.isSymbolicLink()) {
|
|
71
84
|
throw new Error(`Output must be a real directory: ${directory}`);
|
|
72
85
|
}
|
|
86
|
+
|
|
73
87
|
await inspectTree(directory);
|
|
74
88
|
await removeTree(directory);
|
|
75
89
|
} catch (error) {
|
|
@@ -139,13 +153,14 @@ async function assertOwned(directory) {
|
|
|
139
153
|
* `direct: true` writes to a fresh output directory without staging or rollback.
|
|
140
154
|
* Generated discovery and responsive files are included in the published tree.
|
|
141
155
|
*/
|
|
142
|
-
export async function build({ root = process.cwd(), outdir, entries, wpRoot, wordpress = true, discovery, responsive, css, editor, buildOptions = {}, direct = false } = {}) {
|
|
156
|
+
export async function build({ root = process.cwd(), outdir, entries, wpRoot, wordpress = true, discovery, responsive, css, editor, buildOptions = {}, entryPublicPaths = {}, direct = false } = {}) {
|
|
143
157
|
root = resolve(root);
|
|
144
158
|
|
|
145
159
|
if (typeof outdir !== 'string' || !outdir || !entries || typeof entries !== 'object' || Array.isArray(entries) || !Object.keys(entries).length) {
|
|
146
160
|
throw new TypeError('outdir and at least one named entry are required');
|
|
147
161
|
}
|
|
148
162
|
|
|
163
|
+
validateEntryPublicPaths(entryPublicPaths, entries);
|
|
149
164
|
outdir = resolve(root, outdir);
|
|
150
165
|
const rootFromOutput = relative(outdir, root);
|
|
151
166
|
|
|
@@ -195,6 +210,7 @@ export async function build({ root = process.cwd(), outdir, entries, wpRoot, wor
|
|
|
195
210
|
|
|
196
211
|
return file;
|
|
197
212
|
};
|
|
213
|
+
|
|
198
214
|
let generatedDiscovery;
|
|
199
215
|
|
|
200
216
|
if (discovery) {
|
|
@@ -259,6 +275,7 @@ export async function build({ root = process.cwd(), outdir, entries, wpRoot, wor
|
|
|
259
275
|
splitting: true,
|
|
260
276
|
naming: { entry: '[name]-[hash].[ext]', chunk: 'chunks/[name]-[hash].[ext]', asset: 'assets/[name]-[hash].[ext]' },
|
|
261
277
|
...buildOptions,
|
|
278
|
+
...(Object.hasOwn(entryPublicPaths, name) ? { publicPath: entryPublicPaths[name] } : {}),
|
|
262
279
|
metafile: Boolean(wp) || buildOptions.metafile,
|
|
263
280
|
entrypoints: [resolve(root, entry)],
|
|
264
281
|
outdir: directory,
|
|
@@ -314,8 +331,10 @@ export async function build({ root = process.cwd(), outdir, entries, wpRoot, wor
|
|
|
314
331
|
await inspectTree(stage);
|
|
315
332
|
await writeFile(join(stage, 'manifest.json'), `${JSON.stringify(manifest, null, 2)}\n`);
|
|
316
333
|
await writeFile(join(stage, markerName), markerContents, { flag: 'wx' });
|
|
334
|
+
|
|
317
335
|
if (direct) {
|
|
318
336
|
published = true;
|
|
337
|
+
|
|
319
338
|
return { outdir, entries: manifest };
|
|
320
339
|
}
|
|
321
340
|
|