@eightshift/frontend-libs-tailwind 3.5.0 → 4.0.0-beta.1
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 +9 -0
- package/README.md +142 -0
- package/bun/build.mjs +341 -0
- package/bun/cli.mjs +77 -0
- package/bun/css.mjs +145 -0
- package/bun/discovery.mjs +121 -0
- package/bun/editor.mjs +105 -0
- package/bun/eightshift-css.mjs +66 -0
- package/bun/eightshift-discovery.mjs +135 -0
- package/bun/flat-publication.mjs +343 -0
- package/bun/index.mjs +118 -0
- package/bun/media.mjs +49 -0
- package/bun/preset.mjs +118 -0
- package/bun/publish.mjs +236 -0
- package/bun/release.mjs +182 -0
- package/{webpack/responsive-compiler.mjs → bun/responsive.mjs} +18 -39
- package/bun/ssr.mjs +89 -0
- package/bun/theme-build.mjs +464 -0
- package/bun/theme-discovery.mjs +171 -0
- package/bun/watch.mjs +211 -0
- package/bun/wordpress.mjs +173 -0
- package/package.json +9 -17
- package/bun.lock +0 -1504
- package/webpack/base.mjs +0 -126
- package/webpack/helpers.mjs +0 -55
- package/webpack/index.mjs +0 -46
- package/webpack/production.mjs +0 -60
- package/webpack/project.mjs +0 -50
|
@@ -0,0 +1,343 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { chmod, lstat, mkdir, readFile, readdir, rename, rmdir, unlink, writeFile } from 'node:fs/promises';
|
|
3
|
+
import { basename, dirname, isAbsolute, posix, relative, resolve, sep } from 'node:path';
|
|
4
|
+
import { packageRelease } from './release.mjs';
|
|
5
|
+
|
|
6
|
+
const pathName = (value) => {
|
|
7
|
+
if (typeof value !== 'string' || !value || isAbsolute(value) || value.includes('\\') || value.includes('\0') || value.split('/').some((part) => !part || part === '.' || part === '..')) {
|
|
8
|
+
throw new Error(`Invalid path: ${String(value)}`);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
return value;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
/** Collect build outputs and flatten them, rewriting relative JS, CSS and map references.
|
|
15
|
+
* sources maps output-relative paths to source files. Returns the rewrite function.
|
|
16
|
+
*/
|
|
17
|
+
export async function flattenAssets({ outdir, flatDir, directories = ['frontend', 'editor', 'admin', 'css'], extraSources = {}, editorEntry, editorScript }) {
|
|
18
|
+
const sources = new Map();
|
|
19
|
+
const collect = async (directory, prefix) => {
|
|
20
|
+
for (const item of await readdir(directory, { withFileTypes: true })) {
|
|
21
|
+
const path = `${prefix}/${item.name}`;
|
|
22
|
+
|
|
23
|
+
if (path === 'editor/discovery' || (item.isFile() && !prefix.includes('/') && item.name === `${prefix}.asset.json`)) {
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (item.isDirectory()) {
|
|
28
|
+
await collect(resolve(directory, item.name), path);
|
|
29
|
+
} else if (item.isFile()) {
|
|
30
|
+
sources.set(path, resolve(directory, item.name));
|
|
31
|
+
} else {
|
|
32
|
+
throw new Error(`Unsupported build output: ${path}`);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
for (const directory of directories) {
|
|
38
|
+
await collect(resolve(outdir, pathName(directory)), directory);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
for (const [path, source] of Object.entries(extraSources)) {
|
|
42
|
+
sources.set(pathName(path), source);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const targetsByName = new Map();
|
|
46
|
+
for (const target of sources.keys()) {
|
|
47
|
+
const name = basename(target);
|
|
48
|
+
const targets = targetsByName.get(name) ?? [];
|
|
49
|
+
targets.push(target);
|
|
50
|
+
targetsByName.set(name, targets);
|
|
51
|
+
}
|
|
52
|
+
const unusualTargets = [...targetsByName].filter(([name]) => !/^[\w.@+-]+$/.test(name)).flatMap(([, targets]) => targets);
|
|
53
|
+
const rewrite = (input, path) => {
|
|
54
|
+
let source = input;
|
|
55
|
+
const candidates = new Set(unusualTargets);
|
|
56
|
+
|
|
57
|
+
for (const match of input.matchAll(/[/'"=]([\w.@+-]+)/g)) {
|
|
58
|
+
for (const target of targetsByName.get(match[1]) ?? []) {
|
|
59
|
+
candidates.add(target);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
for (const target of candidates) {
|
|
64
|
+
const reference = posix.relative(posix.dirname(path), target);
|
|
65
|
+
|
|
66
|
+
if (!reference) {
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const variants = [reference];
|
|
71
|
+
|
|
72
|
+
if (!reference.startsWith('.')) {
|
|
73
|
+
variants.push(`./${reference}`);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (target.startsWith('editor/editor-assets/') || (target.startsWith('editor/') && path.startsWith('editor/'))) {
|
|
77
|
+
variants.push(target.slice('editor/'.length));
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
for (const value of variants) {
|
|
81
|
+
for (const quote of ['"', "'"]) {
|
|
82
|
+
source = source.replaceAll(`${quote}${value}${quote}`, `${quote}./${basename(target)}${quote}`);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
source = source.replaceAll(`sourceMappingURL=${value}`, `sourceMappingURL=./${basename(target)}`);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return source;
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
const contentFor = async (path) => {
|
|
93
|
+
const content = await readFile(sources.get(path));
|
|
94
|
+
return /\.(?:js|css|map)$/.test(basename(path)) ? Buffer.from(rewrite(content.toString(), path)) : content;
|
|
95
|
+
};
|
|
96
|
+
const outputNames = new Map();
|
|
97
|
+
for (const path of sources.keys()) {
|
|
98
|
+
if (path === editorEntry) {
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
const name = basename(path);
|
|
102
|
+
const previous = outputNames.get(name);
|
|
103
|
+
if (previous !== undefined) {
|
|
104
|
+
if (!(await contentFor(path)).equals(await contentFor(previous))) {
|
|
105
|
+
throw new Error(`Flat output collision: ${path} and ${previous}`);
|
|
106
|
+
}
|
|
107
|
+
} else {
|
|
108
|
+
outputNames.set(name, path);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
await mkdir(flatDir);
|
|
113
|
+
|
|
114
|
+
for (const [path] of sources) {
|
|
115
|
+
const name = basename(path);
|
|
116
|
+
|
|
117
|
+
if (path === editorEntry || outputNames.get(name) !== path) {
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
await writeFile(resolve(flatDir, name), await contentFor(path), { flag: 'wx' });
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (editorEntry !== undefined) {
|
|
125
|
+
await writeFile(resolve(flatDir, 'editor-entry.js'), rewrite(editorScript, editorEntry), { flag: 'wx' });
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return { sources, rewrite };
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** Package flattened files, optional SSR files and standalone assets. */
|
|
132
|
+
export async function packageFlatRelease({ root, flatDir, entries, ssrDir, ssrNames = [], standaloneAssets = new Map(), aliases = {}, outputDirectory }) {
|
|
133
|
+
const files = [];
|
|
134
|
+
|
|
135
|
+
for (const name of await readdir(flatDir)) {
|
|
136
|
+
files.push({ source: resolve(flatDir, name), destination: name });
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const extraAliases = {};
|
|
140
|
+
|
|
141
|
+
for (const name of [...ssrNames].sort()) {
|
|
142
|
+
pathName(name);
|
|
143
|
+
|
|
144
|
+
if (name.includes('/')) {
|
|
145
|
+
throw new Error(`Invalid SSR name: ${name}`);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
files.push({ source: resolve(ssrDir, `${name}.js`), destination: `ssr/${name}.js` });
|
|
149
|
+
extraAliases[`ssr/${name}.js`] = `ssr/${name}.js`;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
for (const [name, source] of standaloneAssets) {
|
|
153
|
+
pathName(name);
|
|
154
|
+
files.push({ source, destination: name });
|
|
155
|
+
extraAliases[name] = name;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return packageRelease({ root, files, entries, aliases: { ...extraAliases, ...aliases }, outputDirectory });
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** Publish a flat release and synchronize an existing PHP asset cache.
|
|
162
|
+
* Staged publication restores the previous public directory on cache failure.
|
|
163
|
+
* Direct publication writes in public and does not roll back. Missing caches are ignored.
|
|
164
|
+
*/
|
|
165
|
+
export async function publishFlatRelease({ root, wpRoot, release, manifest, version, direct = false, publicPrefix }) {
|
|
166
|
+
root = resolve(root);
|
|
167
|
+
wpRoot = resolve(wpRoot);
|
|
168
|
+
release = resolve(release);
|
|
169
|
+
const publicDir = resolve(root, 'public');
|
|
170
|
+
const within = (parent, child) => {
|
|
171
|
+
const path = relative(parent, child);
|
|
172
|
+
|
|
173
|
+
return !path || (path !== '..' && !path.startsWith(`..${sep}`) && !isAbsolute(path));
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
if ((!direct && (within(publicDir, release) || within(release, publicDir) || within(release, root))) || (direct && release !== publicDir)) {
|
|
177
|
+
throw new Error('Release, root and public directory must be distinct');
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
if (publicPrefix === undefined) {
|
|
181
|
+
const themePath = relative(wpRoot, root);
|
|
182
|
+
|
|
183
|
+
if (!themePath || themePath === '..' || themePath.startsWith(`..${sep}`) || isAbsolute(themePath)) {
|
|
184
|
+
throw new Error('Theme must be inside the WordPress root unless publicPrefix is configured');
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
publicPrefix = `/${themePath.split(sep).join('/')}/public/`;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
if (typeof publicPrefix !== 'string' || !publicPrefix.startsWith('/') || publicPrefix.startsWith('//') || !publicPrefix.endsWith('/') || publicPrefix.includes('\\') || /[?#\s\x00-\x1f]/.test(publicPrefix) || publicPrefix.split('/').some((part) => part === '.' || part === '..')) {
|
|
191
|
+
throw new Error('publicPrefix must be a root-relative URL path ending in /');
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
const info = await lstat(release);
|
|
195
|
+
|
|
196
|
+
if (!info.isDirectory() || info.isSymbolicLink()) {
|
|
197
|
+
throw new Error('Release must be a directory');
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const result = { ...manifest };
|
|
201
|
+
|
|
202
|
+
for (const [key, value] of Object.entries(result)) {
|
|
203
|
+
pathName(key);
|
|
204
|
+
pathName(value);
|
|
205
|
+
let current = release;
|
|
206
|
+
|
|
207
|
+
for (const part of value.split('/')) {
|
|
208
|
+
current = resolve(current, part);
|
|
209
|
+
const entry = await lstat(current);
|
|
210
|
+
|
|
211
|
+
if (entry.isSymbolicLink()) {
|
|
212
|
+
throw new Error(`Symlink in release path: ${value}`);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
if (!(await lstat(current)).isFile()) {
|
|
217
|
+
throw new Error(`Manifest target is not a file: ${value}`);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
for (const name of await readdir(release)) {
|
|
222
|
+
const entry = await lstat(resolve(release, name));
|
|
223
|
+
|
|
224
|
+
if (entry.isSymbolicLink() || (!entry.isFile() && !entry.isDirectory())) {
|
|
225
|
+
throw new Error(`Unsupported release output: ${name}`);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
if (name !== 'manifest.json' && name !== 'ssr' && !(direct && name === '.build') && !Object.hasOwn(result, name)) {
|
|
229
|
+
result[name] = name;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
const publicManifest = Object.fromEntries(Object.entries(result).map(([key, value]) => [key, `${publicPrefix}${value}`]));
|
|
234
|
+
|
|
235
|
+
await writeFile(resolve(release, 'manifest.json'), `${JSON.stringify(publicManifest, null, 2)}\n`);
|
|
236
|
+
const backup = resolve(root, `.public-previous-${randomUUID()}`);
|
|
237
|
+
let hadPublic = false;
|
|
238
|
+
|
|
239
|
+
if (!direct) {
|
|
240
|
+
try {
|
|
241
|
+
const publicInfo = await lstat(publicDir);
|
|
242
|
+
|
|
243
|
+
if (!publicInfo.isDirectory() || publicInfo.isSymbolicLink()) {
|
|
244
|
+
throw new Error('Refusing to replace non-directory public path');
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
await rename(publicDir, backup);
|
|
248
|
+
hadPublic = true;
|
|
249
|
+
} catch (error) {
|
|
250
|
+
if (error.code !== 'ENOENT') {
|
|
251
|
+
throw error;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
try {
|
|
257
|
+
if (!direct) {
|
|
258
|
+
await rename(release, publicDir);
|
|
259
|
+
}
|
|
260
|
+
const cachePath = resolve(root, 'eightshift/manifests.json');
|
|
261
|
+
let cacheExists = false;
|
|
262
|
+
|
|
263
|
+
try {
|
|
264
|
+
const cacheInfo = await lstat(cachePath);
|
|
265
|
+
if (!cacheInfo.isFile() || cacheInfo.isSymbolicLink()) {
|
|
266
|
+
throw new Error(`Manifest cache must be a regular file: ${cachePath}`);
|
|
267
|
+
}
|
|
268
|
+
cacheExists = true;
|
|
269
|
+
} catch (error) {
|
|
270
|
+
if (error.code !== 'ENOENT') {
|
|
271
|
+
throw error;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
if (cacheExists) {
|
|
276
|
+
let temporary;
|
|
277
|
+
|
|
278
|
+
try {
|
|
279
|
+
const cacheInfo = await lstat(cachePath);
|
|
280
|
+
if (!cacheInfo.isFile() || cacheInfo.isSymbolicLink()) {
|
|
281
|
+
throw new Error(`Manifest cache must be a regular file: ${cachePath}`);
|
|
282
|
+
}
|
|
283
|
+
const data = JSON.parse(await readFile(cachePath, 'utf8'));
|
|
284
|
+
|
|
285
|
+
if (!data?.assets || Array.isArray(data.assets) || typeof data.assets !== 'object' || !data.assets.assets || Array.isArray(data.assets.assets) || typeof data.assets.assets !== 'object') {
|
|
286
|
+
throw new Error('Invalid manifest cache structure');
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
data.assets.assets = publicManifest;
|
|
290
|
+
temporary = `${cachePath}.${randomUUID()}`;
|
|
291
|
+
await writeFile(temporary, JSON.stringify(data), { flag: 'wx', mode: cacheInfo.mode & 0o7777 });
|
|
292
|
+
await chmod(temporary, cacheInfo.mode & 0o7777);
|
|
293
|
+
await rename(temporary, cachePath);
|
|
294
|
+
temporary = undefined;
|
|
295
|
+
} finally {
|
|
296
|
+
if (temporary) {
|
|
297
|
+
await unlink(temporary).catch((error) => {
|
|
298
|
+
if (error.code !== 'ENOENT') {
|
|
299
|
+
throw error;
|
|
300
|
+
}
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
} catch (error) {
|
|
306
|
+
if (direct) {
|
|
307
|
+
throw error;
|
|
308
|
+
}
|
|
309
|
+
try {
|
|
310
|
+
await rename(publicDir, release);
|
|
311
|
+
} catch (rollbackError) {
|
|
312
|
+
if (rollbackError.code !== 'ENOENT') {
|
|
313
|
+
throw rollbackError;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
if (hadPublic) {
|
|
318
|
+
await rename(backup, publicDir);
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
throw error;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
const removeTree = async (path) => {
|
|
325
|
+
const entry = await lstat(path);
|
|
326
|
+
|
|
327
|
+
if (entry.isDirectory() && !entry.isSymbolicLink()) {
|
|
328
|
+
for (const name of await readdir(path)) {
|
|
329
|
+
await removeTree(resolve(path, name));
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
await rmdir(path);
|
|
333
|
+
} else {
|
|
334
|
+
await unlink(path);
|
|
335
|
+
}
|
|
336
|
+
};
|
|
337
|
+
|
|
338
|
+
if (hadPublic) {
|
|
339
|
+
await removeTree(backup);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
return { publicDir, manifest: publicManifest, version };
|
|
343
|
+
}
|
package/bun/index.mjs
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { basename, dirname, isAbsolute, relative, resolve, sep } from 'node:path';
|
|
2
|
+
import { buildPreset, createPreset } from './preset.mjs';
|
|
3
|
+
import { watchBuild } from './watch.mjs';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Define a reusable build. `preset` may be an object or a function of the
|
|
7
|
+
* invocation context. `ssr` and `publish` run after the browser build, in that
|
|
8
|
+
* order; neither is included in the browser build's atomic publication.
|
|
9
|
+
* Watch options are passed to watchBuild, with root and build controlled here.
|
|
10
|
+
*/
|
|
11
|
+
export function defineBuild({ root = process.cwd(), preset, ssr, publish, watch = {} } = {}) {
|
|
12
|
+
if (typeof root !== 'string' || !root) {
|
|
13
|
+
throw new TypeError('root must be a non-empty string');
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
if (!preset || (typeof preset !== 'object' && typeof preset !== 'function') || Array.isArray(preset)) {
|
|
17
|
+
throw new TypeError('preset must be an object or function');
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (ssr !== undefined && typeof ssr !== 'function') {
|
|
21
|
+
throw new TypeError('ssr must be a function');
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (publish !== undefined && typeof publish !== 'function') {
|
|
25
|
+
throw new TypeError('publish must be a function');
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (!watch || typeof watch !== 'object' || Array.isArray(watch)) {
|
|
29
|
+
throw new TypeError('watch must be an object');
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (watch.ignore !== undefined && typeof watch.ignore !== 'function') {
|
|
33
|
+
throw new TypeError('watch.ignore must be a function');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
root = resolve(root);
|
|
37
|
+
let active;
|
|
38
|
+
const outputs = new Set();
|
|
39
|
+
const run = async (options = {}) => {
|
|
40
|
+
if (active) {
|
|
41
|
+
throw new Error('A build is already running');
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
active = (async () => {
|
|
45
|
+
const context = { ...options, root };
|
|
46
|
+
const configured = typeof preset === 'function' ? await preset(context) : preset;
|
|
47
|
+
outputs.clear();
|
|
48
|
+
const output = typeof configured?.outdir === 'string' && configured.outdir ? resolve(root, configured.outdir) : undefined;
|
|
49
|
+
if (output) {
|
|
50
|
+
const rootFromOutput = relative(output, root);
|
|
51
|
+
if (!rootFromOutput || (!isAbsolute(rootFromOutput) && rootFromOutput !== '..' && !rootFromOutput.startsWith(`..${sep}`))) {
|
|
52
|
+
throw new Error('outdir must not contain the project root');
|
|
53
|
+
}
|
|
54
|
+
outputs.add(output);
|
|
55
|
+
}
|
|
56
|
+
const result = await buildPreset({ ...configured, root });
|
|
57
|
+
const state = { ...context, result };
|
|
58
|
+
|
|
59
|
+
if (ssr) {
|
|
60
|
+
state.ssr = await ssr(state);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (publish) {
|
|
64
|
+
state.publication = await publish(state);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return state;
|
|
68
|
+
})();
|
|
69
|
+
|
|
70
|
+
try {
|
|
71
|
+
return await active;
|
|
72
|
+
} finally {
|
|
73
|
+
active = undefined;
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
return {
|
|
78
|
+
build: run,
|
|
79
|
+
watch: (options = {}) => {
|
|
80
|
+
if ((ssr || publish) && watch.sources === undefined && !watch.ignored?.length) {
|
|
81
|
+
throw new Error('Watch builds with ssr or publish callbacks require explicit watch.sources or watch.ignored');
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return watchBuild({
|
|
85
|
+
sources: ['.'],
|
|
86
|
+
...watch,
|
|
87
|
+
projectDir: root,
|
|
88
|
+
ignore: (path) => {
|
|
89
|
+
if (watch.ignore?.(path)) {
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
92
|
+
for (const output of outputs) {
|
|
93
|
+
const location = relative(output, path);
|
|
94
|
+
if (!location || (!isAbsolute(location) && location !== '..' && !location.startsWith(`..${sep}`))) {
|
|
95
|
+
return true;
|
|
96
|
+
}
|
|
97
|
+
const sibling = relative(dirname(output), path).split(sep)[0];
|
|
98
|
+
if (sibling.startsWith(`.${basename(output)}-stage-`) || sibling.startsWith(`.${basename(output)}-backup-`)) {
|
|
99
|
+
return true;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return false;
|
|
103
|
+
},
|
|
104
|
+
build: async () => {
|
|
105
|
+
while (active) {
|
|
106
|
+
await active.catch(() => {});
|
|
107
|
+
}
|
|
108
|
+
return run(options);
|
|
109
|
+
},
|
|
110
|
+
});
|
|
111
|
+
},
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export { buildPreset, createPreset } from './preset.mjs';
|
|
116
|
+
export { build } from './build.mjs';
|
|
117
|
+
export { watchBuild } from './watch.mjs';
|
|
118
|
+
export { packageRelease } from './release.mjs';
|
package/bun/media.mjs
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { readFile } from 'node:fs/promises';
|
|
2
|
+
import { basename, dirname, resolve } from 'node:path';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Adapt the published UI Components smart-image worker for flat theme releases.
|
|
6
|
+
* Callbacks belong to one build and provide the worker to the publication step.
|
|
7
|
+
* Unsupported upstream worker URL forms fail rather than producing a broken URL.
|
|
8
|
+
*/
|
|
9
|
+
export function editorWorkerPlugin({ setEditorWorker, getEditorWorker }) {
|
|
10
|
+
if (typeof setEditorWorker !== 'function' || typeof getEditorWorker !== 'function') {
|
|
11
|
+
throw new TypeError('Worker callbacks are required');
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
return {
|
|
15
|
+
name: 'eightshift-editor-worker-url',
|
|
16
|
+
setup(builder) {
|
|
17
|
+
builder.onLoad({ filter: /[/\\]smart-image[/\\]smart-image\.js$/ }, async ({ path }) => {
|
|
18
|
+
if (!/[/\\]@eightshift[/\\]ui-components[/\\]/.test(path)) {
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const source = await readFile(path, 'utf8');
|
|
23
|
+
|
|
24
|
+
if (!source.includes('image-analysis-worker')) {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const pattern = /new URL\("" \+ new URL\("(\.\.\/\.\.\/assets\/image-analysis-worker-[A-Za-z0-9_-]+\.js)", import\.meta\.url\)\.href \+ "", "" \+ import\.meta\.url\)/g;
|
|
29
|
+
const matches = [...source.matchAll(pattern)];
|
|
30
|
+
|
|
31
|
+
if (matches.length !== 1) {
|
|
32
|
+
throw new Error(`Unsupported smart-image worker URL in ${path}; configure media.editorWorker to override`);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const worker = resolve(dirname(path), matches[0][1]);
|
|
36
|
+
const previous = getEditorWorker();
|
|
37
|
+
|
|
38
|
+
if (previous && (basename(previous) !== basename(worker) || !(await readFile(previous)).equals(await readFile(worker)))) {
|
|
39
|
+
throw new Error('Conflicting smart-image workers');
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
setEditorWorker(worker);
|
|
43
|
+
const base = '(typeof __eightshiftEditorScriptUrl !== "undefined" ? __eightshiftEditorScriptUrl : (() => { throw new Error("Smart-image worker requires the Eightshift editor script URL; override media.editorWorker for other bundles"); })())';
|
|
44
|
+
|
|
45
|
+
return { contents: source.replace(pattern, `new URL(${JSON.stringify(`editor-assets/${basename(worker)}`)}, ${base})`), loader: 'js', resolveDir: dirname(path) };
|
|
46
|
+
});
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
}
|
package/bun/preset.mjs
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { isAbsolute, relative, resolve, sep } from 'node:path';
|
|
2
|
+
import { build } from './build.mjs';
|
|
3
|
+
import { processEightshiftCss } from './eightshift-css.mjs';
|
|
4
|
+
|
|
5
|
+
const checkOptions = (value, keys, label) => {
|
|
6
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
7
|
+
throw new TypeError(`${label} must be an object`);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
for (const key of Object.keys(value)) {
|
|
11
|
+
if (!keys.includes(key)) {
|
|
12
|
+
throw new TypeError(`Unknown ${label} option: ${key}`);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const feature = (value, keys, label) => {
|
|
18
|
+
if (value === undefined || value === false) {
|
|
19
|
+
return undefined;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
checkOptions(value, keys, label);
|
|
23
|
+
|
|
24
|
+
return { ...value };
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Create build options for a project. No layout or entrypoints are assumed.
|
|
29
|
+
* discovery and responsive are disabled unless configured. CSS is disabled
|
|
30
|
+
* unless configured as a callback or an array of Eightshift surface options.
|
|
31
|
+
* Set wordpress: false to bundle dependencies instead of externalizing them.
|
|
32
|
+
* Configure Bun target, format, naming and splitting via buildOptions.
|
|
33
|
+
* Entry points, staging output and write mode are controlled by build.
|
|
34
|
+
* editor: { entry, filename, chunkDir, scriptUrlIdentifier, discovery, lazy }
|
|
35
|
+
* makes the named entry a synchronous classic script with lazy ESM chunks.
|
|
36
|
+
* discovery configures eightshiftDiscovery; lazy configures editorLazyImports.
|
|
37
|
+
* Both are opt-in, and all filenames and discovery behavior are overridable.
|
|
38
|
+
*/
|
|
39
|
+
export function createPreset(options = {}) {
|
|
40
|
+
checkOptions(options, ['root', 'outdir', 'entries', 'wpRoot', 'wordpress', 'discovery', 'responsive', 'css', 'editor', 'buildOptions', 'direct'], 'preset');
|
|
41
|
+
const { root = process.cwd(), outdir, entries, wpRoot, wordpress = true, buildOptions = {}, direct = false } = options;
|
|
42
|
+
|
|
43
|
+
if (typeof root !== 'string' || !root || typeof outdir !== 'string' || !outdir) {
|
|
44
|
+
throw new TypeError('root and outdir must be non-empty strings');
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (!entries || typeof entries !== 'object' || Array.isArray(entries) || !Object.keys(entries).length) {
|
|
48
|
+
throw new TypeError('entries must contain named entrypoints');
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (typeof wordpress !== 'boolean') {
|
|
52
|
+
throw new TypeError('wordpress must be a boolean');
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (wpRoot !== undefined && (typeof wpRoot !== 'string' || !wpRoot)) {
|
|
56
|
+
throw new TypeError('wpRoot must be a non-empty string');
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (!buildOptions || typeof buildOptions !== 'object' || Array.isArray(buildOptions)) {
|
|
60
|
+
throw new TypeError('buildOptions must be an object');
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
for (const key of ['entrypoints', 'outdir', 'write']) {
|
|
64
|
+
if (Object.hasOwn(buildOptions, key)) {
|
|
65
|
+
throw new TypeError(`buildOptions.${key} is controlled by build`);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const editor = feature(options.editor, ['entry', 'chunkDir', 'filename', 'scriptUrlIdentifier', 'discovery', 'lazy'], 'editor');
|
|
70
|
+
const discovery = feature(options.discovery, ['output', 'definitions'], 'discovery');
|
|
71
|
+
const responsive = feature(options.responsive, ['breakpoints', 'manifestGlob', 'output'], 'responsive');
|
|
72
|
+
|
|
73
|
+
if (responsive?.output !== undefined) {
|
|
74
|
+
checkOptions(responsive.output, ['frontend', 'editor'], 'responsive.output');
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
let css;
|
|
78
|
+
|
|
79
|
+
if (options.css !== undefined && options.css !== false) {
|
|
80
|
+
if (typeof options.css === 'function') {
|
|
81
|
+
css = options.css;
|
|
82
|
+
} else {
|
|
83
|
+
if (!Array.isArray(options.css)) {
|
|
84
|
+
throw new TypeError('css must be false, a function, or an array of surfaces');
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const surfaces = options.css.map((surface) => {
|
|
88
|
+
checkOptions(surface, ['surface', 'outputFile', 'production', 'browsers', 'blocksDir', 'manifestGlob', 'tailwindFile', 'colorThemesFile', 'applicationFile', 'groups', 'scanExclusions', 'responsiveSource', 'responsiveFile'], 'css surface');
|
|
89
|
+
|
|
90
|
+
if (!['frontend', 'editor', 'admin'].includes(surface.surface) || typeof surface.outputFile !== 'string' || !surface.outputFile || isAbsolute(surface.outputFile)) {
|
|
91
|
+
throw new TypeError('Each CSS surface requires a valid surface and relative outputFile');
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return { ...surface };
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
css = async ({ root: projectDir, outdir: stage, responsive: generated }) => {
|
|
98
|
+
for (const { outputFile, ...surface } of surfaces) {
|
|
99
|
+
const output = resolve(stage, outputFile);
|
|
100
|
+
const location = relative(stage, output);
|
|
101
|
+
|
|
102
|
+
if (!location || location === '..' || location.startsWith(`..${sep}`) || isAbsolute(location)) {
|
|
103
|
+
throw new Error(`CSS output escapes staging: ${outputFile}`);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
await processEightshiftCss({ ...surface, projectDir, outputFile: output, responsiveFile: surface.responsiveFile ?? generated?.[surface.surface] });
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return { root, outdir, entries: { ...entries }, wpRoot, wordpress, discovery, responsive, css, editor, direct, buildOptions: { ...buildOptions } };
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** Build and atomically publish a configured preset. */
|
|
116
|
+
export async function buildPreset(options) {
|
|
117
|
+
return build(createPreset(options));
|
|
118
|
+
}
|