@specific.dev/spectest 0.119.6 → 0.120.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/src/next.ts DELETED
@@ -1,85 +0,0 @@
1
- import { promises as fs } from "node:fs";
2
- import path from "node:path";
3
-
4
- /** Keep each traced script's map alongside it in standalone output. Turbopack
5
- * versions that do not apply outputFileTracingIncludes still emit these traces
6
- * before runAfterProductionCompile. No source files or generated code change. */
7
- async function includeTracedMaps(distDir: string): Promise<void> {
8
- const traces: string[] = [];
9
- const serverMaps: string[] = [];
10
- async function walk(dir: string): Promise<void> {
11
- for (const entry of await fs.readdir(dir, { withFileTypes: true })) {
12
- const file = path.join(dir, entry.name);
13
- if (entry.isDirectory() && entry.name !== "cache" && entry.name !== "standalone") await walk(file);
14
- else if (entry.isFile()) {
15
- if (entry.name.endsWith(".nft.json")) traces.push(file);
16
- if (entry.name.endsWith(".js.map") && file.startsWith(path.join(distDir, "server") + path.sep)) serverMaps.push(file);
17
- }
18
- }
19
- }
20
- await walk(distDir);
21
- for (const file of traces) {
22
- const dir = path.dirname(file);
23
- const trace = JSON.parse(await fs.readFile(file, "utf8"));
24
- if (!Array.isArray(trace.files)) throw new Error(`Invalid Next trace: ${file}`);
25
- const scripts = [path.basename(file).slice(0, -".nft.json".length), ...trace.files];
26
- // Edge bundles are copied through middleware-manifest, not NFT traces.
27
- // The server trace is always copied into standalone output; retain
28
- // their maps there without changing the middleware's executable files.
29
- const maps = file === path.join(distDir, "next-server.js.nft.json")
30
- ? serverMaps.map(map => path.relative(dir, map)) : [];
31
- for (const script of scripts) {
32
- if (typeof script !== "string" || !script.endsWith(".js")) continue;
33
- const map = script + ".map";
34
- try { if ((await fs.stat(path.resolve(dir, map))).isFile()) maps.push(map); }
35
- catch (e) { if ((e as NodeJS.ErrnoException).code !== "ENOENT") throw e; }
36
- }
37
- trace.files = [...new Set([...trace.files, ...maps])];
38
- await fs.writeFile(file, JSON.stringify(trace));
39
- }
40
- }
41
-
42
- /** Build-time companion for coverage.next(). Import this lightweight module in
43
- * next.config; it does not load the Spectest harness or require Next at runtime.
44
- * Apply only in the test build if production should not expose source maps. */
45
- export function withCoverage<T extends object>(config: T): T {
46
- const c = config as Record<string, any>;
47
- const distDir = c.distDir ?? ".next";
48
- const includes = c.outputFileTracingIncludes ?? {};
49
- return {
50
- ...config,
51
- productionBrowserSourceMaps: true,
52
- turbopack: c.turbopack ?? {},
53
- webpack(webpackConfig: any, context: any) {
54
- const configured = c.webpack ? c.webpack(webpackConfig, context) : webpackConfig;
55
- configured.plugins ??= [];
56
- configured.plugins.push({
57
- apply(compiler: any) {
58
- compiler.hooks.compilation.tap("SpectestCoverage", (compilation: any) => {
59
- compiler.webpack.NormalModule.getCompilationHooks(compilation).loader.tap(
60
- "SpectestCoverage", (loader: any, module: any) => {
61
- // Keep dependencies identifiable as dependencies. Chaining a
62
- // package's published maps can replace node_modules provenance
63
- // with ambiguous paths such as ../../src/client/router.ts.
64
- if (/[\\/]node_modules[\\/]/.test(module.resource ?? "")) loader.sourceMap = false;
65
- },
66
- );
67
- });
68
- },
69
- });
70
- return configured;
71
- },
72
- experimental: { ...c.experimental, serverSourceMaps: true },
73
- compiler: {
74
- ...c.compiler,
75
- async runAfterProductionCompile(metadata: { projectDir: string; distDir: string }) {
76
- await c.compiler?.runAfterProductionCompile?.(metadata);
77
- await includeTracedMaps(metadata.distDir);
78
- },
79
- },
80
- outputFileTracingIncludes: {
81
- ...includes,
82
- "/**": [...(includes["/**"] ?? []), `./${distDir}/server/**/*.map`],
83
- },
84
- };
85
- }