@lynx-js/react-webpack-plugin 0.9.0 → 0.9.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/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @lynx-js/react-webpack-plugin
2
2
 
3
+ ## 0.9.1
4
+
5
+ ### Patch Changes
6
+
7
+ - Support rstest for testing library using a dedicated testing loader. ([#2328](https://github.com/lynx-family/lynx-stack/pull/2328))
8
+
9
+ - fix(rstest): normalize partial `compat` options in the testing loader ([#2464](https://github.com/lynx-family/lynx-stack/pull/2464))
10
+
11
+ The testing loader forwards `compat` directly to `transformReactLynxSync` without normalization. When `compat` is supplied as a partial object, the required `target` field is absent and the WASM transform throws `Error: Missing field 'target'`. Added the same normalization already present in `getCommonOptions` for the background/main-thread loaders: fills in `target: 'MIXED'` and all other required fields with sensible defaults.
12
+
13
+ - Add `enableUiSourceMap` option to enable UI source map generation and debug-metadata asset emission. ([#2402](https://github.com/lynx-family/lynx-stack/pull/2402))
14
+
3
15
  ## 0.9.0
4
16
 
5
17
  ### Minor Changes
@@ -101,7 +101,7 @@ declare class ReactWebpackPlugin {
101
101
  *
102
102
  * @public
103
103
  */
104
- static loaders: Record<keyof typeof LAYERS, string>;
104
+ static loaders: Record<keyof typeof LAYERS | 'TESTING', string>;
105
105
  constructor(options?: ReactWebpackPluginOptions | undefined);
106
106
  /**
107
107
  * `defaultOptions` is the default options that the {@link ReactWebpackPlugin} uses.
@@ -60,6 +60,7 @@ class ReactWebpackPlugin {
60
60
  static { this.loaders = {
61
61
  BACKGROUND: require.resolve('../lib/loaders/background.js'),
62
62
  MAIN_THREAD: require.resolve('../lib/loaders/main-thread.js'),
63
+ TESTING: require.resolve('../lib/loaders/testing.js'),
63
64
  }; }
64
65
  constructor(options) {
65
66
  this.options = options;
@@ -2,6 +2,7 @@
2
2
  // Licensed under the Apache License Version 2.0 that can be found in the
3
3
  // LICENSE file in the root directory of this source tree.
4
4
  import { createRequire } from 'node:module';
5
+ import { UI_SOURCE_MAP_RECORDS_BUILD_INFO } from '@lynx-js/template-webpack-plugin';
5
6
  import { getMainThreadTransformOptions } from './options.js';
6
7
  const mainThreadLoader = function (content, sourceMap) {
7
8
  const require = createRequire(import.meta.url);
@@ -75,6 +76,11 @@ const mainThreadLoader = function (content, sourceMap) {
75
76
  this.emitWarning(new Error(warning.text));
76
77
  }
77
78
  }
79
+ const currentModule = this._module;
80
+ const buildInfo = currentModule?.buildInfo;
81
+ if (buildInfo) {
82
+ buildInfo[UI_SOURCE_MAP_RECORDS_BUILD_INFO] = result.uiSourceMapRecords;
83
+ }
78
84
  this.callback(null, result.code + (this.hot
79
85
  // TODO: temporary fix LEPUS error `$RefreshReg$ is not defined`
80
86
  // should make react-refresh transform in `react-transform`.
@@ -1,5 +1,10 @@
1
1
  import type { LoaderContext } from '@rspack/core';
2
2
  import type { CompatVisitorConfig, DefineDceVisitorConfig, JsxTransformerConfig, ShakeVisitorConfig, TransformNodiffOptions } from '@lynx-js/react/transform';
3
+ export declare const JSX_IMPORT_SOURCE: {
4
+ MAIN_THREAD: string;
5
+ BACKGROUND: string;
6
+ };
7
+ export declare const RUNTIME_PKG = "@lynx-js/react/internal";
3
8
  /**
4
9
  * The options of the ReactLynx plugin.
5
10
  * @public
@@ -17,6 +22,10 @@ export interface ReactLoaderOptions {
17
22
  * {@inheritdoc @lynx-js/react-rsbuild-plugin#PluginReactLynxOptions.jsx}
18
23
  */
19
24
  jsx?: JsxTransformerConfig | undefined;
25
+ /**
26
+ * {@inheritdoc @lynx-js/react-rsbuild-plugin#PluginReactLynxOptions.enableUiSourceMap}
27
+ */
28
+ enableUiSourceMap?: boolean | undefined;
20
29
  /**
21
30
  * Enable the Fast Refresh for ReactLynx.
22
31
  */
@@ -3,12 +3,12 @@
3
3
  // LICENSE file in the root directory of this source tree.
4
4
  import path from 'node:path';
5
5
  const PLUGIN_NAME = 'react:webpack';
6
- const JSX_IMPORT_SOURCE = {
6
+ export const JSX_IMPORT_SOURCE = {
7
7
  MAIN_THREAD: '@lynx-js/react/lepus',
8
8
  BACKGROUND: '@lynx-js/react',
9
9
  };
10
10
  const PUBLIC_RUNTIME_PKG = '@lynx-js/react';
11
- const RUNTIME_PKG = '@lynx-js/react/internal';
11
+ export const RUNTIME_PKG = '@lynx-js/react/internal';
12
12
  const OLD_RUNTIME_PKG = '@lynx-js/react-runtime';
13
13
  const COMPONENT_PKG = '@lynx-js/react-components';
14
14
  function normalizeSlashes(file) {
@@ -16,7 +16,7 @@ function normalizeSlashes(file) {
16
16
  }
17
17
  function getCommonOptions(inputSourceMap) {
18
18
  const filename = normalizeSlashes(path.relative(this.rootContext, this.resourcePath));
19
- const { compat, enableRemoveCSSScope, inlineSourcesContent, isDynamicComponent, engineVersion, defineDCE = { define: {} }, } = this.getOptions();
19
+ const { compat, enableRemoveCSSScope, enableUiSourceMap, inlineSourcesContent, isDynamicComponent, engineVersion, defineDCE = { define: {} }, } = this.getOptions();
20
20
  const syntax = (/\.[mc]?tsx?$/.exec(this.resourcePath))
21
21
  ? 'typescript'
22
22
  : 'ecmascript';
@@ -70,6 +70,7 @@ function getCommonOptions(inputSourceMap) {
70
70
  // This allows serializing the updated runtime code to Lepus using `Function.prototype.toString`.
71
71
  ? 'MIXED'
72
72
  : 'JS',
73
+ enableUiSourceMap: enableUiSourceMap ?? false,
73
74
  runtimePkg: RUNTIME_PKG,
74
75
  filename,
75
76
  isDynamicComponent: isDynamicComponent ?? false,
@@ -0,0 +1,4 @@
1
+ import type { LoaderContext } from '@rspack/core';
2
+ import type { ReactLoaderOptions } from './options.js';
3
+ declare function testingLoader(this: LoaderContext<ReactLoaderOptions>, content: string): void;
4
+ export default testingLoader;
@@ -0,0 +1,118 @@
1
+ // Copyright 2024 The Lynx Authors. All rights reserved.
2
+ // Licensed under the Apache License Version 2.0 that can be found in the
3
+ // LICENSE file in the root directory of this source tree.
4
+ import { createRequire } from 'node:module';
5
+ import path from 'node:path';
6
+ import { JSX_IMPORT_SOURCE, RUNTIME_PKG } from './options.js';
7
+ function normalizeSlashes(file) {
8
+ return file.replaceAll(path.win32.sep, '/');
9
+ }
10
+ function testingLoader(content) {
11
+ const require = createRequire(import.meta.url);
12
+ const { compat = false, defineDCE = { define: {} }, engineVersion = '', shake = false, transformPath = '@lynx-js/react/transform', } = this.getOptions();
13
+ const { transformReactLynxSync } = require(transformPath);
14
+ const filename = normalizeSlashes(path.relative(this.rootContext, this.resourcePath));
15
+ const normalizedCompat = typeof compat === 'object'
16
+ ? {
17
+ target: 'MIXED',
18
+ addComponentElement: compat.addComponentElement ?? false,
19
+ additionalComponentAttributes: compat.additionalComponentAttributes ?? [],
20
+ componentsPkg: compat.componentsPkg ?? ['@lynx-js/react-components'],
21
+ disableDeprecatedWarning: compat.disableDeprecatedWarning ?? false,
22
+ newRuntimePkg: compat.newRuntimePkg ?? '@lynx-js/react',
23
+ oldRuntimePkg: compat.oldRuntimePkg ?? ['@lynx-js/react-runtime'],
24
+ simplifyCtorLikeReactLynx2: compat.simplifyCtorLikeReactLynx2 ?? false,
25
+ ...(typeof compat.removeComponentAttrRegex === 'string' && {
26
+ removeComponentAttrRegex: compat.removeComponentAttrRegex,
27
+ }),
28
+ darkMode: compat.darkMode ?? false,
29
+ }
30
+ : (compat ?? false);
31
+ const result = transformReactLynxSync(content, {
32
+ mode: 'test',
33
+ pluginName: '',
34
+ filename: this.resourcePath,
35
+ sourcemap: true,
36
+ snapshot: {
37
+ preserveJsx: false,
38
+ runtimePkg: RUNTIME_PKG,
39
+ jsxImportSource: JSX_IMPORT_SOURCE.BACKGROUND,
40
+ filename,
41
+ target: 'MIXED',
42
+ },
43
+ // snapshot: true,
44
+ directiveDCE: false,
45
+ defineDCE,
46
+ shake,
47
+ compat: normalizedCompat,
48
+ engineVersion,
49
+ worklet: {
50
+ filename,
51
+ runtimePkg: RUNTIME_PKG,
52
+ target: 'MIXED',
53
+ },
54
+ refresh: false,
55
+ cssScope: false,
56
+ });
57
+ if (result.errors.length > 0) {
58
+ for (const error of result.errors) {
59
+ if (this.experiments?.emitDiagnostic) {
60
+ // Rspack with `emitDiagnostic` API
61
+ try {
62
+ this.experiments.emitDiagnostic({
63
+ message: error.text,
64
+ sourceCode: content,
65
+ location: {
66
+ line: error.location?.line ?? 1,
67
+ column: error.location?.column ?? 0,
68
+ length: error.location?.length ?? 0,
69
+ text: error.text ?? '',
70
+ },
71
+ severity: 'error',
72
+ });
73
+ }
74
+ catch {
75
+ // Rspack may throw on invalid line & column when containing UTF-8.
76
+ // We catch it up here.
77
+ this.emitError(new Error(error.text));
78
+ }
79
+ }
80
+ else {
81
+ // Webpack or legacy Rspack
82
+ this.emitError(new Error(error.text));
83
+ }
84
+ }
85
+ this.callback(new Error('react-transform failed'));
86
+ return;
87
+ }
88
+ for (const warning of result.warnings) {
89
+ if (this.experiments?.emitDiagnostic) {
90
+ // Rspack with `emitDiagnostic` API
91
+ try {
92
+ this.experiments.emitDiagnostic({
93
+ message: warning.text,
94
+ sourceCode: content,
95
+ location: {
96
+ line: warning.location?.line ?? 1,
97
+ column: warning.location?.column ?? 0,
98
+ length: warning.location?.length ?? 0,
99
+ text: warning.text ?? '',
100
+ },
101
+ severity: 'warning',
102
+ });
103
+ }
104
+ catch {
105
+ // Rspack may throw on invalid line & column when containing UTF-8.
106
+ // We catch it up here.
107
+ this.emitWarning(new Error(warning.text));
108
+ }
109
+ }
110
+ else {
111
+ // Webpack or legacy Rspack
112
+ this.emitWarning(new Error(warning.text));
113
+ }
114
+ }
115
+ this.callback(null, result.code, result.map);
116
+ }
117
+ export default testingLoader;
118
+ //# sourceMappingURL=testing.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lynx-js/react-webpack-plugin",
3
- "version": "0.9.0",
3
+ "version": "0.9.1",
4
4
  "description": "A webpack plugin for ReactLynx",
5
5
  "keywords": [
6
6
  "webpack",
@@ -43,9 +43,9 @@
43
43
  "swc-loader": "^0.2.7",
44
44
  "webpack": "^5.105.2",
45
45
  "@lynx-js/css-extract-webpack-plugin": "0.7.0",
46
- "@lynx-js/react": "0.118.0",
46
+ "@lynx-js/react": "0.119.0",
47
+ "@lynx-js/template-webpack-plugin": "0.10.9",
47
48
  "@lynx-js/test-tools": "0.0.0",
48
- "@lynx-js/template-webpack-plugin": "0.10.8",
49
49
  "@lynx-js/vitest-setup": "0.0.0"
50
50
  },
51
51
  "peerDependencies": {