@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
package/bun/editor-native.mjs
CHANGED
|
@@ -6,12 +6,14 @@ import { supportsPureJsxGrouping } from './plugin-capabilities.mjs';
|
|
|
6
6
|
const removePassDirectory = async (directory) => {
|
|
7
7
|
for (const item of await readdir(directory, { withFileTypes: true })) {
|
|
8
8
|
const path = resolve(directory, item.name);
|
|
9
|
+
|
|
9
10
|
if (item.isDirectory()) {
|
|
10
11
|
await removePassDirectory(path);
|
|
11
12
|
} else {
|
|
12
13
|
await unlink(path);
|
|
13
14
|
}
|
|
14
15
|
}
|
|
16
|
+
|
|
15
17
|
await rmdir(directory);
|
|
16
18
|
};
|
|
17
19
|
|
|
@@ -22,17 +24,22 @@ export async function buildNativeEditorChunks(options) {
|
|
|
22
24
|
// Some Bun versions write outdir despite write:false. Never expose discarded passes.
|
|
23
25
|
const privateDirectory = resolve(dirname(directory), `.eightshift-native-${randomUUID()}`);
|
|
24
26
|
await mkdir(privateDirectory);
|
|
27
|
+
|
|
25
28
|
try {
|
|
26
29
|
const result = await compileNativeEditorChunks({ ...options, directory: privateDirectory, scriptDirectory: directory });
|
|
27
30
|
const mapPath = (path) => {
|
|
28
31
|
const location = relative(privateDirectory, path);
|
|
32
|
+
|
|
29
33
|
if (!location || isAbsolute(location) || location === '..' || location.startsWith(`..${sep}`)) {
|
|
30
34
|
throw new Error(`Native editor output escapes its private directory: ${path}`);
|
|
31
35
|
}
|
|
36
|
+
|
|
32
37
|
return resolve(directory, location);
|
|
33
38
|
};
|
|
39
|
+
|
|
34
40
|
const outputs = [];
|
|
35
41
|
const byPath = new Map();
|
|
42
|
+
|
|
36
43
|
for (const output of result.outputs) {
|
|
37
44
|
// Materialize before cleanup: a BuildArtifact may be backed by its emitted file.
|
|
38
45
|
const bytes = await output.arrayBuffer();
|
|
@@ -41,19 +48,24 @@ export async function buildNativeEditorChunks(options) {
|
|
|
41
48
|
outputs.push(artifact);
|
|
42
49
|
byPath.set(output.path, artifact);
|
|
43
50
|
}
|
|
51
|
+
|
|
44
52
|
for (const [index, output] of result.outputs.entries()) {
|
|
45
53
|
if (output.sourcemap) {
|
|
46
54
|
const map = byPath.get(output.sourcemap.path);
|
|
55
|
+
|
|
47
56
|
if (!map) {
|
|
48
57
|
throw new Error('Missing native editor source map artifact');
|
|
49
58
|
}
|
|
59
|
+
|
|
50
60
|
outputs[index].sourcemap = map;
|
|
51
61
|
}
|
|
52
62
|
}
|
|
63
|
+
|
|
53
64
|
const metafile = {
|
|
54
65
|
...result.metafile,
|
|
55
66
|
outputs: Object.fromEntries(Object.entries(result.metafile.outputs).map(([path, info]) => [mapPath(resolve(privateDirectory, path)), { ...info, imports: info.imports?.map((item) => ({ ...item, path: isAbsolute(item.path) && !item.external ? mapPath(item.path) : item.path })) }])),
|
|
56
67
|
};
|
|
68
|
+
|
|
57
69
|
return { ...result, outputs, metafile };
|
|
58
70
|
} finally {
|
|
59
71
|
await removePassDirectory(privateDirectory);
|
|
@@ -62,6 +74,7 @@ export async function buildNativeEditorChunks(options) {
|
|
|
62
74
|
|
|
63
75
|
async function compileNativeEditorChunks({ chunks, directory, scriptDirectory, entryOutput, scriptUrlIdentifier, buildOptions, plugins, pureJsxGroups = false }) {
|
|
64
76
|
let entryPath = relative(scriptDirectory, entryOutput).split(sep).join('/');
|
|
77
|
+
|
|
65
78
|
if (!entryPath.startsWith('.')) {
|
|
66
79
|
entryPath = `./${entryPath}`;
|
|
67
80
|
}
|
|
@@ -88,21 +101,26 @@ async function compileNativeEditorChunks({ chunks, directory, scriptDirectory, e
|
|
|
88
101
|
|
|
89
102
|
const readMetadata = (build) => {
|
|
90
103
|
let metadata;
|
|
104
|
+
|
|
91
105
|
try {
|
|
92
106
|
metadata = typeof build.metafile === 'string' ? JSON.parse(build.metafile) : build.metafile;
|
|
107
|
+
|
|
93
108
|
if (!metadata?.outputs || typeof metadata.outputs !== 'object') {
|
|
94
109
|
throw new Error('Missing output metadata');
|
|
95
110
|
}
|
|
96
111
|
} catch (cause) {
|
|
97
112
|
throw new Error('Cannot map native editor entries: Bun returned unreadable build metadata', { cause });
|
|
98
113
|
}
|
|
114
|
+
|
|
99
115
|
if (!metadata.inputs || typeof metadata.inputs !== 'object') {
|
|
100
116
|
throw new Error('Cannot verify native editor dynamic imports: missing input metadata');
|
|
101
117
|
}
|
|
118
|
+
|
|
102
119
|
for (const [source, info] of Object.entries(metadata.inputs)) {
|
|
103
120
|
if (!Array.isArray(info?.imports)) {
|
|
104
121
|
throw new Error(`Cannot verify native editor dynamic imports in ${source}`);
|
|
105
122
|
}
|
|
123
|
+
|
|
106
124
|
// Inlined dynamic imports can return plain objects or broken self-import helpers.
|
|
107
125
|
for (const imported of info.imports) {
|
|
108
126
|
if (imported.kind === 'dynamic-import' && !imported.external) {
|
|
@@ -110,12 +128,16 @@ async function compileNativeEditorChunks({ chunks, directory, scriptDirectory, e
|
|
|
110
128
|
}
|
|
111
129
|
}
|
|
112
130
|
}
|
|
131
|
+
|
|
113
132
|
return metadata;
|
|
114
133
|
};
|
|
134
|
+
|
|
115
135
|
let metadata = readMetadata(result);
|
|
116
136
|
let grouping;
|
|
137
|
+
|
|
117
138
|
if (pureJsxGroups) {
|
|
118
139
|
let reason;
|
|
140
|
+
|
|
119
141
|
if (options.sourcemap && options.sourcemap !== 'none') {
|
|
120
142
|
reason = 'source maps require original ungrouped sources';
|
|
121
143
|
} else if (plugins.some((plugin) => !supportsPureJsxGrouping(plugin))) {
|
|
@@ -125,16 +147,20 @@ async function compileNativeEditorChunks({ chunks, directory, scriptDirectory, e
|
|
|
125
147
|
} else if (buildOptions.define?.['process.env.NODE_ENV'] !== '"production"') {
|
|
126
148
|
reason = 'only the standard production JSX runtime is supported';
|
|
127
149
|
}
|
|
150
|
+
|
|
128
151
|
if (!reason) {
|
|
129
152
|
try {
|
|
130
153
|
const { createPureJsxGroups } = await import('./editor-pure-jsx.mjs');
|
|
131
154
|
const plan = await createPureJsxGroups({ result, metadata, directory, entries: chunks, maxBytes: pureJsxGroups.maxBytes });
|
|
132
155
|
grouping = plan.summary;
|
|
156
|
+
|
|
133
157
|
if (plan.groups.length) {
|
|
134
158
|
const grouped = await Bun.build({ ...options, plugins: [plan.plugin, ...plugins] });
|
|
159
|
+
|
|
135
160
|
if (!grouped.success) {
|
|
136
161
|
throw new AggregateError(grouped.logs, 'Grouped native editor compilation failed');
|
|
137
162
|
}
|
|
163
|
+
|
|
138
164
|
const groupedMetadata = readMetadata(grouped);
|
|
139
165
|
await plan.verify(groupedMetadata, grouped);
|
|
140
166
|
result = grouped;
|
|
@@ -144,6 +170,7 @@ async function compileNativeEditorChunks({ chunks, directory, scriptDirectory, e
|
|
|
144
170
|
reason = error.message;
|
|
145
171
|
}
|
|
146
172
|
}
|
|
173
|
+
|
|
147
174
|
if (reason) {
|
|
148
175
|
console.warn(`[eightshift] Pure JSX grouping disabled: ${reason}. Using the ungrouped native build.`);
|
|
149
176
|
grouping = { maxBytes: pureJsxGroups.maxBytes, groups: [], disabled: reason };
|
|
@@ -151,34 +178,45 @@ async function compileNativeEditorChunks({ chunks, directory, scriptDirectory, e
|
|
|
151
178
|
}
|
|
152
179
|
|
|
153
180
|
const outputPaths = new Set();
|
|
181
|
+
|
|
154
182
|
for (const output of result.outputs) {
|
|
155
183
|
const location = relative(directory, output.path);
|
|
184
|
+
|
|
156
185
|
if (!location || isAbsolute(location) || location === '..' || location.startsWith(`..${sep}`)) {
|
|
157
186
|
throw new Error(`Native editor output escapes its directory: ${output.path}`);
|
|
158
187
|
}
|
|
188
|
+
|
|
159
189
|
if (!['entry-point', 'chunk', 'sourcemap'].includes(output.kind) || !/\.js(?:\.map)?$/.test(location)) {
|
|
160
190
|
throw new Error(`Unexpected native editor asset: ${output.path}`);
|
|
161
191
|
}
|
|
192
|
+
|
|
162
193
|
if (outputPaths.has(output.path)) {
|
|
163
194
|
throw new Error(`Duplicate native editor output: ${output.path}`);
|
|
164
195
|
}
|
|
196
|
+
|
|
165
197
|
outputPaths.add(output.path);
|
|
166
198
|
}
|
|
167
199
|
|
|
168
200
|
const names = new Map();
|
|
169
201
|
const entries = new Set();
|
|
202
|
+
|
|
170
203
|
for (const [file, info] of Object.entries(metadata.outputs)) {
|
|
171
204
|
if (!info.entryPoint) {
|
|
172
205
|
continue;
|
|
173
206
|
}
|
|
207
|
+
|
|
174
208
|
const target = resolve(info.entryPoint);
|
|
209
|
+
|
|
175
210
|
if (!chunks.has(target)) {
|
|
176
211
|
continue;
|
|
177
212
|
}
|
|
213
|
+
|
|
178
214
|
const output = resolve(directory, file);
|
|
215
|
+
|
|
179
216
|
if (!outputPaths.has(output) || entries.has(output)) {
|
|
180
217
|
throw new Error(`Missing or ambiguous native editor entry: ${target}`);
|
|
181
218
|
}
|
|
219
|
+
|
|
182
220
|
entries.add(output);
|
|
183
221
|
names.set(target, relative(directory, output).split(sep).join('/'));
|
|
184
222
|
}
|
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
|
}
|