@tsrx/rspack-plugin-preact 0.0.86 → 0.0.87

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
@@ -33,6 +33,10 @@ when unset.
33
33
  `'preact/compat'`).
34
34
  - `runtimeImports`: helper import mode (`'compiler'` by default, or `'direct'` for
35
35
  standalone runtime imports).
36
+ - `platform`: optional override/fallback for the compile-time platform. Normally
37
+ the plugin reads `tsrx.platform` from the project tsconfig (including
38
+ `resolve.tsConfig` and `extends`). An override must match tsconfig. The plugin
39
+ defines all three exact flags and rejects conflicting Rspack definitions.
36
40
 
37
41
  When using `runtimeImports: 'direct'`, install the runtime as a direct production
38
42
  dependency of the package that owns the compiled modules:
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "description": "Rspack plugin for @tsrx/preact",
4
4
  "license": "MIT",
5
5
  "author": "Dominic Gannaway",
6
- "version": "0.0.86",
6
+ "version": "0.0.87",
7
7
  "type": "module",
8
8
  "publishConfig": {
9
9
  "access": "public"
@@ -20,7 +20,8 @@
20
20
  }
21
21
  },
22
22
  "dependencies": {
23
- "@tsrx/preact": "0.1.70"
23
+ "@tsrx/core": "0.1.71",
24
+ "@tsrx/preact": "0.1.71"
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/preact' */
3
+ /** @import { Platform, RuntimeImportMode } from '@tsrx/preact' */
4
4
 
5
5
  import { compile } from '@tsrx/preact';
6
6
 
@@ -9,7 +9,7 @@ import { compile } from '@tsrx/preact';
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<{ suspenseSource?: string, runtimeImports?: RuntimeImportMode }>}
12
+ * @this {LoaderContext<{ suspenseSource?: string, 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/preact' */
2
+ /** @import { Platform, RuntimeImportMode } from '@tsrx/preact' */
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,37 @@ 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
+ const compiler_with_definitions = /** @type {any} */ (compiler);
36
+ for (const plugin of compiler_with_definitions.options.plugins ?? []) {
37
+ const definitions = get_define_plugin_definitions(plugin);
38
+ if (definitions) {
39
+ mergePlatformDefinitions(definitions, platform, { integration: 'Rspack' });
40
+ }
41
+ }
42
+ const DefinePlugin = compiler_with_definitions.webpack?.DefinePlugin;
43
+ if (typeof DefinePlugin !== 'function') {
44
+ throw new Error('Rspack compiler does not expose DefinePlugin for TSRX platform flags.');
45
+ }
46
+ new DefinePlugin(createPlatformDefinitions(platform)).apply(compiler);
47
+ }
48
+
16
49
  /**
17
50
  * Rspack plugin for `.tsrx` files that compiles them via `@tsrx/preact` and
18
51
  * then delegates the final JSX transform to rspack's `builtin:swc-loader` so
@@ -24,13 +57,15 @@ const CSS_QUERY_PATTERN = /tsrx-css/;
24
57
  */
25
58
  export class TsrxPreactRspackPlugin {
26
59
  /**
27
- * @param {{ jsxImportSource?: string, suspenseSource?: string, runtimeImports?: RuntimeImportMode }} [options]
60
+ * @param {{ jsxImportSource?: string, suspenseSource?: string, runtimeImports?: RuntimeImportMode, platform?: Platform }} [options]
28
61
  */
29
62
  constructor(options = {}) {
63
+ this.explicit_platform = validatePlatform(options.platform);
30
64
  this.options = {
31
65
  jsxImportSource: options.jsxImportSource ?? 'preact',
32
66
  suspenseSource: options.suspenseSource,
33
67
  runtimeImports: options.runtimeImports ?? 'compiler',
68
+ platform: this.explicit_platform,
34
69
  };
35
70
  }
36
71
 
@@ -39,7 +74,17 @@ export class TsrxPreactRspackPlugin {
39
74
  * @returns {void}
40
75
  */
41
76
  apply(compiler) {
77
+ const compiler_options = /** @type {any} */ (compiler).options;
78
+ const resolve_tsconfig = compiler_options.resolve?.tsConfig;
79
+ this.options.platform = resolveBuildPlatform({
80
+ root: /** @type {any} */ (compiler).context ?? process.cwd(),
81
+ tsconfig:
82
+ typeof resolve_tsconfig === 'string' ? resolve_tsconfig : resolve_tsconfig?.configFile,
83
+ platform: this.explicit_platform,
84
+ integration: '@tsrx/rspack-plugin-preact',
85
+ });
42
86
  const { jsxImportSource } = this.options;
87
+ apply_platform_definitions(compiler, this.options.platform);
43
88
 
44
89
  const resolve = compiler.options.resolve;
45
90
  if (resolve.extensions && !resolve.extensions.includes('.tsrx')) {
@@ -88,6 +133,7 @@ export class TsrxPreactRspackPlugin {
88
133
  options: {
89
134
  suspenseSource: this.options.suspenseSource,
90
135
  runtimeImports: this.options.runtimeImports,
136
+ platform: this.options.platform,
91
137
  },
92
138
  },
93
139
  ],
@@ -102,6 +148,7 @@ export class TsrxPreactRspackPlugin {
102
148
  options: {
103
149
  suspenseSource: this.options.suspenseSource,
104
150
  runtimeImports: this.options.runtimeImports,
151
+ platform: this.options.platform,
105
152
  },
106
153
  },
107
154
  ],
package/src/js-loader.js CHANGED
@@ -1,6 +1,6 @@
1
1
  /** @import { LoaderContext } from '@rspack/core' */
2
2
 
3
- /** @import { RuntimeImportMode } from '@tsrx/preact' */
3
+ /** @import { Platform, RuntimeImportMode } from '@tsrx/preact' */
4
4
 
5
5
  import { compile } from '@tsrx/preact';
6
6
 
@@ -9,7 +9,7 @@ import { compile } from '@tsrx/preact';
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<{ suspenseSource?: string, runtimeImports?: RuntimeImportMode }>}
12
+ * @this {LoaderContext<{ suspenseSource?: string, runtimeImports?: RuntimeImportMode, platform?: Platform }>}
13
13
  * @param {string} source
14
14
  * @returns {void}
15
15
  */
package/types/index.d.ts CHANGED
@@ -1,17 +1,19 @@
1
1
  import type { Compiler, RspackPluginInstance } from '@rspack/core';
2
- import type { RuntimeImportMode } from '@tsrx/preact';
2
+ import type { Platform, RuntimeImportMode } from '@tsrx/preact';
3
3
 
4
4
  export interface TsrxPreactRspackPluginOptions {
5
5
  jsxImportSource?: string;
6
6
  suspenseSource?: string;
7
7
  /** Direct mode requires `@tsrx/preact-runtime` as a direct production dependency. */
8
8
  runtimeImports?: RuntimeImportMode;
9
+ /** Optional override; inferred from tsconfig by default and must agree when both exist. */
10
+ platform?: Platform;
9
11
  }
10
12
 
11
13
  export declare class TsrxPreactRspackPlugin implements RspackPluginInstance {
12
14
  constructor(options?: TsrxPreactRspackPluginOptions);
13
15
  options: Required<Pick<TsrxPreactRspackPluginOptions, 'jsxImportSource'>> &
14
- Pick<TsrxPreactRspackPluginOptions, 'suspenseSource'> &
16
+ Pick<TsrxPreactRspackPluginOptions, 'suspenseSource' | 'platform'> &
15
17
  Required<Pick<TsrxPreactRspackPluginOptions, 'runtimeImports'>>;
16
18
  apply(compiler: Compiler): void;
17
19
  }