next-yak 7.0.0 → 8.0.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.
Files changed (42) hide show
  1. package/README.md +8 -8
  2. package/dist/context/baseContext.cjs.map +1 -1
  3. package/dist/context/baseContext.d.cts +4 -1
  4. package/dist/context/baseContext.d.ts +4 -1
  5. package/dist/context/baseContext.js.map +1 -1
  6. package/dist/context/index.cjs +4 -1
  7. package/dist/context/index.cjs.map +1 -1
  8. package/dist/context/index.d.cts +2 -2
  9. package/dist/context/index.d.ts +2 -2
  10. package/dist/context/index.js +5 -2
  11. package/dist/context/index.js.map +1 -1
  12. package/dist/context/index.server.cjs +6 -2
  13. package/dist/context/index.server.cjs.map +1 -1
  14. package/dist/context/index.server.js +10 -4
  15. package/dist/context/index.server.js.map +1 -1
  16. package/dist/index.d.cts +2 -2
  17. package/dist/index.d.ts +2 -2
  18. package/dist/loaders/turbo-loader.d.ts +15 -0
  19. package/dist/loaders/turbo-loader.js +896 -0
  20. package/dist/loaders/turbo-loader.js.map +1 -0
  21. package/dist/loaders/{css-loader.js → webpack-loader.js} +50 -62
  22. package/dist/loaders/webpack-loader.js.map +1 -0
  23. package/dist/withYak/index.cjs +80 -15
  24. package/dist/withYak/index.cjs.map +1 -1
  25. package/dist/withYak/index.d.cts +6 -4
  26. package/dist/withYak/index.d.ts +6 -4
  27. package/dist/withYak/index.js +80 -15
  28. package/dist/withYak/index.js.map +1 -1
  29. package/loaders/lib/debugLogger.ts +33 -0
  30. package/loaders/lib/extractCss.ts +39 -0
  31. package/loaders/lib/resolveCrossFileSelectors.ts +3 -1
  32. package/loaders/turbo-loader.ts +187 -0
  33. package/loaders/webpack-loader.ts +43 -0
  34. package/package.json +19 -17
  35. package/runtime/context/README.md +6 -2
  36. package/runtime/context/baseContext.tsx +3 -1
  37. package/runtime/context/index.server.tsx +12 -4
  38. package/runtime/context/index.tsx +8 -5
  39. package/withYak/index.ts +145 -24
  40. package/dist/loaders/css-loader.js.map +0 -1
  41. package/loaders/css-loader.ts +0 -110
  42. /package/dist/loaders/{css-loader.d.ts → webpack-loader.d.ts} +0 -0
