@tsrx/rspack-plugin-vue 0.0.99 → 0.1.1

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
@@ -5,10 +5,12 @@ Rspack plugin for compiling `@tsrx/vue` `.tsrx` files.
5
5
  ## Installation
6
6
 
7
7
  ```bash
8
- pnpm add @tsrx/vue vue vue-jsx-vapor
8
+ pnpm add @tsrx/vue "vue@^3.6.0-0" vue-jsx-vapor
9
9
  pnpm add -D @tsrx/rspack-plugin-vue
10
10
  ```
11
11
 
12
+ Vapor needs Vue 3.6 or later, so name the version.
13
+
12
14
  ## Usage
13
15
 
14
16
  ```ts
@@ -20,9 +22,10 @@ export default {
20
22
  ```
21
23
 
22
24
  The plugin compiles `.tsrx` modules with `@tsrx/vue`, runs the result through
23
- `vue-jsx-vapor`, strips the remaining TypeScript syntax with `builtin:swc-loader`,
24
- and emits sibling-scoped `<style>` blocks (each styles its siblings and everything
25
- below them) through Rspack's built-in CSS module type.
25
+ `vue-jsx-vapor`'s macros and Vapor transform, strips the remaining TypeScript
26
+ syntax with `builtin:swc-loader`, and emits sibling-scoped `<style>` blocks (each
27
+ styles its siblings and everything below them) through Rspack's built-in CSS
28
+ module type.
26
29
 
27
30
  It also pushes `.tsrx` into `resolve.extensions` and enables `experiments.css`
28
31
  when unset. Editor typechecking should set `jsxImportSource: 'vue-jsx-vapor'`.
@@ -30,7 +33,12 @@ when unset. Editor typechecking should set `jsxImportSource: 'vue-jsx-vapor'`.
30
33
  ## Options
31
34
 
32
35
  - `vapor`: options forwarded to `vue-jsx-vapor`. By default the plugin enables
33
- macros and uses `runtimeModuleName: 'vue-jsx-vapor'`.
36
+ macros and uses `runtimeModuleName: 'vue-jsx-vapor'`. The macros run as in the
37
+ Vite plugin, and their `include` and `exclude` options see a module as
38
+ `<file>.tsrx.tsx`. The plugin serves the helper modules they import as virtual
39
+ modules under the compiler's `context`. `defineStyle()` stylesheets must be CSS:
40
+ the plugin can't preprocess Sass, Less or Stylus before vue-jsx-vapor scopes
41
+ them.
34
42
  - `runtimeImports`: helper import mode (`'compiler'` by default, or `'direct'` for
35
43
  standalone runtime imports).
36
44
  - `platform`: optional override/fallback for the compile-time platform. Normally
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "description": "Rspack plugin for @tsrx/vue",
4
4
  "license": "MIT",
5
5
  "author": "Dominic Gannaway",
6
- "version": "0.0.99",
6
+ "version": "0.1.1",
7
7
  "type": "module",
8
8
  "publishConfig": {
9
9
  "access": "public"
@@ -21,19 +21,19 @@
21
21
  },
22
22
  "dependencies": {
23
23
  "source-map": "^0.7.6",
24
- "@tsrx/core": "0.5.5",
25
- "@tsrx/vue": "0.3.5"
24
+ "@tsrx/core": "0.6.1",
25
+ "@tsrx/vue": "0.4.1"
26
26
  },
27
27
  "peerDependencies": {
28
28
  "@rspack/core": ">=1.6.0",
29
- "vue": ">=3.5",
30
- "vue-jsx-vapor": ">=3.2.14"
29
+ "vue": ">=3.6.0-0",
30
+ "vue-jsx-vapor": ">=3.2.27"
31
31
  },
