@tsrx/turbopack-plugin-react 0.1.93 → 0.1.94

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/README.md CHANGED
@@ -14,9 +14,7 @@ pnpm add -D @tsrx/turbopack-plugin-react next
14
14
  ```ts
15
15
  import tsrxReactTurbopack from '@tsrx/turbopack-plugin-react';
16
16
 
17
- export default tsrxReactTurbopack({
18
- reactStrictMode: true,
19
- });
17
+ export default tsrxReactTurbopack({ reactStrictMode: true });
20
18
  ```
21
19
 
22
20
  The helper installs Turbopack rules that compile `.tsrx` modules with
@@ -33,6 +31,15 @@ Pass Next.js config as the first argument. The second argument is optional:
33
31
 
34
32
  - `runtimeImports`: helper import mode (`'compiler'` by default, or `'direct'` for
35
33
  standalone runtime imports).
34
+ - `platform`: optional override/fallback for the compile-time platform. Normally
35
+ the helper reads `tsrx.platform` from Next's `typescript.tsconfigPath` or the
36
+ nearest tsconfig under the Turbopack root. An override must match tsconfig.
37
+ - `tsconfig`: optional config-path override when the Next config does not select
38
+ the desired TypeScript project.
39
+
40
+ When a platform is selected, the helper installs replacement rules for all three
41
+ exact flags in both TSRX output and ordinary application JavaScript/TypeScript
42
+ modules.
36
43
 
