@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
package/bun/publish.mjs
ADDED
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { cp, lstat, mkdir, readFile, readdir, rename, rm, unlink, writeFile } from 'node:fs/promises';
|
|
3
|
+
import { dirname, isAbsolute, join, relative, resolve, sep } from 'node:path';
|
|
4
|
+
|
|
5
|
+
const exists = async (path) => {
|
|
6
|
+
try {
|
|
7
|
+
return await lstat(path);
|
|
8
|
+
} catch (error) {
|
|
9
|
+
if (error.code === 'ENOENT') {
|
|
10
|
+
return null;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
throw error;
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
const pathName = (value) => {
|
|
17
|
+
if (typeof value !== 'string' || !value || isAbsolute(value) || value.includes('\\') || value.includes('\0') || value.split('/').some((part) => !part || part === '.' || part === '..')) {
|
|
18
|
+
throw new Error(`Invalid manifest path: ${String(value)}`);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return value;
|
|
22
|
+
};
|
|
23
|
+
const inside = (parent, child) => {
|
|
24
|
+
const path = relative(parent, child);
|
|
25
|
+
|
|
26
|
+
return !path || (!path.startsWith(`..${sep}`) && path !== '..' && !isAbsolute(path));
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Publish a packageRelease directory without deleting unrelated public files.
|
|
31
|
+
* root is the theme root; publicDir defaults to root/public. publicPrefix is
|
|
32
|
+
* the URL prefix including its trailing slash. manifest maps keys to paths in
|
|
33
|
+
* release. Existing PHP manifest cache assets are updated;
|
|
34
|
+
* missing caches are left alone. Returns { publicDir, manifest }.
|
|
35
|
+
*/
|
|
36
|
+
export async function publishRelease({ root, release, manifest, publicDir, publicPrefix }) {
|
|
37
|
+
if (
|
|
38
|
+
typeof root !== 'string' ||
|
|
39
|
+
!root ||
|
|
40
|
+
typeof release !== 'string' ||
|
|
41
|
+
!release ||
|
|
42
|
+
!manifest ||
|
|
43
|
+
typeof manifest !== 'object' ||
|
|
44
|
+
Array.isArray(manifest) ||
|
|
45
|
+
typeof publicPrefix !== 'string' ||
|
|
46
|
+
!publicPrefix.startsWith('/') ||
|
|
47
|
+
publicPrefix.startsWith('//') ||
|
|
48
|
+
!publicPrefix.endsWith('/') ||
|
|
49
|
+
publicPrefix.split('/').includes('..')
|
|
50
|
+
) {
|
|
51
|
+
throw new TypeError('Expected root, release, manifest and an absolute publicPrefix ending in /');
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
root = resolve(root);
|
|
55
|
+
release = resolve(release);
|
|
56
|
+
publicDir = resolve(root, publicDir ?? 'public');
|
|
57
|
+
|
|
58
|
+
if (inside(publicDir, release) || inside(release, publicDir) || publicDir === root || inside(publicDir, root)) {
|
|
59
|
+
throw new Error('Release, root and public directory must be distinct');
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const cachePath = join(root, 'eightshift/manifests.json');
|
|
63
|
+
|
|
64
|
+
if (inside(publicDir, cachePath) || inside(release, cachePath)) {
|
|
65
|
+
throw new Error('Cache must be outside publication and release directories');
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const parent = dirname(publicDir);
|
|
69
|
+
const stage = join(parent, `.public-stage-${randomUUID()}`);
|
|
70
|
+
const backup = join(parent, `.public-backup-${randomUUID()}`);
|
|
71
|
+
let staged = false;
|
|
72
|
+
let backedUp = false;
|
|
73
|
+
let published = false;
|
|
74
|
+
let cacheTemporary;
|
|
75
|
+
|
|
76
|
+
try {
|
|
77
|
+
await mkdir(parent, { recursive: true });
|
|
78
|
+
const releaseInfo = await lstat(release);
|
|
79
|
+
|
|
80
|
+
if (!releaseInfo.isDirectory() || releaseInfo.isSymbolicLink()) {
|
|
81
|
+
throw new Error('Release must be a directory');
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const publicInfo = await exists(publicDir);
|
|
85
|
+
|
|
86
|
+
if (publicInfo && (!publicInfo.isDirectory() || publicInfo.isSymbolicLink())) {
|
|
87
|
+
throw new Error('Refusing to replace non-directory public path');
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const publicManifest = Object.create(null);
|
|
91
|
+
|
|
92
|
+
for (const [key, value] of Object.entries(manifest)) {
|
|
93
|
+
pathName(key);
|
|
94
|
+
pathName(value);
|
|
95
|
+
publicManifest[key] = `${publicPrefix}${value}`;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Include unaliased flat files, as the theme's legacy publisher did.
|
|
99
|
+
for (const name of await readdir(release)) {
|
|
100
|
+
const info = await lstat(join(release, name));
|
|
101
|
+
|
|
102
|
+
if (info.isSymbolicLink() || (!info.isDirectory() && !info.isFile())) {
|
|
103
|
+
throw new Error(`Unsupported release output: ${name}`);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (info.isFile() && name !== 'manifest.json' && !Object.hasOwn(publicManifest, name)) {
|
|
107
|
+
publicManifest[name] = `${publicPrefix}${name}`;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
for (const value of Object.values(manifest)) {
|
|
112
|
+
let current = release;
|
|
113
|
+
|
|
114
|
+
for (const part of value.split('/')) {
|
|
115
|
+
current = join(current, part);
|
|
116
|
+
const info = await lstat(current);
|
|
117
|
+
|
|
118
|
+
if (info.isSymbolicLink()) {
|
|
119
|
+
throw new Error(`Symlink in release path: ${value}`);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (!(await lstat(current)).isFile()) {
|
|
124
|
+
throw new Error(`Manifest target is not a file: ${value}`);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
await mkdir(stage);
|
|
129
|
+
staged = true;
|
|
130
|
+
|
|
131
|
+
if (publicInfo) {
|
|
132
|
+
await cp(publicDir, stage, { recursive: true, verbatimSymlinks: true });
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// Never follow pre-existing symlinks when overlaying release files.
|
|
136
|
+
const overlay = async (source, destination) => {
|
|
137
|
+
for (const name of await readdir(source)) {
|
|
138
|
+
const from = join(source, name);
|
|
139
|
+
const to = join(destination, name);
|
|
140
|
+
const info = await lstat(from);
|
|
141
|
+
const old = await exists(to);
|
|
142
|
+
|
|
143
|
+
if (info.isSymbolicLink() || (!info.isDirectory() && !info.isFile())) {
|
|
144
|
+
throw new Error(`Unsupported release output: ${from}`);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (old && (old.isSymbolicLink() || (info.isDirectory() ? !old.isDirectory() : !old.isFile()))) {
|
|
148
|
+
throw new Error(`Publication path conflict: ${to}`);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (info.isDirectory()) {
|
|
152
|
+
await mkdir(to, { recursive: true });
|
|
153
|
+
await overlay(from, to);
|
|
154
|
+
} else {
|
|
155
|
+
await cp(from, to);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
await overlay(release, stage);
|
|
161
|
+
const manifestPath = join(stage, 'manifest.json');
|
|
162
|
+
const manifestInfo = await exists(manifestPath);
|
|
163
|
+
|
|
164
|
+
if (manifestInfo && (!manifestInfo.isFile() || manifestInfo.isSymbolicLink())) {
|
|
165
|
+
throw new Error('Publication manifest path conflict');
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
await writeFile(manifestPath, `${JSON.stringify(publicManifest, null, 2)}\n`);
|
|
169
|
+
const cacheInfo = await exists(cachePath);
|
|
170
|
+
let cacheData;
|
|
171
|
+
|
|
172
|
+
if (cacheInfo) {
|
|
173
|
+
if (!cacheInfo.isFile() || cacheInfo.isSymbolicLink()) {
|
|
174
|
+
throw new Error('Manifest cache must be a regular file');
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
cacheData = JSON.parse(await readFile(cachePath, 'utf8'));
|
|
178
|
+
|
|
179
|
+
if (!cacheData?.assets || Array.isArray(cacheData.assets) || typeof cacheData.assets !== 'object' || !cacheData.assets.assets || Array.isArray(cacheData.assets.assets) || typeof cacheData.assets.assets !== 'object') {
|
|
180
|
+
throw new Error('Invalid manifest cache structure');
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
cacheData.assets.assets = publicManifest;
|
|
184
|
+
cacheTemporary = `${cachePath}.${randomUUID()}`;
|
|
185
|
+
await writeFile(cacheTemporary, JSON.stringify(cacheData), { flag: 'wx', mode: cacheInfo.mode });
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
if (publicInfo) {
|
|
189
|
+
await rename(publicDir, backup);
|
|
190
|
+
backedUp = true;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
await rename(stage, publicDir);
|
|
194
|
+
staged = false;
|
|
195
|
+
published = true;
|
|
196
|
+
|
|
197
|
+
if (cacheTemporary) {
|
|
198
|
+
await rename(cacheTemporary, cachePath);
|
|
199
|
+
cacheTemporary = undefined;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
if (backedUp) {
|
|
203
|
+
backedUp = false;
|
|
204
|
+
await rm(backup, { recursive: true }).catch((error) => {
|
|
205
|
+
console.error(`Could not remove publication backup ${backup}:`, error);
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
return { publicDir, manifest: { ...publicManifest } };
|
|
210
|
+
} catch (error) {
|
|
211
|
+
if (published && (await exists(publicDir))) {
|
|
212
|
+
await rename(publicDir, stage).then(() => {
|
|
213
|
+
staged = true;
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
if (backedUp) {
|
|
218
|
+
await rename(backup, publicDir);
|
|
219
|
+
backedUp = false;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
throw error;
|
|
223
|
+
} finally {
|
|
224
|
+
if (cacheTemporary) {
|
|
225
|
+
await unlink(cacheTemporary).catch((error) => {
|
|
226
|
+
if (error.code !== 'ENOENT') {
|
|
227
|
+
throw error;
|
|
228
|
+
}
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
if (staged) {
|
|
233
|
+
await rm(stage, { recursive: true, force: true });
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}
|
package/bun/release.mjs
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { createHash, randomUUID } from 'node:crypto';
|
|
2
|
+
import { lstat, mkdir, readFile, readdir, writeFile } from 'node:fs/promises';
|
|
3
|
+
import { dirname, isAbsolute, join, relative, resolve, sep } from 'node:path';
|
|
4
|
+
|
|
5
|
+
const digest = (value) => createHash('sha256').update(value).digest('hex');
|
|
6
|
+
|
|
7
|
+
async function ensureDirectory(directory, root) {
|
|
8
|
+
const location = root && relative(root, directory);
|
|
9
|
+
const withinRoot = root && (!location || (!isAbsolute(location) && location !== '..' && !location.startsWith(`..${sep}`)));
|
|
10
|
+
const parent = dirname(directory);
|
|
11
|
+
if (parent !== directory && (!withinRoot || directory !== root)) {
|
|
12
|
+
await ensureDirectory(parent, root);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
try {
|
|
16
|
+
await mkdir(directory, { recursive: directory === root });
|
|
17
|
+
} catch (error) {
|
|
18
|
+
if (error.code !== 'EEXIST') {
|
|
19
|
+
throw error;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const info = await lstat(directory);
|
|
24
|
+
if (!info.isDirectory() || info.isSymbolicLink()) {
|
|
25
|
+
throw new Error(`Release path must be a real directory: ${directory}`);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const releasePath = (value) => {
|
|
30
|
+
if (typeof value !== 'string' || !value || value.includes('\\') || value.includes('\0') || isAbsolute(value) || value.split('/').some((part) => !part || part === '.' || part === '..')) {
|
|
31
|
+
throw new Error(`Invalid release path: ${String(value)}`);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return value;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Package a WordPress release without modifying or deleting existing releases.
|
|
39
|
+
*
|
|
40
|
+
* files: [{ source, destination }] copies files or directory trees.
|
|
41
|
+
* entries: [{ name, entry, style, dependencies, script? }] generates the root
|
|
42
|
+
* scripts and WordPress asset metadata. script supplies synchronous editor code;
|
|
43
|
+
* otherwise a dynamic-import bootstrap loads entry relative to the root script.
|
|
44
|
+
* aliases maps additional flat manifest keys to packaged paths.
|
|
45
|
+
* Returns { release, version, manifest }. A failed staging directory is retained.
|
|
46
|
+
* outputDirectory writes directly to an existing directory instead of staging.
|
|
47
|
+
*/
|
|
48
|
+
export async function packageRelease({ root, files, entries, aliases = {}, outputDirectory }) {
|
|
49
|
+
if (typeof root !== 'string' || !root || !Array.isArray(files) || !Array.isArray(entries) || !entries.length || !aliases || typeof aliases !== 'object' || Array.isArray(aliases)) {
|
|
50
|
+
throw new TypeError('Expected root, files, entries and optional aliases');
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const contents = new Map();
|
|
54
|
+
const add = (path, bytes) => {
|
|
55
|
+
releasePath(path);
|
|
56
|
+
|
|
57
|
+
if (contents.has(path)) {
|
|
58
|
+
throw new Error(`Duplicate release path: ${path}`);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
contents.set(path, bytes);
|
|
62
|
+
};
|
|
63
|
+
const collect = async (source, destination) => {
|
|
64
|
+
const info = await lstat(source);
|
|
65
|
+
|
|
66
|
+
if (info.isSymbolicLink()) {
|
|
67
|
+
throw new Error(`Symlinks are not supported: ${source}`);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (info.isDirectory()) {
|
|
71
|
+
for (const name of (await readdir(source)).sort()) {
|
|
72
|
+
await collect(join(source, name), `${destination}/${name}`);
|
|
73
|
+
}
|
|
74
|
+
} else if (info.isFile()) {
|
|
75
|
+
const bytes = await readFile(source);
|
|
76
|
+
|
|
77
|
+
add(destination, bytes);
|
|
78
|
+
} else {
|
|
79
|
+
throw new Error(`Unsupported release file: ${source}`);
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
for (const file of files) {
|
|
84
|
+
if (!file || typeof file.source !== 'string' || !file.source) {
|
|
85
|
+
throw new TypeError('Each file requires a source and destination');
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
await collect(resolve(file.source), releasePath(file.destination));
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const manifest = Object.create(null);
|
|
92
|
+
const setManifest = (key, value) => {
|
|
93
|
+
releasePath(key);
|
|
94
|
+
releasePath(value);
|
|
95
|
+
|
|
96
|
+
if (Object.hasOwn(manifest, key)) {
|
|
97
|
+
throw new Error(`Duplicate manifest key: ${key}`);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
manifest[key] = value;
|
|
101
|
+
};
|
|
102
|
+
const scripts = new Map();
|
|
103
|
+
|
|
104
|
+
for (const entry of entries) {
|
|
105
|
+
if (!entry || typeof entry.name !== 'string' || !/^[A-Za-z0-9_-]+$/.test(entry.name) || !Array.isArray(entry.dependencies) || entry.dependencies.some((dependency) => typeof dependency !== 'string' || !dependency)) {
|
|
106
|
+
throw new TypeError('Invalid release entry');
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const target = releasePath(entry.entry);
|
|
110
|
+
const style = releasePath(entry.style);
|
|
111
|
+
|
|
112
|
+
if (!contents.has(target) || !contents.has(style) || !contents.get(target).length || !contents.get(style).length) {
|
|
113
|
+
throw new Error(`Missing or empty entry or style for ${entry.name}`);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (entry.script !== undefined && (typeof entry.script !== 'string' || !entry.script.trim())) {
|
|
117
|
+
throw new TypeError(`Invalid script for ${entry.name}`);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const script = entry.script ?? `(() => { const url = new URL(${JSON.stringify(target)}, document.currentScript.src); import(url.href).catch((error) => console.error(${JSON.stringify(`Failed to load ${entry.name}:`)}, error)); })();\n`;
|
|
121
|
+
|
|
122
|
+
if (scripts.has(entry.name)) {
|
|
123
|
+
throw new Error(`Duplicate entry: ${entry.name}`);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
scripts.set(entry.name, { script, dependencies: entry.dependencies });
|
|
127
|
+
const scriptName = `${entry.name}-${digest(script)}.js`;
|
|
128
|
+
const styleBytes = contents.get(style);
|
|
129
|
+
const styleName = `${entry.name}-${digest(styleBytes)}.css`;
|
|
130
|
+
|
|
131
|
+
if (contents.has(scriptName)) {
|
|
132
|
+
throw new Error(`Duplicate release path: ${scriptName}`);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
if (contents.has(styleName)) {
|
|
136
|
+
throw new Error(`Duplicate release path: ${styleName}`);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
add(styleName, styleBytes);
|
|
140
|
+
setManifest(`${entry.name}.js`, scriptName);
|
|
141
|
+
setManifest(`${entry.name}.css`, styleName);
|
|
142
|
+
setManifest(`${entry.name}.asset.json`, `${entry.name}.asset.json`);
|
|
143
|
+
setManifest(`${entry.name}.json`, `${entry.name}.asset.json`);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
for (const [key, value] of Object.entries(aliases)) {
|
|
147
|
+
if (!contents.has(value)) {
|
|
148
|
+
throw new Error(`Missing manifest target: ${value}`);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
setManifest(key, value);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const inventory = Object.fromEntries([...contents].sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)).map(([path, bytes]) => [path, digest(bytes)]));
|
|
155
|
+
const version = digest(JSON.stringify({ format: 1, inventory, scripts: [...scripts].sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)), manifest }));
|
|
156
|
+
|
|
157
|
+
for (const [name, { script, dependencies }] of scripts) {
|
|
158
|
+
add(manifest[`${name}.js`], Buffer.from(script));
|
|
159
|
+
add(`${name}.asset.json`, Buffer.from(`${JSON.stringify({ dependencies, version }, null, 2)}\n`));
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
add('manifest.json', Buffer.from(`${JSON.stringify(manifest, null, 2)}\n`));
|
|
163
|
+
const parent = resolve(root);
|
|
164
|
+
|
|
165
|
+
await ensureDirectory(parent, parent);
|
|
166
|
+
const release = outputDirectory ? resolve(outputDirectory) : join(parent, `release-${randomUUID()}`, version);
|
|
167
|
+
|
|
168
|
+
await ensureDirectory(release, parent);
|
|
169
|
+
|
|
170
|
+
for (const [path, bytes] of contents) {
|
|
171
|
+
const target = resolve(release, ...path.split('/'));
|
|
172
|
+
|
|
173
|
+
if (relative(release, target).startsWith(`..${sep}`)) {
|
|
174
|
+
throw new Error(`Invalid release path: ${path}`);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
await ensureDirectory(dirname(target), parent);
|
|
178
|
+
await writeFile(target, bytes, { flag: 'wx' });
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
return { release, version, manifest: { ...manifest } };
|
|
182
|
+
}
|
|
@@ -27,6 +27,7 @@ const addClasses = (value, breakpoints, output, isResponsive = false) => {
|
|
|
27
27
|
|
|
28
28
|
if (Array.isArray(value)) {
|
|
29
29
|
value.forEach((item) => addClasses(item, breakpoints, output, isResponsive));
|
|
30
|
+
|
|
30
31
|
return;
|
|
31
32
|
}
|
|
32
33
|
|
|
@@ -52,6 +53,7 @@ const addClasses = (value, breakpoints, output, isResponsive = false) => {
|
|
|
52
53
|
.forEach((token) => {
|
|
53
54
|
if (isResponsiveToken(token, breakpoints)) {
|
|
54
55
|
output.add(token);
|
|
56
|
+
|
|
55
57
|
return;
|
|
56
58
|
}
|
|
57
59
|
|
|
@@ -71,12 +73,14 @@ const collectTailwindClasses = (value, allowedKeys, breakpoints, output) => {
|
|
|
71
73
|
|
|
72
74
|
if (Array.isArray(value)) {
|
|
73
75
|
value.forEach((item) => collectTailwindClasses(item, allowedKeys, breakpoints, output));
|
|
76
|
+
|
|
74
77
|
return;
|
|
75
78
|
}
|
|
76
79
|
|
|
77
80
|
Object.entries(value).forEach(([key, item]) => {
|
|
78
81
|
if (allowedKeys.has(key)) {
|
|
79
82
|
addClasses(item, breakpoints, output, value.responsive === true);
|
|
83
|
+
|
|
80
84
|
return;
|
|
81
85
|
}
|
|
82
86
|
|
|
@@ -104,62 +108,37 @@ const runPreprocess = ({ projectDir, breakpoints, manifestGlob, outputFrontend,
|
|
|
104
108
|
|
|
105
109
|
matches.forEach((filePath) => {
|
|
106
110
|
const result = processManifest(filePath, breakpoints);
|
|
111
|
+
|
|
107
112
|
result.frontend.forEach((cls) => frontend.add(cls));
|
|
108
113
|
result.editor.forEach((cls) => editor.add(cls));
|
|
109
114
|
});
|
|
110
115
|
|
|
116
|
+
fs.mkdirSync(path.dirname(outputFrontend), { recursive: true });
|
|
117
|
+
fs.mkdirSync(path.dirname(outputEditor), { recursive: true });
|
|
111
118
|
fs.writeFileSync(outputFrontend, [...frontend].join(' '));
|
|
112
119
|
fs.writeFileSync(outputEditor, [...editor].join(' '));
|
|
113
120
|
};
|
|
114
121
|
|
|
115
122
|
/**
|
|
116
|
-
*
|
|
117
|
-
* files before each build and on every manifest change in watch mode.
|
|
123
|
+
* Generate responsive Tailwind class source files without a bundler.
|
|
118
124
|
*
|
|
119
|
-
* @param {object} options
|
|
120
|
-
* @param {string} options.projectDir Absolute
|
|
121
|
-
* @param {string[]} [options.breakpoints]
|
|
122
|
-
* @param {string} [options.manifestGlob]
|
|
123
|
-
* @param {{ frontend?: string, editor?: string }} [options.output]
|
|
125
|
+
* @param {object} options Compiler options.
|
|
126
|
+
* @param {string} options.projectDir Absolute project directory.
|
|
127
|
+
* @param {string[]} [options.breakpoints] Responsive breakpoint prefixes.
|
|
128
|
+
* @param {string} [options.manifestGlob] Manifest glob relative to the project.
|
|
129
|
+
* @param {{ frontend?: string, editor?: string }} [options.output] Output paths.
|
|
130
|
+
* @returns {void}
|
|
124
131
|
*/
|
|
125
|
-
export const
|
|
132
|
+
export const generateResponsiveClasses = ({ projectDir, breakpoints = DEFAULT_BREAKPOINTS, manifestGlob = DEFAULT_MANIFEST_GLOB, output = {} } = {}) => {
|
|
126
133
|
if (!projectDir) {
|
|
127
|
-
throw new Error('
|
|
134
|
+
throw new Error('generateResponsiveClasses: `projectDir` is required.');
|
|
128
135
|
}
|
|
129
136
|
|
|
130
|
-
|
|
137
|
+
runPreprocess({
|
|
131
138
|
projectDir,
|
|
132
139
|
breakpoints,
|
|
133
140
|
manifestGlob,
|
|
134
141
|
outputFrontend: output.frontend ?? path.resolve(projectDir, 'es-tw-frontend-classes.txt'),
|
|
135
142
|
outputEditor: output.editor ?? path.resolve(projectDir, 'es-tw-editor-classes.txt'),
|
|
136
|
-
};
|
|
137
|
-
|
|
138
|
-
const run = (label) => {
|
|
139
|
-
console.log(`🔄 ${label}`);
|
|
140
|
-
try {
|
|
141
|
-
runPreprocess(config);
|
|
142
|
-
console.log('✅ Done!');
|
|
143
|
-
} catch (error) {
|
|
144
|
-
console.error('❌ Failed. ', error);
|
|
145
|
-
}
|
|
146
|
-
};
|
|
147
|
-
|
|
148
|
-
return {
|
|
149
|
-
apply(compiler) {
|
|
150
|
-
compiler.hooks.beforeRun.tap('EsTwResponsiveCompiler', () => {
|
|
151
|
-
run('Generating Tailwind manifest class sources');
|
|
152
|
-
});
|
|
153
|
-
|
|
154
|
-
compiler.hooks.watchRun.tapAsync('EsTwResponsiveCompiler', ({ modifiedFiles }, callback) => {
|
|
155
|
-
const manifestChanged = modifiedFiles && [...modifiedFiles].some((file) => file.includes('manifest.json'));
|
|
156
|
-
|
|
157
|
-
if (manifestChanged) {
|
|
158
|
-
run('A manifest was modified, re-running Tailwind manifest class generation');
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
callback();
|
|
162
|
-
});
|
|
163
|
-
},
|
|
164
|
-
};
|
|
143
|
+
});
|
|
165
144
|
};
|
package/bun/ssr.mjs
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { mkdir, readFile, writeFile } from 'node:fs/promises';
|
|
2
|
+
import { basename, dirname, resolve } from 'node:path';
|
|
3
|
+
|
|
4
|
+
/** Discover SSR entries by their block/component directory name. */
|
|
5
|
+
export async function discoverSsrEntries({ root, pattern = 'src/**/assets-ssr/ssr.{js,mjs}' }) {
|
|
6
|
+
if (typeof root !== 'string' || !root || typeof pattern !== 'string' || !pattern) {
|
|
7
|
+
throw new TypeError('Expected root and a valid SSR glob pattern');
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const entries = new Map();
|
|
11
|
+
|
|
12
|
+
for await (const file of new Bun.Glob(pattern).scan({ cwd: resolve(root), onlyFiles: true })) {
|
|
13
|
+
const name = basename(dirname(dirname(file)));
|
|
14
|
+
|
|
15
|
+
if (entries.has(name)) {
|
|
16
|
+
throw new Error(`Duplicate SSR entry name: ${name}`);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
entries.set(name, resolve(root, file));
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if (!entries.size) {
|
|
23
|
+
throw new Error('No SSR entries found.');
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return new Map([...entries].sort(([a], [b]) => a.localeCompare(b)));
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Treat CSS imports as source text, matching the theme SSR build. */
|
|
30
|
+
export function ssrCssAsText() {
|
|
31
|
+
return {
|
|
32
|
+
name: 'css-as-source',
|
|
33
|
+
setup(build) {
|
|
34
|
+
build.onLoad({ filter: /\.css$/ }, async ({ path }) => ({ contents: await readFile(path, 'utf8'), loader: 'text' }));
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Build discovered SSR entries as individual Node CJS bundles. Paths are
|
|
41
|
+
* relative to root unless absolute. Caller plugins run before the CSS plugin;
|
|
42
|
+
* pass cssAsText: false to disable it. write: false returns in-memory outputs.
|
|
43
|
+
*/
|
|
44
|
+
export async function buildSsr({ root, outdir, pattern, mode = 'development', plugins = [], cssAsText = true, write = true }) {
|
|
45
|
+
if (typeof root !== 'string' || !root || typeof outdir !== 'string' || !outdir || !['development', 'production'].includes(mode) || !Array.isArray(plugins) || typeof cssAsText !== 'boolean' || typeof write !== 'boolean') {
|
|
46
|
+
throw new TypeError('Invalid SSR build options');
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const directory = resolve(root, outdir);
|
|
50
|
+
const entries = await discoverSsrEntries({ root, pattern });
|
|
51
|
+
const outputs = [];
|
|
52
|
+
|
|
53
|
+
for (const [name, entrypoint] of entries) {
|
|
54
|
+
const result = await Bun.build({
|
|
55
|
+
entrypoints: [entrypoint],
|
|
56
|
+
outdir: directory,
|
|
57
|
+
write: false,
|
|
58
|
+
naming: `${name}.js`,
|
|
59
|
+
target: 'node',
|
|
60
|
+
format: 'cjs',
|
|
61
|
+
splitting: false,
|
|
62
|
+
minify: mode === 'production',
|
|
63
|
+
sourcemap: 'none',
|
|
64
|
+
loader: { '.js': 'jsx' },
|
|
65
|
+
define: { 'process.env.NODE_ENV': JSON.stringify(mode) },
|
|
66
|
+
plugins: [...plugins, ...(cssAsText ? [ssrCssAsText()] : [])],
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
if (!result.success) {
|
|
70
|
+
throw new AggregateError(result.logs, `SSR build failed: ${name}`);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (result.outputs.length !== 1 || result.outputs[0].kind !== 'entry-point') {
|
|
74
|
+
throw new Error(`Unexpected SSR output count or kind for ${name}: ${result.outputs.length}`);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
outputs.push({ name, entrypoint, path: resolve(directory, `${name}.js`), output: result.outputs[0], logs: result.logs });
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (write) {
|
|
81
|
+
await mkdir(directory, { recursive: true });
|
|
82
|
+
|
|
83
|
+
for (const { path, output } of outputs) {
|
|
84
|
+
await writeFile(path, await output.arrayBuffer());
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return { entries, outputs };
|
|
89
|
+
}
|