@ttsc/unplugin 0.15.2 → 0.15.4

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.
@@ -0,0 +1,84 @@
1
+ import type { TtscUnpluginOptions } from "./core/options";
2
+ import { resolveOptions } from "./core/options";
3
+ import {
4
+ createTtscTransformCache,
5
+ isDeclarationFile,
6
+ stripQuery,
7
+ transformTtsc,
8
+ } from "./core/transform";
9
+
10
+ /**
11
+ * Subset of the webpack loader context Turbopack provides to loaders wired
12
+ * through `turbopack.rules`. Turbopack has no JS plugin API, but it runs
13
+ * webpack-compatible loaders: source string in, source string out, with
14
+ * `async()` for asynchronous completion and `getOptions()` for the rule's
15
+ * `options` object.
16
+ */
17
+ export interface TtscTurbopackLoaderContext {
18
+ /** Marks the loader asynchronous and returns the completion callback. */
19
+ async(): (error?: unknown, content?: string) => void;
20
+ /** Absolute path of the module being loaded. */
21
+ resourcePath: string;
22
+ /** The rule's `options` object, when one was configured. */
23
+ getOptions?(): TtscUnpluginOptions | undefined;
24
+ }
25
+
26
+ /** Matches any path segment that is a `node_modules` directory (cross-platform). */
27
+ const nodeModulesPattern = /(?:^|[/\\])node_modules(?:[/\\]|$)/;
28
+
29
+ /**
30
+ * Per-process transform cache. Turbopack runs loaders in a worker pool and
31
+ * never signals build boundaries to a loader, so the cache lives for the
32
+ * worker's lifetime; entries self-invalidate by re-hashing the project's input
33
+ * files on every request (see `transformTtsc`), which is the same freshness
34
+ * rule the bundler-plugin adapters rely on between watch rebuilds.
35
+ */
36
+ const transformCache = createTtscTransformCache();
37
+
38
+ /**
39
+ * Standalone webpack-loader entrypoint for Turbopack.
40
+ *
41
+ * Turbopack cannot load unplugin-based plugins (no JS plugin API), but its
42
+ * `turbopack.rules` accept webpack loaders, and a ttsc transform is exactly
43
+ * loader-shaped: pure TypeScript source in, transformed source out. Wire it per
44
+ * extension:
45
+ *
46
+ * ```js
47
+ * // next.config.mjs
48
+ * const nextConfig = {
49
+ * turbopack: {
50
+ * rules: {
51
+ * "*.ts": { loaders: ["@ttsc/unplugin/turbopack"] },
52
+ * "*.tsx": { loaders: ["@ttsc/unplugin/turbopack"] },
53
+ * },
54
+ * },
55
+ * };
56
+ * ```
57
+ *
58
+ * Pass {@link TtscUnpluginOptions} through the rule's `options` object. The
59
+ * loader returns the source unchanged for declaration files, `node_modules`
60
+ * paths, and transforms that produce no change — mirroring the unplugin
61
+ * adapters' `transformInclude` filter, since a broad rule glob routes
62
+ * everything matching the extension through the loader.
63
+ */
64
+ export default function turbopack(
65
+ this: TtscTurbopackLoaderContext,
66
+ source: string,
67
+ ): void {
68
+ const callback = this.async();
69
+ const file = stripQuery(this.resourcePath);
70
+ if (isDeclarationFile(file) || nodeModulesPattern.test(file)) {
71
+ callback(undefined, source);
72
+ return;
73
+ }
74
+ transformTtsc(
75
+ file,
76
+ source,
77
+ resolveOptions(this.getOptions?.() ?? {}),
78
+ undefined,
79
+ transformCache,
80
+ ).then(
81
+ (result) => callback(undefined, result?.code ?? source),
82
+ (error) => callback(error),
83
+ );
84
+ }