32
32
  "devDependencies": {
33
33
  "@rspack/core": "^2.1.5",
34
34
  "typescript": "^5.9.3",
35
- "vue": "3.6.0-beta.12",
36
- "vue-jsx-vapor": "^3.2.14"
35
+ "vue": "3.6.0-rc.14",
36
+ "vue-jsx-vapor": "^3.2.29"
37
37
  },
38
38
  "files": [
39
39
  "src",
package/src/index.js CHANGED
@@ -1,10 +1,17 @@
1
1
  /** @import { Compiler, RspackPluginInstance } from '@rspack/core' */
2
2
  /** @import { Platform, RuntimeImportMode } from '@tsrx/vue' */
3
+ /** @import { VueJsxVapor } from './vue-jsx-vapor.js' */
3
4
 
4
5
  import path from 'node:path';
5
6
  import { fileURLToPath } from 'node:url';
6
7
  import { createPlatformDefinitions, mergePlatformDefinitions, validatePlatform } from '@tsrx/core';
7
8
  import { resolveBuildPlatform } from '@tsrx/core/config';
9
+ import {
10
+ create_vue_jsx_vapor,
11
+ parse_vapor_helper_request,
12
+ VAPOR_HELPER_ID_PATTERN,
13
+ VAPOR_STYLESHEET_ID_PATTERN,
14
+ } from './vue-jsx-vapor.js';
8
15
 
9
16
  const __filename = fileURLToPath(import.meta.url);
10
17
  const __dirname = path.dirname(__filename);
@@ -17,6 +24,11 @@ const INTEROP_LOADER = path.join(__dirname, 'interop-loader.js');
17
24
  const TSRX_EXTENSION_PATTERN = /\.tsrx$/;
18
25
  const CSS_QUERY_PATTERN = /tsrx-css/;
19
26
  const SOURCE_EXTENSION_PATTERN = /\.[cm]?[jt]sx?$/;
27
+ const PLUGIN_NAME = 'TsrxVueRspackPlugin';
28
+ const VAPOR_HELPER_DIR = '.tsrx-vue-jsx-vapor';
29
+ // `defineStyle` sheets get extensions that no project rule for `.css` matches.
30
+ const VAPOR_STYLESHEET_PATTERN = /[/\\]\.tsrx-vue-jsx-vapor[/\\][^/\\]+\.vapor-css$/;
31
+ const VAPOR_CSS_MODULE_PATTERN = /[/\\]\.tsrx-vue-jsx-vapor[/\\][^/\\]+\.vapor-module-css$/;
20
32
 
21
33
  /** @param {any} plugin @returns {Record<string, unknown> | undefined} */
22
34
  function get_define_plugin_definitions(plugin) {
@@ -49,12 +61,56 @@ function apply_platform_definitions(compiler, platform) {
49
61
  new DefinePlugin(createPlatformDefinitions(platform)).apply(compiler);
50
62
  }
51
63
 
64
+ /**
65
+ * Resolves the helper modules the macros import into compiled `.tsrx` modules,
66
+ * such as `/vue-jsx-vapor/props`, to virtual files under the compiler's
67
+ * context, where they resolve `vue`, as in vue-jsx-vapor's own Rspack plugin.
68
+ * A `defineStyle` stylesheet gets an extension for the plugin's CSS rules.
69
+ *
70
+ * @param {Compiler} compiler
71
+ * @param {VueJsxVapor} vue_jsx_vapor
72
+ */
73
+ function serve_vapor_helpers(compiler, vue_jsx_vapor) {
74
+ const virtual_modules = new compiler.webpack.experiments.VirtualModulesPlugin();
75
+ virtual_modules.apply(compiler);
76
+ /** @type {Set<string>} */
77
+ const written = new Set();
78
+
79
+ compiler.hooks.compilation.tap(PLUGIN_NAME, (_compilation, { normalModuleFactory }) => {
80
+ normalModuleFactory.hooks.resolve.tapPromise(PLUGIN_NAME, async (data) => {
81
+ if (!VAPOR_HELPER_ID_PATTERN.test(data.request)) return;
82
+ if (!TSRX_EXTENSION_PATTERN.test(data.contextInfo.issuer)) return;
83
+ const { id, source } = parse_vapor_helper_request(data.request);
84
+ const [, css_module, lang] = VAPOR_STYLESHEET_ID_PATTERN.exec(id) ?? [];
85
+ if (lang !== undefined && lang !== 'css') {
86
+ throw new Error(
87
+ `@tsrx/rspack-plugin-vue can't compile a defineStyle.${lang} stylesheet (${id}). Use defineStyle() or defineStyle.css().`,
88
+ );
89
+ }
90
+ const code = await vue_jsx_vapor.load_helper(id, source);
91
+ if (code === undefined) return;
92
+ const extension = !lang ? '.js' : css_module ? '.vapor-module-css' : '.vapor-css';
93
+ const file = path.join(
94
+ compiler.context,
95
+ 'node_modules',
96
+ VAPOR_HELPER_DIR,
97
+ encodeURIComponent(id) + extension,
98
+ );
99
+ if (!written.has(file)) {
100
+ written.add(file);
101
+ virtual_modules.writeModule(file, code);
102
+ }
103
+ data.request = file;
104
+ });
105
+ });
106
+ }
107
+
52
108
  /**
53
109
  * Rspack plugin for `.tsrx` files that compiles them via `@tsrx/vue`, runs the
54
- * result through `vue-jsx-vapor`, and finally strips the remaining TypeScript
55
- * syntax with rspack's built-in SWC loader. Per-component `<style>` blocks are
56
- * re-imported via a sibling `?tsrx-css&lang.css` query and handled by rspack's
57
- * built-in CSS module type.
110
+ * result through `vue-jsx-vapor`'s macros and Vapor transform, and finally
111
+ * strips the remaining TypeScript syntax with rspack's built-in SWC loader.
112
+ * Per-component `<style>` blocks are re-imported via a sibling
113
+ * `?tsrx-css&lang.css` query and handled by rspack's built-in CSS module type.
58
114
  *
59
115
  * @implements {RspackPluginInstance}
60
116
  */
@@ -105,6 +161,9 @@ export class TsrxVueRspackPlugin {
105
161
  experiments.deferImport = true;
106
162
  }
107
163
 
164
+ const vue_jsx_vapor = create_vue_jsx_vapor(this.options.vapor);
165
+ serve_vapor_helpers(compiler, vue_jsx_vapor);
166
+
108
167
  compiler.options.module.rules.unshift(
109
168
  {
110
169
  test: SOURCE_EXTENSION_PATTERN,
@@ -135,6 +194,7 @@ export class TsrxVueRspackPlugin {
135
194
  loader: VAPOR_LOADER,
136
195
  options: {
137
196
  vapor: this.options.vapor,
197
+ vueJsxVapor: vue_jsx_vapor,
138
198
  },
139
199
  },
140
200
  {
@@ -160,6 +220,16 @@ export class TsrxVueRspackPlugin {
160
220
  },
161
221
  ],
162
222
  },
223
+ {
224
+ test: VAPOR_STYLESHEET_PATTERN,
225
+ type: 'css',
226
+ },
227
+ {
228
+ test: VAPOR_CSS_MODULE_PATTERN,
229
+ type: 'css/module',
230
+ // `const styles = defineStyle(…)` imports the class names as the default.
231
+ parser: { namedExports: false },
232
+ },
163
233
  );
164
234
  }
165
235
  }