37
44
  ```ts
38
45
  export default tsrxReactTurbopack({}, { runtimeImports: 'direct' });
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "description": "Turbopack integration for @tsrx/react",
4
4
  "license": "MIT",
5
5
  "author": "Dominic Gannaway",
6
- "version": "0.1.93",
6
+ "version": "0.1.94",
7
7
  "type": "module",
8
8
  "publishConfig": {
9
9
  "access": "public"
@@ -20,7 +20,8 @@
20
20
  }
21
21
  },
22
22
  "dependencies": {
23
- "@tsrx/react": "0.2.70"
23
+ "@tsrx/core": "0.1.71",
24
+ "@tsrx/react": "0.2.71"
24
25
  },
25
26
  "peerDependencies": {
26
27
  "next": ">=15.3.0"
package/src/css-loader.js CHANGED
@@ -1,11 +1,11 @@
1
- /** @import { RuntimeImportMode } from '@tsrx/react' */
1
+ /** @import { Platform, RuntimeImportMode } from '@tsrx/react' */
2
2
 
3
3
  import { compile } from '@tsrx/react';
4
4
 
5
5
  /**
6
6
  * @typedef {{
7
7
  * resourcePath: string,
8
- * getOptions?: () => { runtimeImports?: RuntimeImportMode },
8
+ * getOptions?: () => { runtimeImports?: RuntimeImportMode, platform?: Platform },
9
9
  * async: () => (err: unknown, output?: string | null, map?: unknown) => void,
10
10
  * }} LoaderContext
11
11
  */
package/src/index.js CHANGED
@@ -1,15 +1,28 @@
1
- /** @import { RuntimeImportMode } from '@tsrx/react' */
1
+ /** @import { Platform, RuntimeImportMode } from '@tsrx/react' */
2
2
 
3
3
  import path from 'node:path';
4
4
  import { fileURLToPath } from 'node:url';
5
+ import { validatePlatform } from '@tsrx/core';
6
+ import { resolveBuildPlatform } from '@tsrx/core/config';
5
7
 
6
8
  const __filename = fileURLToPath(import.meta.url);
7
9
  const __dirname = path.dirname(__filename);
8
10
 
9
11
  const TURBOPACK_JS_LOADER = path.join(__dirname, 'loader.js');
10
12
  const TURBOPACK_CSS_LOADER = path.join(__dirname, 'css-loader.js');
13
+ const TURBOPACK_PLATFORM_LOADER = path.join(__dirname, 'platform-loader.js');
11
14
  const DEFAULT_RESOLVE_EXTENSIONS = ['.tsrx', '.tsx', '.ts', '.jsx', '.js', '.mjs', '.json'];
12
15
  const CSS_QUERY = '?tsrx-css&lang.css';
16
+ const PLATFORM_SOURCE_GLOBS = [
17
+ '*.js',
18
+ '*.jsx',
19
+ '*.mjs',
20
+ '*.cjs',
21
+ '*.ts',
22
+ '*.tsx',
23
+ '*.mts',
24
+ '*.cts',
25
+ ];
13
26
 
14
27
  /**
15
28
  * @typedef {{
@@ -20,24 +33,37 @@ const CSS_QUERY = '?tsrx-css&lang.css';
20
33
  * resolveExtensions?: string[],
21
34
  * debugIds?: boolean,
22
35
  * },
36
+ * typescript?: { tsconfigPath?: string },
23
37
  * [key: string]: any,
24
38
  * }} NextTurbopackConfig
25
39
  */
26
40
 
27
41
  /**
28
- * @typedef {{ runtimeImports?: RuntimeImportMode }} TsrxReactTurbopackOptions
42
+ * @typedef {{ runtimeImports?: RuntimeImportMode, platform?: Platform, tsconfig?: string }} TsrxReactTurbopackOptions
29
43
  */
30
44
 
45
+ /** @param {TsrxReactTurbopackOptions} options @returns {TsrxReactTurbopackOptions} */
46
+ function normalize_options(options) {
47
+ return { ...options, platform: validatePlatform(options.platform) };
48
+ }
49
+
31
50
  /**
32
51
  * @param {TsrxReactTurbopackOptions} [options]
33
52
  * @returns {{ condition: { all: any[] }, loaders: Array<string | { loader: string, options: TsrxReactTurbopackOptions }>, as: string }}
34
53
  */
35
54
  export function create_tsrx_react_turbopack_rule(options = {}) {
55
+ options = normalize_options(options);
56
+ const loaders = [with_loader_options(TURBOPACK_JS_LOADER, options)];
57
+ if (options.platform !== undefined) {
58
+ // Webpack-compatible loaders execute right-to-left: compile TSRX first,
59
+ // then replace flags that intentionally remain in runtime expressions.
60
+ loaders.unshift({ loader: TURBOPACK_PLATFORM_LOADER, options: { platform: options.platform } });
61
+ }
36
62
  return {
37
63
  condition: {
38
64
  all: [{ not: 'foreign' }, { not: { query: CSS_QUERY } }],
39
65
  },
40
- loaders: [with_loader_options(TURBOPACK_JS_LOADER, options)],
66
+ loaders,
41
67
  as: '*.tsx',
42
68
  };
43
69
  }
@@ -47,6 +73,7 @@ export function create_tsrx_react_turbopack_rule(options = {}) {
47
73
  * @returns {{ condition: { all: any[] }, loaders: Array<string | { loader: string, options: TsrxReactTurbopackOptions }>, type: string }}
48
74
  */
49
75
  export function create_tsrx_react_turbopack_css_rule(options = {}) {
76
+ options = normalize_options(options);
50
77
  return {
51
78
  condition: {
52
79
  all: [{ not: 'foreign' }, { query: CSS_QUERY }],
@@ -65,7 +92,17 @@ export function create_tsrx_react_turbopack_css_rule(options = {}) {
65
92
  * @returns {string | { loader: string, options: TsrxReactTurbopackOptions }}
66
93
  */
67
94
  function with_loader_options(loader, options) {
68
- return options.runtimeImports === undefined ? loader : { loader, options };
95
+ return options.runtimeImports === undefined && options.platform === undefined
96
+ ? loader
97
+ : { loader, options };
98
+ }
99
+
100
+ /** @param {TsrxReactTurbopackOptions} options */
101
+ function create_platform_rule(options) {
102
+ return {
103
+ condition: { all: [{ not: 'foreign' }] },
104
+ loaders: [{ loader: TURBOPACK_PLATFORM_LOADER, options: { platform: options.platform } }],
105
+ };
69
106
  }
70
107
 
71
108
  /**
@@ -108,9 +145,27 @@ function merge_tsrx_rule(existing_rule, options) {
108
145
  * @returns {NextTurbopackConfig}
109
146
  */
110
147
  export function tsrxReactTurbopack(next_config = {}, options = {}) {
148
+ options = normalize_options(options);
111
149
  const turbopack = next_config.turbopack ?? {};
150
+ options.platform = resolveBuildPlatform({
151
+ root: turbopack.root ?? process.cwd(),
152
+ tsconfig: options.tsconfig ?? next_config.typescript?.tsconfigPath,
153
+ platform: options.platform,
154
+ integration: '@tsrx/turbopack-plugin-react',
155
+ });
112
156
  const rules = { ...(turbopack.rules ?? {}) };
113
157
  rules['*.tsrx'] = merge_tsrx_rule(rules['*.tsrx'], options);
158
+ if (options.platform !== undefined) {
159
+ for (const glob of PLATFORM_SOURCE_GLOBS) {
160
+ const platform_rule = create_platform_rule(options);
161
+ const existing_rule = rules[glob];
162
+ rules[glob] = existing_rule
163
+ ? Array.isArray(existing_rule)
164
+ ? [platform_rule, ...existing_rule]
165
+ : [platform_rule, existing_rule]
166
+ : platform_rule;
167
+ }
168
+ }
114
169
 
115
170
  return {
116
171
  ...next_config,
package/src/loader.js CHANGED
@@ -1,4 +1,4 @@
1
- /** @import { RuntimeImportMode } from '@tsrx/react' */
1
+ /** @import { Platform, RuntimeImportMode } from '@tsrx/react' */
2
2
 
3
3
  import { compile } from '@tsrx/react';
4
4
 
@@ -22,7 +22,7 @@ function append_css_import(code, resource_path) {
22
22
  /**
23
23
  * @typedef {{
24
24
  * resourcePath: string,
25
- * getOptions?: () => { runtimeImports?: RuntimeImportMode },
25
+ * getOptions?: () => { runtimeImports?: RuntimeImportMode, platform?: Platform },
26
26
  * async: () => (err: unknown, output?: string | null, map?: unknown) => void,
27
27
  * }} LoaderContext
28
28
  */
@@ -0,0 +1,37 @@
1
+ /** @import { Platform } from '@tsrx/react' */
2
+
3
+ import { replacePlatformFlags } from '@tsrx/core';
4
+
5
+ /**
6
+ * @typedef {{
7
+ * resourcePath: string,
8
+ * getOptions?: () => { platform?: Platform },
9
+ * async: () => (err: unknown, output?: string | null, map?: unknown) => void,
10
+ * }} LoaderContext
11
+ */
12
+
13
+ /**
14
+ * Supply Turbopack's missing `define` equivalent. TSRX's own loader has already
15
+ * pruned exact ordinary-if guards; this pass handles flags left in runtime
16
+ * expressions and ordinary JavaScript/TypeScript modules.
17
+ *
18
+ * @this {LoaderContext}
19
+ * @param {string} source
20
+ * @param {import('source-map').RawSourceMap | string | undefined} [input_map]
21
+ * @returns {void}
22
+ */
23
+ export default function tsrx_react_turbopack_platform_loader(source, input_map) {
24
+ const callback = this.async();
25
+
26
+ try {
27
+ const { code, map } = replacePlatformFlags(
28
+ source,
29
+ this.resourcePath,
30
+ this.getOptions?.().platform,
31
+ input_map,
32
+ );
33
+ callback(null, code, map);
34
+ } catch (/** @type {any} */ error) {
35
+ callback(error);
36
+ }
37
+ }
package/types/index.d.ts CHANGED
@@ -1,6 +1,9 @@
1
- import type { RuntimeImportMode } from '@tsrx/react';
1
+ import type { Platform, RuntimeImportMode } from '@tsrx/react';
2
2
 
3
3
  export interface NextTurbopackConfig {
4
+ typescript?: {
5
+ tsconfigPath?: string;
6
+ };
4
7
  turbopack?: {
5
8
  root?: string;
6
9
  rules?: Record<string, unknown>;
@@ -14,6 +17,10 @@ export interface NextTurbopackConfig {
14
17
  export interface TsrxReactTurbopackOptions {
15
18
  /** Direct mode requires `@tsrx/react-runtime` as a direct production dependency. */
16
19
  runtimeImports?: RuntimeImportMode;
20
+ /** Optional override; inferred from tsconfig by default and must agree when both exist. */
21
+ platform?: Platform;
22
+ /** Optional tsconfig path override, resolved from the Turbopack project root. */
23
+ tsconfig?: string;
17
24
  }
18
25
 
19
26
  export interface TsrxReactTurbopackLoader {