@tsrx/rspack-plugin-react 0.0.96 → 0.0.98

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
@@ -31,6 +31,10 @@ when unset.
31
31
  - `jsxImportSource`: automatic JSX runtime import source (default: `'react'`).
32
32
  - `runtimeImports`: helper import mode (`'compiler'` by default, or `'direct'` for
33
33
  standalone runtime imports).
34
+ - `platform`: optional override/fallback for the compile-time platform. Normally
35
+ the plugin reads `tsrx.platform` from the project tsconfig (including
36
+ `resolve.tsConfig` and `extends`). An override must match tsconfig. The plugin
37
+ defines all three exact flags and rejects conflicting Rspack definitions.
34
38
 
35
39
  When using `runtimeImports: 'direct'`, install the runtime as a direct production
36
40
  dependency of the package that owns the compiled modules:
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "description": "Rspack plugin for @tsrx/react",
4
4
  "license": "MIT",
5
5
  "author": "Dominic Gannaway",
6
- "version": "0.0.96",
6
+ "version": "0.0.98",
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.2.0",
24
+ "@tsrx/react": "0.3.0"
24
25
  },
25
26
  "peerDependencies": {
26
27
  "@rspack/core": ">=1.6.0"
package/src/css-loader.js CHANGED
@@ -1,6 +1,6 @@
1
1
  /** @import { LoaderContext } from '@rspack/core' */
2
2
 
3
- /** @import { RuntimeImportMode } from '@tsrx/react' */
3
+ /** @import { Platform, RuntimeImportMode } from '@tsrx/react' */
4
4
 
5
5
  import { compile } from '@tsrx/react';
6
6
 
@@ -9,7 +9,7 @@ import { compile } from '@tsrx/react';
9
9
  * the scoped CSS emitted by its `<style>` block. Invoked when rspack resolves
10
10
  * the sibling `?tsrx-css&lang.css` import emitted by the JS loader.
11
11
  *
12
- * @this {LoaderContext<{ runtimeImports?: RuntimeImportMode }>}
12
+ * @this {LoaderContext<{ runtimeImports?: RuntimeImportMode, platform?: Platform }>}
13
13
  * @param {string} source
14
14
  * @returns {void}
15
15
  */
package/src/index.js CHANGED
@@ -1,8 +1,10 @@
1
1
  /** @import { Compiler, RspackPluginInstance } from '@rspack/core' */
2
- /** @import { RuntimeImportMode } from '@tsrx/react' */
2
+ /** @import { Platform, RuntimeImportMode } from '@tsrx/react' */
3
3
 
4
4
  import path from 'node:path';
5
5
  import { fileURLToPath } from 'node:url';
6
+ import { createPlatformDefinitions, mergePlatformDefinitions, validatePlatform } from '@tsrx/core';
7
+ import { resolveBuildPlatform } from '@tsrx/core/config';
6
8
 
7
9
  const __filename = fileURLToPath(import.meta.url);
8
10
  const __dirname = path.dirname(__filename);
@@ -13,6 +15,39 @@ const CSS_LOADER = path.join(__dirname, 'css-loader.js');
13
15
  const TSRX_EXTENSION_PATTERN = /\.tsrx$/;
14
16
  const CSS_QUERY_PATTERN = /tsrx-css/;
15
17
 
18
+ /** @param {any} plugin @returns {Record<string, unknown> | undefined} */
19
+ function get_define_plugin_definitions(plugin) {
20
+ if (plugin == null || typeof plugin !== 'object') return undefined;
21
+ if ((plugin.name ?? plugin.constructor?.name) !== 'DefinePlugin') return undefined;
22
+ if (plugin.definitions && typeof plugin.definitions === 'object') return plugin.definitions;
23
+ if (Array.isArray(plugin._args) && plugin._args[0] && typeof plugin._args[0] === 'object') {
24
+ return plugin._args[0];
25
+ }
26
+ if (plugin.options && typeof plugin.options === 'object' && !Array.isArray(plugin.options)) {
27
+ return plugin.options;
28
+ }
29
+ return undefined;
30
+ }
31
+
32
+ /** @param {Compiler} compiler @param {Platform | undefined} platform */
33
+ function apply_platform_definitions(compiler, platform) {
34
+ if (platform === undefined) return;
35
+
36
+ const compiler_with_definitions = /** @type {any} */ (compiler);
37
+ for (const plugin of compiler_with_definitions.options.plugins ?? []) {
38
+ const definitions = get_define_plugin_definitions(plugin);
39
+ if (definitions) {
40
+ mergePlatformDefinitions(definitions, platform, { integration: 'Rspack' });
41
+ }
42
+ }
43
+
44
+ const DefinePlugin = compiler_with_definitions.webpack?.DefinePlugin;
45
+ if (typeof DefinePlugin !== 'function') {
46
+ throw new Error('Rspack compiler does not expose DefinePlugin for TSRX platform flags.');
47
+ }
48
+ new DefinePlugin(createPlatformDefinitions(platform)).apply(compiler);
49
+ }
50
+
16
51
  /**
17
52
  * Rspack plugin for `.tsrx` files that compiles them via `@tsrx/react` and
18
53
  * then delegates the final JSX transform to rspack's `builtin:swc-loader` so
@@ -24,12 +59,14 @@ const CSS_QUERY_PATTERN = /tsrx-css/;
24
59
  */
25
60
  export class TsrxReactRspackPlugin {
26
61
  /**
27
- * @param {{ jsxImportSource?: string, runtimeImports?: RuntimeImportMode }} [options]
62
+ * @param {{ jsxImportSource?: string, runtimeImports?: RuntimeImportMode, platform?: Platform }} [options]
28
63
  */
29
64
  constructor(options = {}) {
65
+ this.explicit_platform = validatePlatform(options.platform);
30
66
  this.options = {
31
67
  jsxImportSource: options.jsxImportSource ?? 'react',
32
68
  runtimeImports: options.runtimeImports ?? 'compiler',
69
+ platform: this.explicit_platform,
33
70
  };
34
71
  }
35
72
 
@@ -38,7 +75,17 @@ export class TsrxReactRspackPlugin {
38
75
  * @returns {void}
39
76
  */
40
77
  apply(compiler) {
78
+ const compiler_options = /** @type {any} */ (compiler).options;
79
+ const resolve_tsconfig = compiler_options.resolve?.tsConfig;
80
+ this.options.platform = resolveBuildPlatform({
81
+ root: /** @type {any} */ (compiler).context ?? process.cwd(),
82
+ tsconfig:
83
+ typeof resolve_tsconfig === 'string' ? resolve_tsconfig : resolve_tsconfig?.configFile,
84
+ platform: this.explicit_platform,
85
+ integration: '@tsrx/rspack-plugin-react',
86
+ });
41
87
  const { jsxImportSource } = this.options;
88
+ apply_platform_definitions(compiler, this.options.platform);
42
89
 
43
90
  const resolve = compiler.options.resolve;
44
91
  if (resolve.extensions && !resolve.extensions.includes('.tsrx')) {
@@ -86,6 +133,7 @@ export class TsrxReactRspackPlugin {
86
133
  loader: JS_LOADER,
87
134
  options: {
88
135
  runtimeImports: this.options.runtimeImports,
136
+ platform: this.options.platform,
89
137
  },
90
138
  },
91
139
  ],
@@ -99,6 +147,7 @@ export class TsrxReactRspackPlugin {
99
147
  loader: CSS_LOADER,
100
148
  options: {
101
149
  runtimeImports: this.options.runtimeImports,
150
+ platform: this.options.platform,
102
151
  },
103
152
  },
104
153
  ],
package/src/js-loader.js CHANGED
@@ -1,6 +1,6 @@
1
1
  /** @import { LoaderContext } from '@rspack/core' */
2
2
 
3
- /** @import { RuntimeImportMode } from '@tsrx/react' */
3
+ /** @import { Platform, RuntimeImportMode } from '@tsrx/react' */
4
4
 
5
5
  import { compile } from '@tsrx/react';
6
6
 
@@ -9,7 +9,7 @@ import { compile } from '@tsrx/react';
9
9
  * appends an `import` of the sibling virtual CSS module so rspack can
10
10
  * include the styles in the asset graph.
11
11
  *
12
- * @this {LoaderContext<{ runtimeImports?: RuntimeImportMode }>}
12
+ * @this {LoaderContext<{ runtimeImports?: RuntimeImportMode, platform?: Platform }>}
13
13
  * @param {string} source
14
14
  * @returns {void}
15
15
  */
package/types/index.d.ts CHANGED
@@ -1,15 +1,18 @@
1
1
  import type { Compiler, RspackPluginInstance } from '@rspack/core';
2
- import type { RuntimeImportMode } from '@tsrx/react';
2
+ import type { Platform, RuntimeImportMode } from '@tsrx/react';
3
3
 
4
4
  export interface TsrxReactRspackPluginOptions {
5
5
  jsxImportSource?: string;
6
6
  /** Direct mode requires `@tsrx/react-runtime` as a direct production dependency. */
7
7
  runtimeImports?: RuntimeImportMode;
8
+ /** Optional override; inferred from tsconfig by default and must agree when both exist. */
9
+ platform?: Platform;
8
10
  }
9
11
 
10
12
  export declare class TsrxReactRspackPlugin implements RspackPluginInstance {
11
13
  constructor(options?: TsrxReactRspackPluginOptions);
12
- options: Required<TsrxReactRspackPluginOptions>;
14
+ options: Required<Pick<TsrxReactRspackPluginOptions, 'jsxImportSource' | 'runtimeImports'>> &
15
+ Pick<TsrxReactRspackPluginOptions, 'platform'>;
13
16
  apply(compiler: Compiler): void;
14
17
  }
15
18