@typestyles/build-runner 0.3.0 → 0.5.0
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/LICENSE +204 -0
- package/README.md +199 -0
- package/dist/index.cjs +521 -8
- package/dist/index.d.cts +179 -1
- package/dist/index.d.ts +179 -1
- package/dist/index.js +498 -7
- package/package.json +5 -3
package/dist/index.js
CHANGED
|
@@ -1,7 +1,478 @@
|
|
|
1
|
-
// src/
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
// src/discover.ts
|
|
2
|
+
import { existsSync } from "fs";
|
|
3
|
+
import { resolve as resolvePath } from "path";
|
|
4
|
+
var DEFAULT_EXTRACT_MODULE_BASES = [
|
|
5
|
+
"src/typestyles-entry",
|
|
6
|
+
"src/typestyles",
|
|
7
|
+
"src/styles/index",
|
|
8
|
+
"src/styles",
|
|
9
|
+
"styles/typestyles-entry",
|
|
10
|
+
"styles/typestyles"
|
|
11
|
+
];
|
|
12
|
+
var DISCOVERY_EXTENSIONS = [".ts", ".tsx", ".js", ".mjs"];
|
|
13
|
+
var DEFAULT_EXTRACT_MODULE_CANDIDATES = DEFAULT_EXTRACT_MODULE_BASES.map(
|
|
14
|
+
(base) => `${base}.ts`
|
|
15
|
+
);
|
|
16
|
+
function discoverDefaultExtractModules(root) {
|
|
17
|
+
const absRoot = resolvePath(root);
|
|
18
|
+
for (const base of DEFAULT_EXTRACT_MODULE_BASES) {
|
|
19
|
+
for (const ext of DISCOVERY_EXTENSIONS) {
|
|
20
|
+
const rel = `${base}${ext}`;
|
|
21
|
+
if (existsSync(resolvePath(absRoot, rel))) {
|
|
22
|
+
return [rel.replace(/\\/g, "/")];
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return [];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// src/namespaces.ts
|
|
30
|
+
var STYLES_COMPONENT_RE = /styles\.component\(\s*['"]([^'"]+)['"]/g;
|
|
31
|
+
var STYLES_CLASS_RE = /styles\.class\(\s*['"]([^'"]+)['"]/g;
|
|
32
|
+
var TOKENS_CREATE_RE = /tokens\.create\(\s*['"]([^'"]+)['"]/g;
|
|
33
|
+
var CREATE_THEME_RE = /(?:tokens\.)?createTheme\(\s*['"]([^'"]+)['"]/g;
|
|
34
|
+
var KEYFRAMES_CREATE_RE = /keyframes\.create\(\s*['"]([^'"]+)['"]/g;
|
|
35
|
+
var GLOBAL_STYLE_RE = /global\.style\(\s*['"]([^'"]+)['"]/g;
|
|
36
|
+
var GLOBAL_FONT_FACE_RE = /global\.fontFace\(\s*['"]([^'"]+)['"]/g;
|
|
37
|
+
var TYPESTYLES_IMPORT_RE = /(?:from\s+|import\s+|require\s*\(\s*)['"]typestyles['"]/;
|
|
38
|
+
function extractNamespaces(code) {
|
|
39
|
+
const keys = [];
|
|
40
|
+
const prefixes = [];
|
|
41
|
+
for (const match of code.matchAll(STYLES_COMPONENT_RE)) {
|
|
42
|
+
prefixes.push(`.${match[1]}-`);
|
|
43
|
+
}
|
|
44
|
+
for (const match of code.matchAll(STYLES_CLASS_RE)) {
|
|
45
|
+
prefixes.push(`.${match[1]}-`);
|
|
46
|
+
}
|
|
47
|
+
for (const match of code.matchAll(TOKENS_CREATE_RE)) {
|
|
48
|
+
keys.push(`tokens:${match[1]}`);
|
|
49
|
+
}
|
|
50
|
+
for (const match of code.matchAll(CREATE_THEME_RE)) {
|
|
51
|
+
keys.push(`theme:${match[1]}`);
|
|
52
|
+
}
|
|
53
|
+
for (const match of code.matchAll(KEYFRAMES_CREATE_RE)) {
|
|
54
|
+
keys.push(`keyframes:${match[1]}`);
|
|
55
|
+
}
|
|
56
|
+
for (const match of code.matchAll(GLOBAL_STYLE_RE)) {
|
|
57
|
+
prefixes.push(match[1]);
|
|
58
|
+
}
|
|
59
|
+
for (const match of code.matchAll(GLOBAL_FONT_FACE_RE)) {
|
|
60
|
+
prefixes.push(`font-face:${match[1]}`);
|
|
61
|
+
}
|
|
62
|
+
return { keys, prefixes };
|
|
63
|
+
}
|
|
64
|
+
function reportDuplicateNamespaces(moduleNamespaces, id, prefixes, reporter) {
|
|
65
|
+
for (const [otherId, other] of moduleNamespaces) {
|
|
66
|
+
if (otherId === id) continue;
|
|
67
|
+
for (const prefix of prefixes) {
|
|
68
|
+
if (other.prefixes.includes(prefix)) {
|
|
69
|
+
const ns = prefix.slice(1, -1);
|
|
70
|
+
reporter.error(
|
|
71
|
+
`[typestyles] Style namespace "${ns}" is also used in ${otherId}. Duplicate namespaces cause class name collisions. Use a distinct name or isolate with createStyles({ scopeId: fileScopeId(import.meta) }).`
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// src/resolve-extract.ts
|
|
79
|
+
function resolveExtractModules(root, extract) {
|
|
80
|
+
if (extract?.modules !== void 0) {
|
|
81
|
+
return extract.modules;
|
|
82
|
+
}
|
|
83
|
+
return discoverDefaultExtractModules(root);
|
|
84
|
+
}
|
|
85
|
+
function resolveExtractMode(explicitMode, resolvedModules) {
|
|
86
|
+
return explicitMode ?? (resolvedModules.length > 0 ? "build" : "runtime");
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// src/verify.ts
|
|
90
|
+
import { existsSync as existsSync2, readFileSync } from "fs";
|
|
4
91
|
import { resolve } from "path";
|
|
92
|
+
|
|
93
|
+
// src/manifest.ts
|
|
94
|
+
function isManifestV2(manifest) {
|
|
95
|
+
return manifest.version === 2;
|
|
96
|
+
}
|
|
97
|
+
function normalizeRoutePath(routePath) {
|
|
98
|
+
if (!routePath || routePath === "/") return "/";
|
|
99
|
+
const trimmed = routePath.replace(/\/+$/, "");
|
|
100
|
+
return trimmed.startsWith("/") ? trimmed : `/${trimmed}`;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// src/verify.ts
|
|
104
|
+
var VerifyTypestylesBuildError = class extends Error {
|
|
105
|
+
constructor(message, code) {
|
|
106
|
+
super(`[typestyles:verify] ${message}`);
|
|
107
|
+
this.name = "VerifyTypestylesBuildError";
|
|
108
|
+
this.code = code;
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
function fail(message, code) {
|
|
112
|
+
throw new VerifyTypestylesBuildError(message, code);
|
|
113
|
+
}
|
|
114
|
+
function readManifest(path) {
|
|
115
|
+
let parsed;
|
|
116
|
+
try {
|
|
117
|
+
parsed = JSON.parse(readFileSync(path, "utf8"));
|
|
118
|
+
} catch {
|
|
119
|
+
fail(`Invalid JSON in ${path}.`, "manifest-invalid-json");
|
|
120
|
+
}
|
|
121
|
+
if (typeof parsed !== "object" || parsed === null || !("version" in parsed) || !("css" in parsed)) {
|
|
122
|
+
fail(`Manifest at ${path} is missing required fields.`, "manifest-invalid-shape");
|
|
123
|
+
}
|
|
124
|
+
const version = parsed.version;
|
|
125
|
+
if (version !== 1 && version !== 2) {
|
|
126
|
+
fail(`Unsupported manifest version ${String(version)}.`, "manifest-version-unsupported");
|
|
127
|
+
}
|
|
128
|
+
const manifest = parsed;
|
|
129
|
+
if (isManifestV2(manifest) && typeof manifest.routes !== "object") {
|
|
130
|
+
fail(`Manifest v2 at ${path} is missing routes.`, "manifest-invalid-shape");
|
|
131
|
+
}
|
|
132
|
+
return manifest;
|
|
133
|
+
}
|
|
134
|
+
function verifyTypestylesBuild(options) {
|
|
135
|
+
const {
|
|
136
|
+
root,
|
|
137
|
+
cssFile,
|
|
138
|
+
manifestFile,
|
|
139
|
+
manifestCssPath,
|
|
140
|
+
manifestVersion = 1,
|
|
141
|
+
minBytes,
|
|
142
|
+
requiredCssSubstrings = [],
|
|
143
|
+
minRouteEntries
|
|
144
|
+
} = options;
|
|
145
|
+
const cssPath = resolve(root, cssFile);
|
|
146
|
+
if (!existsSync2(cssPath)) {
|
|
147
|
+
fail(`Missing ${cssPath} \u2014 run your TypeStyles build step first.`, "css-missing");
|
|
148
|
+
}
|
|
149
|
+
const css = readFileSync(cssPath, "utf8");
|
|
150
|
+
const cssBytes = Buffer.byteLength(css, "utf8");
|
|
151
|
+
const min = minBytes ?? 1;
|
|
152
|
+
if (cssBytes < min) {
|
|
153
|
+
fail(
|
|
154
|
+
`CSS file unexpectedly small (${cssBytes} bytes, expected at least ${min}).`,
|
|
155
|
+
"css-too-small"
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
for (const substring of requiredCssSubstrings) {
|
|
159
|
+
if (!css.includes(substring)) {
|
|
160
|
+
fail(`Expected CSS to contain ${JSON.stringify(substring)}.`, "css-missing-substring");
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
const manifestRel = manifestFile === false ? void 0 : manifestFile;
|
|
164
|
+
if (!manifestRel) {
|
|
165
|
+
return { cssPath, cssBytes };
|
|
166
|
+
}
|
|
167
|
+
const manifestPath = resolve(root, manifestRel);
|
|
168
|
+
if (!existsSync2(manifestPath)) {
|
|
169
|
+
fail(`Missing ${manifestPath}.`, "manifest-missing");
|
|
170
|
+
}
|
|
171
|
+
const manifest = readManifest(manifestPath);
|
|
172
|
+
if (manifest.version !== manifestVersion) {
|
|
173
|
+
fail(
|
|
174
|
+
`Expected manifest version ${manifestVersion}, got ${manifest.version}.`,
|
|
175
|
+
"manifest-version-mismatch"
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
const expectedCssPath = manifestCssPath ?? cssFile;
|
|
179
|
+
if (manifest.css !== expectedCssPath) {
|
|
180
|
+
fail(
|
|
181
|
+
`Expected manifest.css ${JSON.stringify(expectedCssPath)}, got ${JSON.stringify(manifest.css)}.`,
|
|
182
|
+
"manifest-css-mismatch"
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
if (isManifestV2(manifest)) {
|
|
186
|
+
const routeCount = Object.keys(manifest.routes).length;
|
|
187
|
+
if (minRouteEntries !== void 0 && routeCount < minRouteEntries) {
|
|
188
|
+
fail(
|
|
189
|
+
`Expected at least ${minRouteEntries} route CSS entries, got ${routeCount}.`,
|
|
190
|
+
"manifest-routes-too-few"
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
for (const [routePath, entry] of Object.entries(manifest.routes)) {
|
|
194
|
+
const routeCssPath = resolve(root, entry.css);
|
|
195
|
+
if (!existsSync2(routeCssPath)) {
|
|
196
|
+
fail(`Missing route CSS for ${routePath} at ${routeCssPath}.`, "route-css-missing");
|
|
197
|
+
}
|
|
198
|
+
if (Buffer.byteLength(readFileSync(routeCssPath, "utf8"), "utf8") < 1) {
|
|
199
|
+
fail(`Route CSS for ${routePath} is empty.`, "route-css-empty");
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
return {
|
|
204
|
+
cssPath,
|
|
205
|
+
cssBytes,
|
|
206
|
+
manifestPath,
|
|
207
|
+
manifestVersion: manifest.version
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// src/next-routes.ts
|
|
212
|
+
import { existsSync as existsSync3, readdirSync, statSync } from "fs";
|
|
213
|
+
import { dirname, join, relative, resolve as resolve2 } from "path";
|
|
214
|
+
var PAGE_BASENAMES = /* @__PURE__ */ new Set(["page.tsx", "page.ts", "page.jsx", "page.js"]);
|
|
215
|
+
var LAYOUT_BASENAMES = /* @__PURE__ */ new Set(["layout.tsx", "layout.ts", "layout.jsx", "layout.js"]);
|
|
216
|
+
function normalizeRelPath(root, absPath) {
|
|
217
|
+
return relative(root, absPath).replace(/\\/g, "/");
|
|
218
|
+
}
|
|
219
|
+
function isRouteGroupSegment(segment) {
|
|
220
|
+
return segment.startsWith("(") && segment.endsWith(")");
|
|
221
|
+
}
|
|
222
|
+
function pageRelPathToRoutePath(pageRelPath) {
|
|
223
|
+
const dir = dirname(pageRelPath).replace(/\\/g, "/");
|
|
224
|
+
if (dir === "." || dir === "") return "/";
|
|
225
|
+
const segments = dir.split("/").filter((segment) => segment.length > 0 && !isRouteGroupSegment(segment));
|
|
226
|
+
return segments.length === 0 ? "/" : `/${segments.join("/")}`;
|
|
227
|
+
}
|
|
228
|
+
function walkForPages(appAbs, currentRel, pages) {
|
|
229
|
+
const currentAbs = join(appAbs, currentRel);
|
|
230
|
+
let entries;
|
|
231
|
+
try {
|
|
232
|
+
entries = readdirSync(currentAbs);
|
|
233
|
+
} catch {
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
for (const entry of entries) {
|
|
237
|
+
const entryRel = currentRel ? join(currentRel, entry) : entry;
|
|
238
|
+
const entryAbs = join(appAbs, entryRel);
|
|
239
|
+
let stat;
|
|
240
|
+
try {
|
|
241
|
+
stat = statSync(entryAbs);
|
|
242
|
+
} catch {
|
|
243
|
+
continue;
|
|
244
|
+
}
|
|
245
|
+
if (stat.isDirectory()) {
|
|
246
|
+
walkForPages(appAbs, entryRel, pages);
|
|
247
|
+
continue;
|
|
248
|
+
}
|
|
249
|
+
if (PAGE_BASENAMES.has(entry)) {
|
|
250
|
+
pages.push(entryRel.replace(/\\/g, "/"));
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
function collectLayoutChain(appAbs, pageRelPath) {
|
|
255
|
+
const layouts = [];
|
|
256
|
+
const segments = dirname(pageRelPath).replace(/\\/g, "/").split("/");
|
|
257
|
+
const chainDirs = [""];
|
|
258
|
+
for (const segment of segments) {
|
|
259
|
+
if (!segment || segment === ".") continue;
|
|
260
|
+
const parent = chainDirs[chainDirs.length - 1];
|
|
261
|
+
chainDirs.push(parent ? `${parent}/${segment}` : segment);
|
|
262
|
+
}
|
|
263
|
+
for (const dir of chainDirs) {
|
|
264
|
+
const dirAbs = dir ? join(appAbs, dir) : appAbs;
|
|
265
|
+
for (const layoutName of LAYOUT_BASENAMES) {
|
|
266
|
+
const layoutAbs = join(dirAbs, layoutName);
|
|
267
|
+
if (existsSync3(layoutAbs)) {
|
|
268
|
+
layouts.push(join(dir, layoutName).replace(/\\/g, "/"));
|
|
269
|
+
break;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
return layouts;
|
|
274
|
+
}
|
|
275
|
+
function discoverNextAppRoutes(root, appDir = "app") {
|
|
276
|
+
const appAbs = resolve2(root, appDir);
|
|
277
|
+
if (!existsSync3(appAbs)) return [];
|
|
278
|
+
const pageRelPaths = [];
|
|
279
|
+
walkForPages(appAbs, "", pageRelPaths);
|
|
280
|
+
return pageRelPaths.map((pageRelPath) => {
|
|
281
|
+
const pageFile = normalizeRelPath(root, join(appAbs, pageRelPath));
|
|
282
|
+
const layoutFiles = collectLayoutChain(appAbs, pageRelPath).map(
|
|
283
|
+
(layoutRel) => normalizeRelPath(root, join(appAbs, layoutRel))
|
|
284
|
+
);
|
|
285
|
+
return {
|
|
286
|
+
routePath: pageRelPathToRoutePath(pageRelPath),
|
|
287
|
+
pageFile,
|
|
288
|
+
layoutFiles
|
|
289
|
+
};
|
|
290
|
+
}).sort((a, b) => a.routePath.localeCompare(b.routePath));
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
// src/trace-imports.ts
|
|
294
|
+
import { build as esbuildBuild } from "esbuild";
|
|
295
|
+
import { existsSync as existsSync4, readFileSync as readFileSync2 } from "fs";
|
|
296
|
+
import { join as join2, resolve as resolve3 } from "path";
|
|
297
|
+
import { pathToFileURL } from "url";
|
|
298
|
+
var EXTERNAL_PREFIXES = [
|
|
299
|
+
"react",
|
|
300
|
+
"react-dom",
|
|
301
|
+
"react/",
|
|
302
|
+
"react-dom/",
|
|
303
|
+
"next",
|
|
304
|
+
"next/",
|
|
305
|
+
"server-only",
|
|
306
|
+
"client-only",
|
|
307
|
+
"typestyles",
|
|
308
|
+
"typestyles/"
|
|
309
|
+
];
|
|
310
|
+
function isExternalBareImport(specifier) {
|
|
311
|
+
if (specifier.startsWith("node:")) return true;
|
|
312
|
+
return EXTERNAL_PREFIXES.some(
|
|
313
|
+
(prefix) => specifier === prefix || specifier.startsWith(`${prefix}/`)
|
|
314
|
+
);
|
|
315
|
+
}
|
|
316
|
+
function isExternalAssetPath(path, importer, namespace) {
|
|
317
|
+
if (path.endsWith(".css")) return true;
|
|
318
|
+
const fromCss = namespace === "css" || importer.endsWith(".css");
|
|
319
|
+
if (fromCss && path.startsWith("/") && !path.startsWith("//")) {
|
|
320
|
+
return true;
|
|
321
|
+
}
|
|
322
|
+
return false;
|
|
323
|
+
}
|
|
324
|
+
function fileImportsTypestyles(absPath) {
|
|
325
|
+
if (!existsSync4(absPath)) return false;
|
|
326
|
+
try {
|
|
327
|
+
return TYPESTYLES_IMPORT_RE.test(readFileSync2(absPath, "utf8"));
|
|
328
|
+
} catch {
|
|
329
|
+
return false;
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
async function traceTypestylesModules(root, entryFiles) {
|
|
333
|
+
const absEntries = entryFiles.map((file) => resolve3(root, file));
|
|
334
|
+
const missing = absEntries.filter((file) => !existsSync4(file));
|
|
335
|
+
if (missing.length > 0) {
|
|
336
|
+
throw new Error(
|
|
337
|
+
`[typestyles] Cannot trace imports; missing entry file(s): ${missing.join(", ")}`
|
|
338
|
+
);
|
|
339
|
+
}
|
|
340
|
+
const tsconfigPath = join2(root, "tsconfig.json");
|
|
341
|
+
const result = await esbuildBuild({
|
|
342
|
+
entryPoints: absEntries,
|
|
343
|
+
bundle: true,
|
|
344
|
+
write: false,
|
|
345
|
+
outdir: root,
|
|
346
|
+
metafile: true,
|
|
347
|
+
platform: "neutral",
|
|
348
|
+
format: "esm",
|
|
349
|
+
logLevel: "silent",
|
|
350
|
+
absWorkingDir: root,
|
|
351
|
+
...existsSync4(tsconfigPath) ? { tsconfig: tsconfigPath } : {},
|
|
352
|
+
plugins: [
|
|
353
|
+
{
|
|
354
|
+
name: "typestyles-trace-imports",
|
|
355
|
+
setup(build) {
|
|
356
|
+
build.onResolve({ filter: /.*/ }, (args) => {
|
|
357
|
+
if (isExternalAssetPath(args.path, args.importer, args.namespace)) {
|
|
358
|
+
return { path: args.path, external: true };
|
|
359
|
+
}
|
|
360
|
+
if (args.path.startsWith(".")) {
|
|
361
|
+
return null;
|
|
362
|
+
}
|
|
363
|
+
if (isExternalBareImport(args.path)) {
|
|
364
|
+
return { path: args.path, external: true };
|
|
365
|
+
}
|
|
366
|
+
return null;
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
]
|
|
371
|
+
});
|
|
372
|
+
const inputs = result.metafile?.inputs ?? {};
|
|
373
|
+
const traced = /* @__PURE__ */ new Set();
|
|
374
|
+
for (const inputPath of Object.keys(inputs)) {
|
|
375
|
+
const absPath = resolve3(root, inputPath);
|
|
376
|
+
if (!fileImportsTypestyles(absPath)) continue;
|
|
377
|
+
traced.add(inputPath.replace(/\\/g, "/"));
|
|
378
|
+
}
|
|
379
|
+
return [...traced].sort();
|
|
380
|
+
}
|
|
381
|
+
function createModuleLoader(root, modulePath) {
|
|
382
|
+
const abs = resolve3(root, modulePath);
|
|
383
|
+
const href = pathToFileURL(abs).href;
|
|
384
|
+
return () => import(href);
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
// src/route-css.ts
|
|
388
|
+
import { mkdir, writeFile } from "fs/promises";
|
|
389
|
+
import { dirname as dirname2, resolve as resolve4 } from "path";
|
|
390
|
+
function routePathToCssFile(routePath) {
|
|
391
|
+
if (routePath === "/") return "index.css";
|
|
392
|
+
const slug = routePath.slice(1).replace(/^\[|\]$/g, "").replace(/[^\w.-]+/g, "-");
|
|
393
|
+
return `${slug || "route"}.css`;
|
|
394
|
+
}
|
|
395
|
+
function uniqueModules(...groups) {
|
|
396
|
+
return [...new Set(groups.flat())].sort();
|
|
397
|
+
}
|
|
398
|
+
async function collectAndWriteRouteCss(options) {
|
|
399
|
+
const {
|
|
400
|
+
root,
|
|
401
|
+
sharedModules,
|
|
402
|
+
appDir = "app",
|
|
403
|
+
routeCssOutDir = "app/_typestyles/routes",
|
|
404
|
+
collectCss
|
|
405
|
+
} = options;
|
|
406
|
+
const routes = discoverNextAppRoutes(root, appDir);
|
|
407
|
+
if (routes.length === 0) {
|
|
408
|
+
return { routes: {} };
|
|
409
|
+
}
|
|
410
|
+
const manifestRoutes = {};
|
|
411
|
+
for (const route of routes) {
|
|
412
|
+
const entryFiles = uniqueModules(route.layoutFiles, [route.pageFile]);
|
|
413
|
+
const routeModules = await traceTypestylesModules(root, entryFiles);
|
|
414
|
+
const modules = uniqueModules(sharedModules, routeModules);
|
|
415
|
+
const loaders = modules.map((mod) => createModuleLoader(root, mod));
|
|
416
|
+
const css = await collectCss(loaders);
|
|
417
|
+
const cssFileName = routePathToCssFile(route.routePath);
|
|
418
|
+
const cssRel = `${routeCssOutDir.replace(/\\/g, "/")}/${cssFileName}`;
|
|
419
|
+
const cssAbs = resolve4(root, cssRel);
|
|
420
|
+
await mkdir(dirname2(cssAbs), { recursive: true });
|
|
421
|
+
await writeFile(cssAbs, css, "utf8");
|
|
422
|
+
manifestRoutes[route.routePath] = { css: cssRel };
|
|
423
|
+
}
|
|
424
|
+
return { routes: manifestRoutes };
|
|
425
|
+
}
|
|
426
|
+
function buildManifestV2(cssOutFile, routeEntries) {
|
|
427
|
+
return {
|
|
428
|
+
version: 2,
|
|
429
|
+
css: cssOutFile,
|
|
430
|
+
routes: routeEntries
|
|
431
|
+
};
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
// src/route-css-read.ts
|
|
435
|
+
import { existsSync as existsSync5, readFileSync as readFileSync3 } from "fs";
|
|
436
|
+
import { resolve as resolve5 } from "path";
|
|
437
|
+
function readTypestylesManifest(root, manifestFile = "app/typestyles.manifest.json") {
|
|
438
|
+
const manifestPath = resolve5(root, manifestFile);
|
|
439
|
+
if (!existsSync5(manifestPath)) {
|
|
440
|
+
throw new Error(`[typestyles] Missing manifest at ${manifestPath}.`);
|
|
441
|
+
}
|
|
442
|
+
let parsed;
|
|
443
|
+
try {
|
|
444
|
+
parsed = JSON.parse(readFileSync3(manifestPath, "utf8"));
|
|
445
|
+
} catch {
|
|
446
|
+
throw new Error(`[typestyles] Invalid JSON in ${manifestPath}.`);
|
|
447
|
+
}
|
|
448
|
+
if (typeof parsed !== "object" || parsed === null || !("version" in parsed) || !("css" in parsed)) {
|
|
449
|
+
throw new Error(`[typestyles] Manifest at ${manifestPath} is missing required fields.`);
|
|
450
|
+
}
|
|
451
|
+
return parsed;
|
|
452
|
+
}
|
|
453
|
+
function readCssFile(root, cssRelPath) {
|
|
454
|
+
const cssPath = resolve5(root, cssRelPath);
|
|
455
|
+
if (!existsSync5(cssPath)) {
|
|
456
|
+
throw new Error(`[typestyles] Missing CSS file at ${cssPath}.`);
|
|
457
|
+
}
|
|
458
|
+
return readFileSync3(cssPath, "utf8");
|
|
459
|
+
}
|
|
460
|
+
function getRouteCss(routePath, options) {
|
|
461
|
+
const manifest = readTypestylesManifest(options.root, options.manifestFile);
|
|
462
|
+
const normalized = normalizeRoutePath(routePath);
|
|
463
|
+
if (isManifestV2(manifest)) {
|
|
464
|
+
const routeEntry = manifest.routes[normalized];
|
|
465
|
+
if (routeEntry) {
|
|
466
|
+
return readCssFile(options.root, routeEntry.css);
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
return readCssFile(options.root, manifest.css);
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
// src/index.ts
|
|
473
|
+
import { build as esbuildBuild2 } from "esbuild";
|
|
474
|
+
import { existsSync as existsSync6, unlinkSync, writeFileSync } from "fs";
|
|
475
|
+
import { resolve as resolve6 } from "path";
|
|
5
476
|
import { spawnSync } from "child_process";
|
|
6
477
|
function toImportPath(modulePath) {
|
|
7
478
|
const normalized = modulePath.replace(/\\/g, "/");
|
|
@@ -16,7 +487,7 @@ async function runTypestylesBuild({
|
|
|
16
487
|
}) {
|
|
17
488
|
if (!modules.length) return "";
|
|
18
489
|
const importLines = modules.map((mod) => `import ${JSON.stringify(toImportPath(mod))};`).join("\n");
|
|
19
|
-
const bundleResult = await
|
|
490
|
+
const bundleResult = await esbuildBuild2({
|
|
20
491
|
write: false,
|
|
21
492
|
bundle: true,
|
|
22
493
|
platform: "node",
|
|
@@ -34,7 +505,7 @@ async function runTypestylesBuild({
|
|
|
34
505
|
if (!bundledCode) {
|
|
35
506
|
throw new Error("[typestyles] Failed to produce extraction bundle.");
|
|
36
507
|
}
|
|
37
|
-
const scriptPath =
|
|
508
|
+
const scriptPath = resolve6(root, `.typestyles-extract-${process.pid}.cjs`);
|
|
38
509
|
const script = `${bundledCode}
|
|
39
510
|
process.stdout.write(require('typestyles').getRegisteredCss());
|
|
40
511
|
`;
|
|
@@ -56,7 +527,7 @@ ${result.stderr || result.stdout || "unknown error"}`
|
|
|
56
527
|
}
|
|
57
528
|
return result.stdout ?? "";
|
|
58
529
|
} finally {
|
|
59
|
-
if (
|
|
530
|
+
if (existsSync6(scriptPath)) {
|
|
60
531
|
try {
|
|
61
532
|
unlinkSync(scriptPath);
|
|
62
533
|
} catch {
|
|
@@ -65,5 +536,25 @@ ${result.stderr || result.stdout || "unknown error"}`
|
|
|
65
536
|
}
|
|
66
537
|
}
|
|
67
538
|
export {
|
|
68
|
-
|
|
539
|
+
DEFAULT_EXTRACT_MODULE_CANDIDATES,
|
|
540
|
+
TYPESTYLES_IMPORT_RE,
|
|
541
|
+
VerifyTypestylesBuildError,
|
|
542
|
+
buildManifestV2,
|
|
543
|
+
collectAndWriteRouteCss,
|
|
544
|
+
createModuleLoader,
|
|
545
|
+
discoverDefaultExtractModules,
|
|
546
|
+
discoverNextAppRoutes,
|
|
547
|
+
extractNamespaces,
|
|
548
|
+
getRouteCss,
|
|
549
|
+
isManifestV2,
|
|
550
|
+
normalizeRoutePath,
|
|
551
|
+
pageRelPathToRoutePath,
|
|
552
|
+
readCssFile,
|
|
553
|
+
readTypestylesManifest,
|
|
554
|
+
reportDuplicateNamespaces,
|
|
555
|
+
resolveExtractMode,
|
|
556
|
+
resolveExtractModules,
|
|
557
|
+
runTypestylesBuild,
|
|
558
|
+
traceTypestylesModules,
|
|
559
|
+
verifyTypestylesBuild
|
|
69
560
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@typestyles/build-runner",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Shared module execution utilities for typestyles build integrations",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -28,16 +28,18 @@
|
|
|
28
28
|
"@types/node": "^25.2.1",
|
|
29
29
|
"eslint": "^9.39.2",
|
|
30
30
|
"tsup": "^8.0.0",
|
|
31
|
-
"typescript": "^5.4.0"
|
|
31
|
+
"typescript": "^5.4.0",
|
|
32
|
+
"vitest": "^3.0.0"
|
|
32
33
|
},
|
|
33
34
|
"license": "Apache-2.0",
|
|
34
35
|
"repository": {
|
|
35
36
|
"type": "git",
|
|
36
|
-
"url": "https://github.com/
|
|
37
|
+
"url": "https://github.com/type-styles/typestyles",
|
|
37
38
|
"directory": "packages/build-runner"
|
|
38
39
|
},
|
|
39
40
|
"scripts": {
|
|
40
41
|
"build": "tsup src/index.ts --format esm,cjs --dts --clean",
|
|
42
|
+
"test": "vitest run",
|
|
41
43
|
"lint": "eslint .",
|
|
42
44
|
"typecheck": "tsc --noEmit"
|
|
43
45
|
}
|