@@ -0,0 +1,896 @@
1
+ // loaders/turbo-loader.ts
2
+ import { transform as swcTransform } from "@swc/core";
3
+ import { dirname } from "path";
4
+ import { createContext, runInContext } from "vm";
5
+
6
+ // cross-file-resolver/parseModule.ts
7
+ async function parseModule(context, modulePath) {
8
+ try {
9
+ const isYak = modulePath.endsWith(".yak.ts") || modulePath.endsWith(".yak.tsx") || modulePath.endsWith(".yak.js") || modulePath.endsWith(".yak.jsx");
10
+ if (isYak && context.evaluateYakModule) {
11
+ const yakModule = await context.evaluateYakModule(modulePath);
12
+ const yakExports = objectToModuleExport(yakModule);
13
+ return {
14
+ type: "yak",
15
+ exports: { importYak: false, named: yakExports, all: [] },
16
+ path: modulePath
17
+ };
18
+ }
19
+ if (context.cache?.parse === void 0) {
20
+ return uncachedParseModule(context, modulePath);
21
+ }
22
+ const cached = context.cache.parse.get(modulePath);
23
+ if (cached === void 0) {
24
+ const parsedModule = await uncachedParseModule(context, modulePath);
25
+ context.cache.parse.set(modulePath, parsedModule);
26
+ if (context.cache.parse.addDependency) {
27
+ context.cache.parse.addDependency(modulePath, modulePath);
28
+ }
29
+ return parsedModule;
30
+ }
31
+ return cached;
32
+ } catch (error) {
33
+ throw new Error(
34
+ `Error parsing file ${modulePath}: ${error.message}`
35
+ );
36
+ }
37
+ }
38
+ async function uncachedParseModule(context, modulePath) {
39
+ const exports = await context.extractExports(modulePath);
40
+ if (!exports.importYak) {
41
+ return {
42
+ type: "regular",
43
+ path: modulePath,
44
+ exports
45
+ };
46
+ }
47
+ const transformed = await context.getTransformed(modulePath);
48
+ const mixins = parseMixins(transformed.code);
49
+ const styledComponents = parseStyledComponents(
50
+ transformed.code,
51
+ context.transpilationMode
52
+ );
53
+ return {
54
+ type: "regular",
55
+ path: modulePath,
56
+ js: transformed,
57
+ exports,
58
+ styledComponents,
59
+ mixins
60
+ };
61
+ }
62
+ function parseMixins(sourceContents) {
63
+ const mixinParts = sourceContents.split("/*YAK EXPORTED MIXIN:");
64
+ let mixins = {};
65
+ for (let i = 1; i < mixinParts.length; i++) {
66
+ const [comment] = mixinParts[i].split("*/", 1);
67
+ const position = comment.indexOf("\n");
68
+ const name = comment.slice(0, position);
69
+ const value = comment.slice(position + 1);
70
+ mixins[name] = {
71
+ type: "mixin",
72
+ value,
73
+ nameParts: name.split(":").map((part) => decodeURIComponent(part))
74
+ };
75
+ }
76
+ return mixins;
77
+ }
78
+ function parseStyledComponents(sourceContents, transpilationMode) {
79
+ const styledParts = sourceContents.split("/*YAK EXPORTED STYLED:");
80
+ let styledComponents = {};
81
+ for (let i = 1; i < styledParts.length; i++) {
82
+ const [comment] = styledParts[i].split("*/", 1);
83
+ const [componentName, className] = comment.split(":");
84
+ styledComponents[componentName] = {
85
+ type: "styled-component",
86
+ nameParts: componentName.split("."),
87
+ value: transpilationMode === "Css" ? `.${className}` : `:global(.${className})`
88
+ };
89
+ }
90
+ return styledComponents;
91
+ }
92
+ function objectToModuleExport(object) {
93
+ return Object.fromEntries(
94
+ Object.entries(object).map(([key, value]) => {
95
+ if (typeof value === "string" || typeof value === "number") {
96
+ return [key, { type: "constant", value }];
97
+ } else if (value && (typeof value === "object" || Array.isArray(value))) {
98
+ return [
99
+ key,
100
+ { type: "record", value: objectToModuleExport(value) }
101
+ ];
102
+ } else {
103
+ return [key, { type: "unsupported", hint: String(value) }];
104
+ }
105
+ })
106
+ );
107
+ }
108
+
109
+ // cross-file-resolver/Errors.ts
110
+ var CauseError = class _CauseError extends Error {
111
+ constructor(message, options) {
112
+ super(
113
+ `${message}${options?.cause ? `
114
+ Caused by: ${typeof options.cause === "object" && options.cause !== null && "message" in options.cause ? options.cause.message : String(options.cause)}` : ""}`
115
+ );
116
+ if (options?.cause instanceof _CauseError && options.cause.circular) {
117
+ this.circular = true;
118
+ }
119
+ }
120
+ };
121
+ var ResolveError = class extends CauseError {
122
+ };
123
+ var CircularDependencyError = class extends CauseError {
124
+ constructor(message, options) {
125
+ super(message, options);
126
+ this.circular = true;
127
+ }
128
+ };
129
+
130
+ // cross-file-resolver/resolveCrossFileConstant.ts
131
+ var yakCssImportRegex = (
132
+ // Make mixin and selector non optional once we dropped support for the babel plugin
133
+ /--yak-css-import\:\s*url\("([^"]+)",?(|mixin|selector)\)(;?)/g
134
+ );
135
+ async function resolveCrossFileConstant(context, filePath, css) {
136
+ const resolveCrossFileConstant2 = context.cache?.resolveCrossFileConstant;
137
+ if (resolveCrossFileConstant2 === void 0) {
138
+ return uncachedResolveCrossFileConstant(context, filePath, css);
139
+ }
140
+ const cacheKey = await sha1(filePath + ":" + css);
141
+ const cached = resolveCrossFileConstant2.get(cacheKey);
142
+ if (cached === void 0) {
143
+ const resolvedCrossFilConstantPromise = uncachedResolveCrossFileConstant(
144
+ context,
145
+ filePath,
146
+ css
147
+ );
148
+ resolveCrossFileConstant2.set(cacheKey, resolvedCrossFilConstantPromise);
149
+ if (resolveCrossFileConstant2.addDependency) {
150
+ resolveCrossFileConstant2.addDependency(cacheKey, filePath);
151
+ resolvedCrossFilConstantPromise.then((value) => {
152
+ for (const dep of value.dependencies) {
153
+ resolveCrossFileConstant2.addDependency(cacheKey, dep);
154
+ }
155
+ });
156
+ }
157
+ return resolvedCrossFilConstantPromise;
158
+ }
159
+ return cached;
160
+ }
161
+ async function uncachedResolveCrossFileConstant(context, filePath, css) {
162
+ const yakImports = await parseYakCssImport(context, filePath, css);
163
+ if (yakImports.length === 0) {
164
+ return { resolved: css, dependencies: [] };
165
+ }
166
+ try {
167
+ const dependencies = /* @__PURE__ */ new Set();
168
+ const resolvedValues = await Promise.all(
169
+ yakImports.map(async ({ moduleSpecifier, specifier }) => {
170
+ const { resolved: resolvedModule } = await resolveModule(
171
+ context,
172
+ moduleSpecifier
173
+ );
174
+ const resolvedValue = await resolveModuleSpecifierRecursively(
175
+ context,
176
+ resolvedModule,
177
+ specifier
178
+ );
179
+ for (const dependency of resolvedValue.from) {
180
+ dependencies.add(dependency);
181
+ }
182
+ return resolvedValue;
183
+ })
184
+ );
185
+ let result = css;
186
+ for (let i = yakImports.length - 1; i >= 0; i--) {
187
+ const { position, size, importKind, specifier, semicolon } = yakImports[i];
188
+ const resolved = resolvedValues[i];
189
+ let replacement;
190
+ if (resolved.type === "unresolved-tag") {
191
+ replacement = importKind === "mixin" ? "" : "undefined";
192
+ } else {
193
+ if (importKind === "selector") {
194
+ if (resolved.type !== "styled-component" && resolved.type !== "constant") {
195
+ throw new Error(
196
+ `Found "${resolved.type}" but expected a selector - did you forget a semicolon after "${specifier.join(
197
+ "."
198
+ )}"?`
199
+ );
200
+ }
201
+ }
202
+ replacement = resolved.type === "styled-component" ? resolved.value : resolved.value + // resolved.value can be of two different types:
203
+ // - mixin:
204
+ // ${mixinName};
205
+ // - constant:
206
+ // color: ${value};
207
+ // For mixins the semicolon is already included in the value
208
+ // but for constants it has to be added manually
209
+ (["}", ";"].includes(String(resolved.value).trimEnd().slice(-1)) ? "" : semicolon);
210
+ }
211
+ result = result.slice(0, position) + String(replacement) + result.slice(position + size);
212
+ }
213
+ return { resolved: result, dependencies: Array.from(dependencies) };
214
+ } catch (error) {
215
+ throw new CauseError(
216
+ `Error while resolving cross-file selectors in file "${filePath}"`,
217
+ { cause: error }
218
+ );
219
+ }
220
+ }
221
+ async function parseYakCssImport(context, filePath, css) {
222
+ const yakImports = [];
223
+ for (const match of css.matchAll(yakCssImportRegex)) {
224
+ const [fullMatch, encodedArguments, importKind, semicolon] = match;
225
+ const [moduleSpecifier, ...specifier] = encodedArguments.split(":").map((entry) => decodeURIComponent(entry));
226
+ yakImports.push({
227
+ encodedArguments,
228
+ moduleSpecifier: await context.resolve(moduleSpecifier, filePath),
229
+ specifier,
230
+ importKind,
231
+ semicolon,
232
+ position: match.index,
233
+ size: fullMatch.length
234
+ });
235
+ }
236
+ return yakImports;
237
+ }
238
+ async function resolveModule(context, filePath) {
239
+ if (context.cache?.resolve === void 0) {
240
+ return uncachedResolveModule(context, filePath);
241
+ }
242
+ const cached = context.cache.resolve.get(filePath);
243
+ if (cached === void 0) {
244
+ const resolvedPromise = uncachedResolveModule(context, filePath);
245
+ context.cache.resolve.set(filePath, resolvedPromise);
246
+ if (context.cache.resolve.addDependency) {
247
+ context.cache.resolve.addDependency(filePath, filePath);
248
+ resolvedPromise.then((value) => {
249
+ for (const dep of value.dependencies) {
250
+ context.cache.resolve.addDependency(filePath, dep);
251
+ }
252
+ });
253
+ }
254
+ return resolvedPromise;
255
+ }
256
+ return cached;
257
+ }
258
+ async function uncachedResolveModule(context, filePath) {
259
+ const parsedModule = await context.parse(filePath);
260
+ const exports = parsedModule.exports;
261
+ if (parsedModule.type !== "regular") {
262
+ return {
263
+ resolved: {
264
+ path: parsedModule.path,
265
+ exports
266
+ },
267
+ dependencies: []
268
+ };
269
+ }
270
+ const dependencies = /* @__PURE__ */ new Set();
271
+ if (parsedModule.styledComponents) {
272
+ Object.values(parsedModule.styledComponents).map((styledComponent) => {
273
+ if (styledComponent.nameParts.length === 1) {
274
+ exports.named[styledComponent.nameParts[0]] = {
275
+ type: "styled-component",
276
+ className: styledComponent.value
277
+ };
278
+ } else {
279
+ let exportEntry = exports.named[styledComponent.nameParts[0]];
280
+ if (!exportEntry) {
281
+ exportEntry = { type: "record", value: {} };
282
+ exports.named[styledComponent.nameParts[0]] = exportEntry;
283
+ } else if (exportEntry.type !== "record") {
284
+ throw new CauseError(`Error parsing file "${parsedModule.path}"`, {
285
+ cause: `"${styledComponent.nameParts[0]}" is not a record`
286
+ });
287
+ }
288
+ let current = exportEntry.value;
289
+ for (let i = 1; i < styledComponent.nameParts.length - 1; i++) {
290
+ let next = current[styledComponent.nameParts[i]];
291
+ if (!next) {
292
+ next = { type: "record", value: {} };
293
+ current[styledComponent.nameParts[i]] = next;
294
+ } else if (next.type !== "record") {
295
+ throw new CauseError(`Error parsing file "${parsedModule.path}"`, {
296
+ cause: `"${styledComponent.nameParts.slice(0, i + 1).join(".")}" is not a record`
297
+ });
298
+ }
299
+ current = next.value;
300
+ }
301
+ current[styledComponent.nameParts[styledComponent.nameParts.length - 1]] = {
302
+ type: "styled-component",
303
+ className: styledComponent.value
304
+ };
305
+ }
306
+ });
307
+ }
308
+ if (parsedModule.mixins) {
309
+ await Promise.all(
310
+ Object.values(parsedModule.mixins).map(async (mixin) => {
311
+ const { resolved, dependencies: deps } = await resolveCrossFileConstant(
312
+ context,
313
+ parsedModule.path,
314
+ mixin.value
315
+ );
316
+ for (const dep of deps) {
317
+ dependencies.add(dep);
318
+ }
319
+ if (mixin.nameParts.length === 1) {
320
+ exports.named[mixin.nameParts[0]] = {
321
+ type: "mixin",
322
+ value: resolved
323
+ };
324
+ } else {
325
+ let exportEntry = exports.named[mixin.nameParts[0]];
326
+ if (!exportEntry) {
327
+ exportEntry = { type: "record", value: {} };
328
+ exports.named[mixin.nameParts[0]] = exportEntry;
329
+ } else if (exportEntry.type !== "record") {
330
+ throw new CauseError(`Error parsing file "${parsedModule.path}"`, {
331
+ cause: `"${mixin.nameParts[0]}" is not a record`
332
+ });
333
+ }
334
+ let current = exportEntry.value;
335
+ for (let i = 1; i < mixin.nameParts.length - 1; i++) {
336
+ let next = current[mixin.nameParts[i]];
337
+ if (!next) {
338
+ next = { type: "record", value: {} };
339
+ current[mixin.nameParts[i]] = next;
340
+ } else if (next.type !== "record") {
341
+ throw new CauseError(
342
+ `Error parsing file "${parsedModule.path}"`,
343
+ {
344
+ cause: `"${mixin.nameParts.slice(0, i + 1).join(".")}" is not a record`
345
+ }
346
+ );
347
+ }
348
+ current = next.value;
349
+ }
350
+ current[mixin.nameParts[mixin.nameParts.length - 1]] = {
351
+ type: "mixin",
352
+ value: resolved
353
+ };
354
+ }
355
+ })
356
+ );
357
+ }
358
+ return {
359
+ resolved: {
360
+ path: parsedModule.path,
361
+ exports
362
+ },
363
+ dependencies: Array.from(dependencies)
364
+ };
365
+ }
366
+ async function resolveModuleSpecifierRecursively(context, resolvedModule, specifiers, seen = /* @__PURE__ */ new Set()) {
367
+ const exportName = specifiers[0];
368
+ const exportValue = resolvedModule.exports.named[exportName];
369
+ if (exportValue !== void 0) {
370
+ if (seen.has(resolvedModule.path + ":" + exportName)) {
371
+ throw new CircularDependencyError(
372
+ `Unable to resolve "${specifiers.join(".")}" in module "${resolvedModule.path}"`,
373
+ { cause: "Circular dependency detected" }
374
+ );
375
+ }
376
+ seen.add(resolvedModule.path + ":" + exportName);
377
+ return resolveModuleExport(
378
+ context,
379
+ resolvedModule.path,
380
+ exportValue,
381
+ specifiers,
382
+ seen
383
+ );
384
+ }
385
+ let i = 1;
386
+ for (const from of resolvedModule.exports.all) {
387
+ if (context.exportAllLimit && i++ > context.exportAllLimit) {
388
+ throw new ResolveError(
389
+ `Unable to resolve "${specifiers.join(".")}" in module "${resolvedModule.path}"`,
390
+ {
391
+ cause: `More than ${context.exportAllLimit} star exports are not supported for performance reasons`
392
+ }
393
+ );
394
+ }
395
+ try {
396
+ const resolved = await resolveModuleExport(
397
+ context,
398
+ resolvedModule.path,
399
+ {
400
+ type: "re-export",
401
+ from,
402
+ name: exportName
403
+ },
404
+ specifiers,
405
+ seen
406
+ );
407
+ if (seen.has(resolvedModule.path + ":*")) {
408
+ throw new CircularDependencyError(
409
+ `Unable to resolve "${specifiers.join(".")}" in module "${resolvedModule.path}"`,
410
+ { cause: "Circular dependency detected" }
411
+ );
412
+ }
413
+ seen.add(resolvedModule.path + ":*");
414
+ return resolved;
415
+ } catch (error) {
416
+ if (!(error instanceof ResolveError)) {
417
+ throw error;
418
+ }
419
+ if (error.circular) {
420
+ throw error;
421
+ }
422
+ }
423
+ }
424
+ throw new ResolveError(`Unable to resolve "${specifiers.join(".")}"`, {
425
+ cause: `no matching export found in module "${resolvedModule.path}"`
426
+ });
427
+ }
428
+ async function resolveModuleExport(context, filePath, moduleExport, specifiers, seen) {
429
+ try {
430
+ switch (moduleExport.type) {
431
+ case "re-export": {
432
+ const { resolved: reExportedModule } = await resolveModule(
433
+ context,
434
+ await context.resolve(moduleExport.from, filePath)
435
+ );
436
+ const resolved = await resolveModuleSpecifierRecursively(
437
+ context,
438
+ reExportedModule,
439
+ [moduleExport.name, ...specifiers.slice(1)],
440
+ seen
441
+ );
442
+ if (resolved) {
443
+ resolved.from.push(filePath);
444
+ }
445
+ return resolved;
446
+ }
447
+ case "namespace-re-export": {
448
+ const { resolved: reExportedModule } = await resolveModule(
449
+ context,
450
+ await context.resolve(moduleExport.from, filePath)
451
+ );
452
+ const resolved = await resolveModuleSpecifierRecursively(
453
+ context,
454
+ reExportedModule,
455
+ specifiers.slice(1),
456
+ seen
457
+ );
458
+ if (resolved) {
459
+ resolved.from.push(filePath);
460
+ }
461
+ return resolved;
462
+ }
463
+ case "styled-component": {
464
+ return {
465
+ type: "styled-component",
466
+ from: [filePath],
467
+ source: filePath,
468
+ name: specifiers[specifiers.length - 1],
469
+ value: moduleExport.className
470
+ };
471
+ }
472
+ // usually at this point `tag-template` exports where already resolved to
473
+ // styled-components if a matching styled-component comment was generated
474
+ // by yak-swc. So resolving a value to a `tag-template` at this stage
475
+ // would mean that the user tried to use the result of a call to a
476
+ // different tag-template than yak's styled in a template. This is usually
477
+ // invalid.
478
+ //
479
+ // But there is an issue with Nextjs. Next build in two passes, once for
480
+ // the server bundle, once for the client bundle. During the server-side
481
+ // build, each module with the `"use client"` directive is transformed to
482
+ // throw errors if the exported symbol are used. This transformation
483
+ // removes the comments generated by `yak-swc`, so instead of the expected
484
+ // `styled-component`, calls to `styled` resolve to a `tag-template`
485
+ // (because no classname was found in the now absent comments).
486
+ //
487
+ // To summarize, if a "use client" bundle exports a styled component that
488
+ // is used in a "standard" module, the resolve logic would throw with
489
+ // "unknown type tag-template".
490
+ //
491
+ // To avoid this error, the resolve logic must handle those `tag-template`
492
+ // with a special type `unresolved-tag`. Those will be interpolated to
493
+ // valid CSS with minimal effect (to avoid CSS syntax error in the case of
494
+ // Nextjs server build)
495
+ case "tag-template": {
496
+ return {
497
+ type: "unresolved-tag",
498
+ from: [filePath],
499
+ source: filePath,
500
+ name: specifiers[specifiers.length - 1]
501
+ };
502
+ }
503
+ case "constant": {
504
+ return {
505
+ type: "constant",
506
+ from: [filePath],
507
+ source: filePath,
508
+ value: moduleExport.value
509
+ };
510
+ }
511
+ case "record": {
512
+ const resolvedInRecord = resolveSpecifierInRecord(
513
+ moduleExport,
514
+ specifiers[0],
515
+ specifiers.slice(1)
516
+ );
517
+ return resolveModuleExport(
518
+ context,
519
+ filePath,
520
+ resolvedInRecord,
521
+ specifiers,
522
+ seen
523
+ );
524
+ }
525
+ case "mixin": {
526
+ return {
527
+ type: "mixin",
528
+ from: [filePath],
529
+ source: filePath,
530
+ value: moduleExport.value
531
+ };
532
+ }
533
+ }
534
+ } catch (error) {
535
+ throw new ResolveError(
536
+ `Unable to resolve "${specifiers.join(".")}" in module "${filePath}"`,
537
+ { cause: error }
538
+ );
539
+ }
540
+ throw new ResolveError(
541
+ `Unable to resolve "${specifiers.join(".")}" in module "${filePath}"`,
542
+ { cause: `unknown type "${moduleExport.type}"` }
543
+ );
544
+ }
545
+ function resolveSpecifierInRecord(record, name, specifiers) {
546
+ if (specifiers.length === 0) {
547
+ throw new ResolveError("did not expect an object");
548
+ }
549
+ let depth = 0;
550
+ let current = record;
551
+ while (current && current.type === "record" && depth < specifiers.length) {
552
+ current = current.value[specifiers[depth]];
553
+ depth += 1;
554
+ }
555
+ if (current === void 0 || depth !== specifiers.length) {
556
+ throw new ResolveError(
557
+ `Unable to resolve "${specifiers.join(".")}" in object/array "${name}"`,
558
+ { cause: "path not found" }
559
+ );
560
+ }
561
+ if (current.type === "constant" || current.type === "styled-component" || current.type === "mixin") {
562
+ return current;
563
+ }
564
+ if (current.type === "record" && "__yak" in current.value && current.value.__yak.type === "constant") {
565
+ return { type: "mixin", value: String(current.value.__yak.value) };
566
+ }
567
+ throw new ResolveError(
568
+ `Unable to resolve "${specifiers.join(".")}" in object/array "${name}"`,
569
+ { cause: "only string and numbers are supported" }
570
+ );
571
+ }
572
+ async function sha1(message) {
573
+ const resultBuffer = await globalThis.crypto.subtle.digest(
574
+ "SHA-1",
575
+ new TextEncoder().encode(message)
576
+ );
577
+ return Array.from(
578
+ new Uint8Array(resultBuffer),
579
+ (byte) => byte.toString(16).padStart(2, "0")
580
+ ).join("");
581
+ }
582
+
583
+ // loaders/lib/debugLogger.ts
584
+ import { relative } from "path";
585
+ function createDebugLogger(loaderContext, debugOptions) {
586
+ if (!debugOptions) {
587
+ return () => {
588
+ };
589
+ }
590
+ const currentPath = loaderContext._compiler ? relative(loaderContext._compiler.context, loaderContext.resourcePath) : loaderContext.resourcePath;
591
+ return (messageType, message) => {
592
+ const pathWithExtension = messageType !== "ts" ? currentPath + `.${messageType}` : currentPath;
593
+ if (debugOptions === true || new RegExp(debugOptions).test(pathWithExtension)) {
594
+ console.log("\u{1F42E} Yak", pathWithExtension, "\n\n", message);
595
+ }
596
+ };
597
+ }
598
+
599
+ // loaders/lib/extractCss.ts
600
+ function extractCss(code, transpilationMode) {
601
+ let codeString;
602
+ if (typeof code === "string") {
603
+ codeString = code;
604
+ } else if (code instanceof Buffer) {
605
+ codeString = code.toString("utf-8");
606
+ } else if (code instanceof ArrayBuffer) {
607
+ codeString = new TextDecoder("utf-8").decode(code);
608
+ } else {
609
+ throw new Error(
610
+ "Invalid input type: code must be string, Buffer, or ArrayBuffer"
611
+ );
612
+ }
613
+ const codeParts = codeString.split("/*YAK Extracted CSS:\n");
614
+ let result = "";
615
+ for (let i = 1; i < codeParts.length; i++) {
616
+ const codeUntilEnd = codeParts[i].split("*/")[0];
617
+ result += codeUntilEnd;
618
+ }
619
+ if (result && transpilationMode !== "Css") {
620
+ result = "/* cssmodules-pure-no-check */\n" + result;
621
+ }
622
+ return result;
623
+ }
624
+
625
+ // loaders/lib/resolveCrossFileSelectors.ts
626
+ import { parse } from "@babel/parser";
627
+ import traverse from "@babel/traverse";
628
+ async function parseExports(sourceContents) {
629
+ const moduleExports = {
630
+ importYak: true,
631
+ named: {},
632
+ all: []
633
+ };
634
+ const variableDeclarations = {};
635
+ let defaultIdentifier = null;
636
+ try {
637
+ const ast = parse(sourceContents, {
638
+ sourceType: "module",
639
+ plugins: ["jsx", "typescript"]
640
+ });
641
+ traverse.default(ast, {
642
+ // Track all variable declarations in the file
643
+ VariableDeclarator({ node }) {
644
+ if (node.id.type === "Identifier" && node.init) {
645
+ variableDeclarations[node.id.name] = node.init;
646
+ }
647
+ },
648
+ ExportNamedDeclaration({ node }) {
649
+ if (node.source) {
650
+ node.specifiers.forEach((specifier) => {
651
+ if (specifier.type === "ExportSpecifier" && specifier.exported.type === "Identifier" && specifier.local.type === "Identifier") {
652
+ moduleExports.named[specifier.exported.name] = {
653
+ type: "re-export",
654
+ from: node.source.value,
655
+ name: specifier.local.name
656
+ };
657
+ }
658
+ });
659
+ } else if (node.declaration?.type === "VariableDeclaration") {
660
+ node.declaration.declarations.forEach((declaration) => {
661
+ if (declaration.id.type === "Identifier" && declaration.init) {
662
+ const parsed = parseExportValueExpression(declaration.init);
663
+ if (parsed) {
664
+ moduleExports.named[declaration.id.name] = parsed;
665
+ }
666
+ }
667
+ });
668
+ }
669
+ },
670
+ ExportDeclaration({ node }) {
671
+ if ("specifiers" in node && node.source) {
672
+ const { specifiers, source } = node;
673
+ specifiers.forEach((specifier) => {
674
+ if (specifier.type === "ExportNamespaceSpecifier" && specifier.exported.type === "Identifier") {
675
+ moduleExports.named[specifier.exported.name] = {
676
+ type: "namespace-re-export",
677
+ from: source.value
678
+ };
679
+ }
680
+ });
681
+ }
682
+ },
683
+ ExportDefaultDeclaration({ node }) {
684
+ if (node.declaration.type === "Identifier") {
685
+ defaultIdentifier = node.declaration.name;
686
+ } else if (node.declaration.type === "FunctionDeclaration" || node.declaration.type === "ClassDeclaration") {
687
+ moduleExports.named["default"] = {
688
+ type: "unsupported",
689
+ hint: node.declaration.type
690
+ };
691
+ } else {
692
+ moduleExports.named["default"] = parseExportValueExpression(
693
+ node.declaration
694
+ );
695
+ }
696
+ },
697
+ ExportAllDeclaration({ node }) {
698
+ moduleExports.all.push(node.source.value);
699
+ }
700
+ });
701
+ if (defaultIdentifier && variableDeclarations[defaultIdentifier]) {
702
+ moduleExports.named["default"] = parseExportValueExpression(
703
+ variableDeclarations[defaultIdentifier]
704
+ );
705
+ }
706
+ return moduleExports;
707
+ } catch (error) {
708
+ throw new Error(`Error parsing exports: ${error.message}`);
709
+ }
710
+ }
711
+ function unpackTSAsExpression(node) {
712
+ if (node.type === "TSAsExpression") {
713
+ return unpackTSAsExpression(node.expression);
714
+ }
715
+ return node;
716
+ }
717
+ function parseExportValueExpression(node) {
718
+ const expression = unpackTSAsExpression(node);
719
+ if (expression.type === "CallExpression" || expression.type === "TaggedTemplateExpression") {
720
+ return { type: "tag-template" };
721
+ } else if (expression.type === "StringLiteral" || expression.type === "NumericLiteral") {
722
+ return { type: "constant", value: expression.value };
723
+ } else if (expression.type === "UnaryExpression" && expression.operator === "-" && expression.argument.type === "NumericLiteral") {
724
+ return { type: "constant", value: -expression.argument.value };
725
+ } else if (expression.type === "TemplateLiteral" && expression.quasis.length === 1) {
726
+ return { type: "constant", value: expression.quasis[0].value.raw };
727
+ } else if (expression.type === "ObjectExpression") {
728
+ return { type: "record", value: parseObjectExpression(expression) };
729
+ }
730
+ return { type: "unsupported", hint: expression.type };
731
+ }
732
+ function parseObjectExpression(node) {
733
+ let result = {};
734
+ for (const property of node.properties) {
735
+ if (property.type === "ObjectProperty" && property.key.type === "Identifier") {
736
+ const key = property.key.name;
737
+ const parsed = parseExportValueExpression(
738
+ property.value
739
+ );
740
+ if (parsed) {
741
+ result[key] = parsed;
742
+ }
743
+ }
744
+ }
745
+ return result;
746
+ }
747
+
748
+ // loaders/turbo-loader.ts
749
+ async function cssExtractLoader(code, sourceMap) {
750
+ const callback = this.async();
751
+ if (!code.includes("next-yak")) {
752
+ return callback(null, code, sourceMap);
753
+ }
754
+ const {
755
+ yakPluginOptions,
756
+ yakOptions: { experiments }
757
+ } = this.getOptions();
758
+ const debugLog = createDebugLogger(this, experiments?.debug);
759
+ const resolveTurbopack = this.getResolve({});
760
+ const transform = createTransform(yakPluginOptions);
761
+ const resolveFn = (specifier, importer) => {
762
+ return new Promise((resolve, reject) => {
763
+ resolveTurbopack(dirname(importer), specifier, (err, result2) => {
764
+ if (err) return reject(err);
765
+ if (!result2) return reject(new Error(`Could not resolve ${specifier}`));
766
+ resolve(result2);
767
+ });
768
+ });
769
+ };
770
+ const fsReadFile = (filePath) => new Promise(
771
+ (resolve, reject) => this.fs.readFile(filePath, "utf-8", (err, result2) => {
772
+ if (err) return reject(err);
773
+ if (!result2) return reject(new Error(`File not found: ${filePath}`));
774
+ resolve(result2);
775
+ })
776
+ );
777
+ const result = await transform(
778
+ code,
779
+ this.resourcePath,
780
+ this.rootContext,
781
+ sourceMap
782
+ );
783
+ debugLog("ts", result.code);
784
+ let css = extractCss(result.code, "Css");
785
+ debugLog("css", css);
786
+ const { resolved } = await resolveCrossFileConstant(
787
+ {
788
+ parse: (modulePath) => {
789
+ return parseModule(
790
+ {
791
+ transpilationMode: "Css",
792
+ extractExports: async (modulePath2) => {
793
+ const sourceContents = await fsReadFile(modulePath2);
794
+ return parseExports(sourceContents);
795
+ },
796
+ getTransformed: async (modulePath2) => {
797
+ const sourceContent = await fsReadFile(modulePath2);
798
+ return transform(sourceContent, modulePath2, this.rootContext);
799
+ },
800
+ evaluateYakModule: async (modulePath2) => {
801
+ const code2 = await fsReadFile(modulePath2);
802
+ const transformed = await swcTransform(code2, {
803
+ filename: modulePath2,
804
+ sourceFileName: modulePath2,
805
+ jsc: {
806
+ transform: {
807
+ react: { runtime: "automatic" }
808
+ },
809
+ experimental: {
810
+ plugins: [["yak-swc", yakPluginOptions]]
811
+ }
812
+ },
813
+ module: {
814
+ type: "commonjs"
815
+ }
816
+ });
817
+ const moduleObject = { exports: {} };
818
+ const context = createContext({
819
+ require: (path) => {
820
+ throw new Error(
821
+ `Yak files cannot have imports in turbopack.
822
+ Found require/import usage in: ${modulePath2} to import: ${path}.
823
+ Yak files should be self-contained and only export constants or styled components.
824
+ This will be resolved once Vercel adds "this.importModule" support for turbopack.`
825
+ );
826
+ },
827
+ __filename: modulePath2,
828
+ __dirname: dirname(modulePath2),
829
+ global: {},
830
+ console,
831
+ Buffer,
832
+ process,
833
+ setTimeout,
834
+ clearTimeout,
835
+ setInterval,
836
+ clearInterval,
837
+ setImmediate,
838
+ clearImmediate,
839
+ exports: moduleObject.exports,
840
+ module: moduleObject
841
+ });
842
+ runInContext(transformed.code, context);
843
+ return moduleObject.exports;
844
+ }
845
+ },
846
+ modulePath
847
+ );
848
+ },
849
+ resolve: resolveFn
850
+ },
851
+ this.resourcePath,
852
+ css
853
+ );
854
+ const dataUrl = result.code.split("\n").find((line) => line.includes("data:text/css;base64"));
855
+ const codeWithCrossFileResolved = result.code.replace(
856
+ dataUrl,
857
+ `import "data:text/css;base64,${Buffer.from(resolved).toString("base64")}"`
858
+ );
859
+ debugLog("css-resolved", resolved);
860
+ return callback(null, codeWithCrossFileResolved, result.map);
861
+ }
862
+ function createTransform(yakPluginOptions) {
863
+ return (data, modulePath, rootPath, sourceMap) => (
864
+ // https://github.com/vercel/next.js/blob/canary/packages/next/src/build/webpack/loaders/next-swc-loader.ts#L143
865
+ swcTransform(data, {
866
+ filename: modulePath,
867
+ inputSourceMap: sourceMap,
868
+ sourceMaps: true,
869
+ sourceFileName: modulePath,
870
+ sourceRoot: rootPath,
871
+ jsc: {
872
+ experimental: {
873
+ plugins: [["yak-swc", yakPluginOptions]]
874
+ },
875
+ transform: {
876
+ react: {
877
+ runtime: "preserve"
878
+ }
879
+ },
880
+ target: "es2022",
881
+ loose: false,
882
+ minify: {
883
+ compress: false,
884
+ mangle: false
885
+ },
886
+ preserveAllComments: true
887
+ },
888
+ minify: false,
889
+ isModule: true
890
+ })
891
+ );
892
+ }
893
+ export {
894
+ cssExtractLoader as default
895
+ };
896
+ //# sourceMappingURL=turbo-loader.js.map