@@ -1,17 +1,8 @@
1
1
  /** @import { LoaderContext } from '@rspack/core' */
2
+ /** @import { VaporOptions, VueJsxVapor } from './vue-jsx-vapor.js' */
2
3
 
3
- import { createRequire } from 'node:module';
4
4
  import { SourceMapConsumer, SourceMapGenerator } from 'source-map';
5
-
6
- const require = createRequire(import.meta.url);
7
- const { transformVueJsxVapor } = require('vue-jsx-vapor/api');
8
-
9
- const DEFAULT_VAPOR_OPTIONS = {
10
- macros: true,
11
- compiler: {
12
- runtimeModuleName: 'vue-jsx-vapor',
13
- },
14
- };
5
+ import { create_vue_jsx_vapor } from './vue-jsx-vapor.js';
15
6
 
16
7
  /**
17
8
  * @param {any} inputMap
@@ -63,10 +54,12 @@ async function composeSourceMaps(inputMap, outputMap) {
63
54
  }
64
55
 
65
56
  /**
66
- * Runs the compiled TSX through `vue-jsx-vapor`'s public transform API, then
67
- * composes its sourcemap with the upstream tsrx compile map when available.
57
+ * Runs the compiled TSX through `vue-jsx-vapor`'s macros and Vapor transform,
58
+ * then composes their sourcemaps with the upstream tsrx compile map when
59
+ * available. The plugin passes the `vueJsxVapor` it serves the macros' helper
60
+ * modules from.
68
61
  *
69
- * @this {LoaderContext<{ vapor?: { macros?: boolean | object, compiler?: { runtimeModuleName?: string } } }>}
62
+ * @this {LoaderContext<{ vapor?: VaporOptions, vueJsxVapor?: VueJsxVapor }>}
70
63
  * @param {string} source
71
64
  * @param {unknown} inputMap
72
65
  * @returns {void}
@@ -74,28 +67,17 @@ async function composeSourceMaps(inputMap, outputMap) {
74
67
  export default function vaporLoader(source, inputMap) {
75
68
  const callback = this.async();
76
69
  const options = this.getOptions?.() ?? {};
77
- const { interop: _interop, ...vapor_options } =
78
- /** @type {{ interop?: boolean, macros?: boolean | object, compiler?: { runtimeModuleName?: string } }} */ (
79
- options.vapor ?? {}
80
- );
81
- const vapor = {
82
- ...DEFAULT_VAPOR_OPTIONS,
83
- ...vapor_options,
84
- compiler: {
85
- ...DEFAULT_VAPOR_OPTIONS.compiler,
86
- ...vapor_options.compiler,
87
- },
88
- };
89
-
90
- const vaporId = this.resourcePath.replace(/\.tsrx$/, '.tsx');
70
+ const vue_jsx_vapor = options.vueJsxVapor ?? create_vue_jsx_vapor(options.vapor);
71
+ const resourcePath = this.resourcePath;
91
72
 
92
73
  Promise.resolve()
93
74
  .then(async () => {
94
- const result = transformVueJsxVapor(source, vaporId, vapor, true, false, false);
95
- const normalized_input_map =
75
+ const result = await vue_jsx_vapor.transform(source, resourcePath);
76
+ let output_map =
96
77
  typeof inputMap === 'string' ? JSON.parse(inputMap) : /** @type {any} */ (inputMap ?? null);
97
- const normalized_output_map = result.map ? JSON.parse(result.map) : null;
98
- const output_map = await composeSourceMaps(normalized_input_map, normalized_output_map);
78
+ for (const map of result.maps) {
79
+ output_map = await composeSourceMaps(output_map, map ?? null);
80
+ }
99
81
 
100
82
  callback(null, result.code, /** @type {any} */ (output_map ?? undefined));
101
83
  })
@@ -0,0 +1,167 @@
1
+ import { createRequire } from 'node:module';
2
+
3
+ const require = createRequire(import.meta.url);
4
+ const { transformVueJsxVapor } = require('vue-jsx-vapor/api');
5
+ // vue-jsx-vapor's plugin hooks as its Rollup plugin. Called outside Rollup,
6
+ // each hook applies its own `filter` first, as unplugin does for Rollup
7
+ // versions without hook filters.
8
+ const { default: create_vue_jsx_vapor_plugins } = require('vue-jsx-vapor/rollup');
9
+
10
+ // The macros import helper modules by ids such as `/vue-jsx-vapor/props`.
11
+ export const VAPOR_HELPER_ID_PATTERN = /^\/vue-jsx-vapor\//;
12
+ // A `defineStyle` stylesheet: `lang.css`, or `lang.module.css` for a CSS module.
13
+ export const VAPOR_STYLESHEET_ID_PATTERN =
14
+ /^\/vue-jsx-vapor\/macros\/define-style\/.*&lang\.(module\.)?(\w+)$/;
15
+ const VAPOR_STYLESHEET_IMPORT_PATTERN = /"(\/vue-jsx-vapor\/macros\/define-style\/[^"]+)"/g;
16
+ // A stylesheet's import carries its source, so loading it needs no state.
17
+ const VAPOR_SOURCE_PARAM = '&vapor-source=';
18
+
19
+ const DEFAULT_VAPOR_OPTIONS = {
20
+ macros: true,
21
+ compiler: {
22
+ runtimeModuleName: 'vue-jsx-vapor',
23
+ },
24
+ };
25
+
26
+ /**
27
+ * @typedef {{ macros?: boolean | object, compiler?: { runtimeModuleName?: string } }} VaporOptions
28
+ * @typedef {{ code: string, map?: unknown }} VaporHookResult
29
+ * @typedef {((...args: string[]) => unknown) | { handler: (...args: string[]) => unknown }} VueJsxVaporHook
30
+ * @typedef {{ name: string, load?: VueJsxVaporHook, transform?: VueJsxVaporHook }} VueJsxVaporPlugin
31
+ */
32
+
33
+ /**
34
+ * @param {VaporOptions | undefined} options
35
+ */
36
+ function resolve_vapor_options(options) {
37
+ const { interop: _interop, ...rest } = /** @type {VaporOptions & { interop?: boolean }} */ (
38
+ options ?? {}
39
+ );
40
+ return {
41
+ ...DEFAULT_VAPOR_OPTIONS,
42
+ ...rest,
43
+ compiler: {
44
+ ...DEFAULT_VAPOR_OPTIONS.compiler,
45
+ ...rest.compiler,
46
+ },
47
+ };
48
+ }
49
+
50
+ /**
51
+ * Calls a vue-jsx-vapor plugin hook and returns the code it produced, if any.
52
+ *
53
+ * @param {VueJsxVaporHook} hook
54
+ * @param {...string} args
55
+ * @returns {Promise<VaporHookResult | undefined>}
56
+ */
57
+ async function call_vapor_hook(hook, ...args) {
58
+ const result = /** @type {string | VaporHookResult | null | undefined} */ (
59
+ await (typeof hook === 'function' ? hook : hook.handler)(...args)
60
+ );
61
+ return typeof result === 'string' ? { code: result } : (result ?? undefined);
62
+ }
63
+
64
+ /**
65
+ * Splits a helper module request into its id and, for a stylesheet, the
66
+ * source its import carries.
67
+ *
68
+ * @param {string} request
69
+ * @returns {{ id: string, source: string | undefined }}
70
+ */
71
+ export function parse_vapor_helper_request(request) {
72
+ const index = request.indexOf(VAPOR_SOURCE_PARAM);
73
+ if (index === -1) return { id: request, source: undefined };
74
+ const source = request.slice(index + VAPOR_SOURCE_PARAM.length);
75
+ return { id: request.slice(0, index), source: Buffer.from(source, 'base64url').toString() };
76
+ }
77
+
78
+ /**
79
+ * vue-jsx-vapor set up as its Vite plugin sets it up: the macros, unless
80
+ * `macros` is false, expand before the Vapor transform, and the helper modules
81
+ * they import load through the same plugin hooks.
82
+ *
83
+ * @param {VaporOptions | undefined} options
84
+ */
85
+ export function create_vue_jsx_vapor(options) {
86
+ const vapor_options = resolve_vapor_options(options);
87
+ /** @type {VueJsxVaporPlugin[]} */
88
+ const plugins = [create_vue_jsx_vapor_plugins(vapor_options)].flat();
89
+ const macros = plugins.find((plugin) => plugin.name === '@vue-jsx-vapor/macros')?.transform;
90
+
91
+ /**
92
+ * The first load hook's result for a helper module id.
93
+ *
94
+ * @param {string} id
95
+ * @returns {Promise<string | undefined>}
96
+ */
97
+ async function load(id) {
98
+ for (const plugin of plugins) {
99
+ const code = plugin.load && (await call_vapor_hook(plugin.load, id))?.code;
100
+ if (code !== undefined) return code;
101
+ }
102
+ return undefined;
103
+ }
104
+
105
+ return {
106
+ /**
107
+ * Expands the macros, unless their `include` and `exclude` options leave
108
+ * the module out, then runs the Vapor transform. Each `defineStyle`
109
+ * import gets its stylesheet's source, so a cached module still loads it.
110
+ *
111
+ * @param {string} code
112
+ * @param {string} file_path the `.tsrx` module's path
113
+ * @returns {Promise<{ code: string, maps: unknown[] }>} the code, and the
114
+ * source map of each step in order
115
+ */
116
+ async transform(code, file_path) {
117
+ // The macros' `include` and `exclude` see the id the Vite plugin gives it.
118
+ const expanded = macros ? await call_vapor_hook(macros, code, `${file_path}.tsx`) : undefined;
119
+ // vue-jsx-vapor's HMR code is written for Vite's `import.meta.hot`, and its
120
+ // SSR output is for Vue's server renderer, so both stay off, as in
121
+ // vue-jsx-vapor's own Rspack plugin.
122
+ const result = transformVueJsxVapor(
123
+ expanded?.code ?? code,
124
+ file_path.replace(/\.tsrx$/, '.tsx'),
125
+ vapor_options,
126
+ true,
127
+ false,
128
+ false,
129
+ );
130
+ /** @type {Map<string, string>} */
131
+ const sources = new Map();
132
+ for (const [, id] of String(result.code).matchAll(VAPOR_STYLESHEET_IMPORT_PATTERN)) {
133
+ const source = await load(id);
134
+ if (source !== undefined) sources.set(id, source);
135
+ }
136
+ return {
137
+ code: String(result.code).replace(VAPOR_STYLESHEET_IMPORT_PATTERN, (specifier, id) => {
138
+ const source = sources.get(id);
139
+ if (source === undefined) return specifier;
140
+ const encoded = Buffer.from(source).toString('base64url');
141
+ return JSON.stringify(id + VAPOR_SOURCE_PARAM + encoded);
142
+ }),
143
+ maps: [expanded?.map, result.map ? JSON.parse(result.map) : undefined],
144
+ };
145
+ },
146
+
147
+ /**
148
+ * Loads a helper module by its `/vue-jsx-vapor/…` id, or takes the source
149
+ * its import carries, then runs the transforms for helper modules, such
150
+ * as the one that compiles a `defineStyle` stylesheet.
151
+ *
152
+ * @param {string} id
153
+ * @param {string} [source]
154
+ * @returns {Promise<string | undefined>}
155
+ */
156
+ async load_helper(id, source) {
157
+ let code = source ?? (await load(id));
158
+ if (code === undefined) return undefined;
159
+ for (const { transform } of plugins) {
160
+ code = (transform && (await call_vapor_hook(transform, code, id))?.code) ?? code;
161
+ }
162
+ return code;
163
+ },
164
+ };
165
+ }
166
+
167
+ /** @typedef {ReturnType<typeof create_vue_jsx_vapor>} VueJsxVapor */