@unpackjs/plugin-react 1.6.0 → 1.6.2

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.
@@ -35,7 +35,7 @@ var import_node_http = __toESM(require("node:http"));
35
35
  var import_node_path = __toESM(require("node:path"));
36
36
  var import_core = require("@unpackjs/core");
37
37
  var import_launch_editor = __toESM(require("launch-editor"));
38
- function createServer() {
38
+ const createServer = () => {
39
39
  return new Promise((resolve) => {
40
40
  const server = import_node_http.default.createServer((req, res) => {
41
41
  const params = new URLSearchParams(req.url.slice(1));
@@ -59,30 +59,42 @@ function createServer() {
59
59
  });
60
60
  });
61
61
  });
62
- }
62
+ };
63
63
  const addClickToComponentSupport = async ({
64
64
  config,
65
- bundler
65
+ unpackConfig
66
66
  }) => {
67
- if (!(0, import_core.isDev)())
68
- return config;
69
67
  const { port } = await createServer();
70
68
  const injectScript = import_node_fs.default.readFileSync(import_node_path.default.resolve(__dirname, "./injectScript.js"), "utf-8");
71
- config.plugins.push(
72
- new bundler.BannerPlugin({
73
- banner: `
69
+ const scriptTpl = `
74
70
  if (!document.querySelector('#__click-to-component__')) {
75
71
  const script = document.createElement('script')
76
72
  window['CLICK_TO_COMPONENT'] = { port: ${port} }
77
73
  script.setAttribute('type', 'text/javascript')
78
74
  script.textContent = \`${injectScript}\`
79
75
  document.body.appendChild(script)
80
- }`,
81
- entryOnly: true,
82
- raw: true,
83
- exclude: /\.css$/
84
- })
85
- );
76
+ }`;
77
+ if (unpackConfig.mpa) {
78
+ Object.values(config.entry).forEach((entryConfig) => {
79
+ const filePath = entryConfig.import[0];
80
+ const content = import_node_fs.default.readFileSync(filePath, "utf-8");
81
+ import_node_fs.default.writeFileSync(filePath, `${scriptTpl}
82
+ ${content}`, "utf-8");
83
+ });
84
+ } else {
85
+ const realEntryPath = import_node_path.default.resolve(config.context, import_core.TEMP_DIR, "entry.jsx");
86
+ const tpl = `
87
+ ${scriptTpl}
88
+ import '${(0, import_core.getPathInJs)(import_node_path.default.resolve(Object.values(config.entry)[0]))}'
89
+ `;
90
+ const dir = import_node_path.default.dirname(realEntryPath);
91
+ if (!import_node_fs.default.existsSync(dir))
92
+ import_node_fs.default.mkdirSync(dir, { recursive: true });
93
+ import_node_fs.default.writeFileSync(realEntryPath, tpl, "utf-8");
94
+ config.entry = {
95
+ main: realEntryPath
96
+ };
97
+ }
86
98
  return config;
87
99
  };
88
100
  // Annotate the CommonJS export names for ESM import in node:
@@ -1,6 +1,6 @@
1
- import { type BundlerConfiguration } from '@unpackjs/core';
2
- export declare const addClickToComponentSupport: ({ config, bundler, }: {
1
+ import { type BundlerConfiguration, type UnpackConfig } from '@unpackjs/core';
2
+ export declare const addClickToComponentSupport: ({ config, unpackConfig, }: {
3
3
  config: BundlerConfiguration;
4
- bundler: any;
4
+ unpackConfig: UnpackConfig;
5
5
  }) => Promise<BundlerConfiguration>;
6
6
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/click-to-component/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,KAAK,oBAAoB,EAAkB,MAAM,gBAAgB,CAAA;AA4B1E,eAAO,MAAM,0BAA0B,yBAGpC;IACD,MAAM,EAAE,oBAAoB,CAAA;IAC5B,OAAO,EAAE,GAAG,CAAA;CACb,KAAG,OAAO,CAAC,oBAAoB,CAoB/B,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/click-to-component/index.ts"],"names":[],"mappings":"AAGA,OAAO,EACL,KAAK,oBAAoB,EAEzB,KAAK,YAAY,EAGlB,MAAM,gBAAgB,CAAA;AA4BvB,eAAO,MAAM,0BAA0B,8BAGpC;IACD,MAAM,EAAE,oBAAoB,CAAA;IAC5B,YAAY,EAAE,YAAY,CAAA;CAC3B,KAAG,OAAO,CAAC,oBAAoB,CA+B/B,CAAA"}
@@ -10,9 +10,13 @@ var __filename = /* @__PURE__ */ getFilename();
10
10
  import fs from "node:fs";
11
11
  import http from "node:http";
12
12
  import path2 from "node:path";
13
- import { getPort, isDev } from "@unpackjs/core";
13
+ import {
14
+ TEMP_DIR,
15
+ getPathInJs,
16
+ getPort
17
+ } from "@unpackjs/core";
14
18
  import launchEditor from "launch-editor";
15
- function createServer() {
19
+ const createServer = () => {
16
20
  return new Promise((resolve) => {
17
21
  const server = http.createServer((req, res) => {
18
22
  const params = new URLSearchParams(req.url.slice(1));
@@ -36,30 +40,42 @@ function createServer() {
36
40
  });
37
41
  });
38
42
  });
39
- }
43
+ };
40
44
  const addClickToComponentSupport = async ({
41
45
  config,
42
- bundler
46
+ unpackConfig
43
47
  }) => {
44
- if (!isDev())
45
- return config;
46
48
  const { port } = await createServer();
47
49
  const injectScript = fs.readFileSync(path2.resolve(__dirname, "./injectScript.js"), "utf-8");
48
- config.plugins.push(
49
- new bundler.BannerPlugin({
50
- banner: `
50
+ const scriptTpl = `
51
51
  if (!document.querySelector('#__click-to-component__')) {
52
52
  const script = document.createElement('script')
53
53
  window['CLICK_TO_COMPONENT'] = { port: ${port} }
54
54
  script.setAttribute('type', 'text/javascript')
55
55
  script.textContent = \`${injectScript}\`
56
56
  document.body.appendChild(script)
57
- }`,
58
- entryOnly: true,
59
- raw: true,
60
- exclude: /\.css$/
61
- })
62
- );
57
+ }`;
58
+ if (unpackConfig.mpa) {
59
+ Object.values(config.entry).forEach((entryConfig) => {
60
+ const filePath = entryConfig.import[0];
61
+ const content = fs.readFileSync(filePath, "utf-8");
62
+ fs.writeFileSync(filePath, `${scriptTpl}
63
+ ${content}`, "utf-8");
64
+ });
65
+ } else {
66
+ const realEntryPath = path2.resolve(config.context, TEMP_DIR, "entry.jsx");
67
+ const tpl = `
68
+ ${scriptTpl}
69
+ import '${getPathInJs(path2.resolve(Object.values(config.entry)[0]))}'
70
+ `;
71
+ const dir = path2.dirname(realEntryPath);
72
+ if (!fs.existsSync(dir))
73
+ fs.mkdirSync(dir, { recursive: true });
74
+ fs.writeFileSync(realEntryPath, tpl, "utf-8");
75
+ config.entry = {
76
+ main: realEntryPath
77
+ };
78
+ }
63
79
  return config;
64
80
  };
65
81
  export {
package/dist/index.cjs CHANGED
@@ -39,17 +39,23 @@ var import_click_to_component = require("./click-to-component/index.cjs");
39
39
  var import_mpa = require("./mpa.cjs");
40
40
  const PLUGIN_NAME = "unpack:react";
41
41
  const pluginReact = (options = {}) => {
42
+ const { clickToComponent, reactCompiler } = options;
42
43
  return {
43
44
  name: PLUGIN_NAME,
44
- bundlerConfig: async (originalConfig, { unpackConfig, mergeConfig, bundler }) => {
45
+ bundlerConfig: async (originalConfig, { unpackConfig, mergeConfig }) => {
45
46
  let config = originalConfig;
46
- if (options.clickToComponent) {
47
- config = await (0, import_click_to_component.addClickToComponentSupport)({ config, bundler });
48
- }
49
47
  config = (0, import_mpa.addMpaSupport)({ config, unpackConfig, mergeConfig });
48
+ if ((0, import_core.isDev)() && clickToComponent) {
49
+ config = await (0, import_click_to_component.addClickToComponentSupport)({ config, unpackConfig });
50
+ }
51
+ if (reactCompiler && !(0, import_core.getUserDepPath)(unpackConfig.root, "react-compiler-runtime")) {
52
+ import_core.logger.error('Dependency "react-compiler-runtime" not found. Did you install it?');
53
+ process.exit(1);
54
+ }
55
+ const isRspack = unpackConfig.bundler === "rspack";
50
56
  const jsExclude = [/node_modules[\\/](?!\.unpack)/];
51
- const swcLoader = unpackConfig.bundler === "rspack" ? "builtin:swc-loader" : require.resolve("swc-loader");
52
- const ReactRefreshPlugin = unpackConfig.bundler === "rspack" ? import_plugin_react_refresh.default : import_react_refresh_webpack_plugin.default;
57
+ const swcLoader = isRspack ? "builtin:swc-loader" : require.resolve("swc-loader");
58
+ const ReactRefreshPlugin = isRspack ? import_plugin_react_refresh.default : import_react_refresh_webpack_plugin.default;
53
59
  const getSwcOptions = ({ tsx }) => {
54
60
  return {
55
61
  jsc: {
@@ -84,8 +90,22 @@ const pluginReact = (options = {}) => {
84
90
  {
85
91
  test: /\.(j|t)sx$/,
86
92
  exclude: jsExclude,
87
- loader: swcLoader,
88
- options: getSwcOptions({ tsx: true })
93
+ use: [
94
+ {
95
+ loader: swcLoader,
96
+ options: getSwcOptions({ tsx: true })
97
+ },
98
+ reactCompiler && unpackConfig.build?.parallel && {
99
+ loader: import_node_path.default.resolve(__dirname, "../../core/dist/thread-loader/index.cjs"),
100
+ options: import_core.THREAD_OPTIONS
101
+ },
102
+ reactCompiler && {
103
+ loader: import_node_path.default.resolve(__dirname, "./react-compiler-loader/index.cjs"),
104
+ options: {
105
+ target: (0, import_core.getUserDepVersion)(unpackConfig.root, "react").split(".")[0]
106
+ }
107
+ }
108
+ ].filter(Boolean)
89
109
  }
90
110
  ]
91
111
  },
@@ -93,7 +113,7 @@ const pluginReact = (options = {}) => {
93
113
  (0, import_core.isDevServer)() && new ReactRefreshPlugin({
94
114
  overlay: false
95
115
  })
96
- ],
116
+ ].filter(Boolean),
97
117
  resolve: {
98
118
  alias: {
99
119
  "@swc/helpers": import_node_path.default.dirname(require.resolve("@swc/helpers/package.json"))
package/dist/index.d.ts CHANGED
@@ -2,6 +2,7 @@ import { type UnpackPlugin } from '@unpackjs/core';
2
2
  export declare const PLUGIN_NAME = "unpack:react";
3
3
  export type PluginReactOptions = {
4
4
  clickToComponent?: boolean;
5
+ reactCompiler?: boolean;
5
6
  };
6
7
  export declare const pluginReact: (options?: PluginReactOptions) => UnpackPlugin;
7
8
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAwB,KAAK,YAAY,EAAe,MAAM,gBAAgB,CAAA;AAIrF,eAAO,MAAM,WAAW,iBAAiB,CAAA;AAEzC,MAAM,MAAM,kBAAkB,GAAG;IAC/B,gBAAgB,CAAC,EAAE,OAAO,CAAA;CAC3B,CAAA;AACD,eAAO,MAAM,WAAW,aAAa,kBAAkB,KAAQ,YAmE9D,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAGL,KAAK,YAAY,EAMlB,MAAM,gBAAgB,CAAA;AAIvB,eAAO,MAAM,WAAW,iBAAiB,CAAA;AAEzC,MAAM,MAAM,kBAAkB,GAAG;IAC/B,gBAAgB,CAAC,EAAE,OAAO,CAAA;IAC1B,aAAa,CAAC,EAAE,OAAO,CAAA;CACxB,CAAA;AACD,eAAO,MAAM,WAAW,aAAa,kBAAkB,KAAQ,YAsF9D,CAAA"}
package/dist/index.js CHANGED
@@ -10,22 +10,36 @@ var __filename = /* @__PURE__ */ getFilename();
10
10
  import path2 from "node:path";
11
11
  import WebpackReactRefreshPlugin from "@pmmmwh/react-refresh-webpack-plugin";
12
12
  import RspackReactRefreshPlugin from "@rspack/plugin-react-refresh";
13
- import { DEFAULT_BROWSERSLIST, isDevServer } from "@unpackjs/core";
13
+ import {
14
+ DEFAULT_BROWSERSLIST,
15
+ THREAD_OPTIONS,
16
+ getUserDepPath,
17
+ getUserDepVersion,
18
+ isDev,
19
+ isDevServer,
20
+ logger
21
+ } from "@unpackjs/core";
14
22
  import { addClickToComponentSupport } from "./click-to-component/index.js";
15
23
  import { addMpaSupport } from "./mpa.js";
16
24
  const PLUGIN_NAME = "unpack:react";
17
25
  const pluginReact = (options = {}) => {
26
+ const { clickToComponent, reactCompiler } = options;
18
27
  return {
19
28
  name: PLUGIN_NAME,
20
- bundlerConfig: async (originalConfig, { unpackConfig, mergeConfig, bundler }) => {
29
+ bundlerConfig: async (originalConfig, { unpackConfig, mergeConfig }) => {
21
30
  let config = originalConfig;
22
- if (options.clickToComponent) {
23
- config = await addClickToComponentSupport({ config, bundler });
24
- }
25
31
  config = addMpaSupport({ config, unpackConfig, mergeConfig });
32
+ if (isDev() && clickToComponent) {
33
+ config = await addClickToComponentSupport({ config, unpackConfig });
34
+ }
35
+ if (reactCompiler && !getUserDepPath(unpackConfig.root, "react-compiler-runtime")) {
36
+ logger.error('Dependency "react-compiler-runtime" not found. Did you install it?');
37
+ process.exit(1);
38
+ }
39
+ const isRspack = unpackConfig.bundler === "rspack";
26
40
  const jsExclude = [/node_modules[\\/](?!\.unpack)/];
27
- const swcLoader = unpackConfig.bundler === "rspack" ? "builtin:swc-loader" : require.resolve("swc-loader");
28
- const ReactRefreshPlugin = unpackConfig.bundler === "rspack" ? RspackReactRefreshPlugin : WebpackReactRefreshPlugin;
41
+ const swcLoader = isRspack ? "builtin:swc-loader" : require.resolve("swc-loader");
42
+ const ReactRefreshPlugin = isRspack ? RspackReactRefreshPlugin : WebpackReactRefreshPlugin;
29
43
  const getSwcOptions = ({ tsx }) => {
30
44
  return {
31
45
  jsc: {
@@ -60,8 +74,22 @@ const pluginReact = (options = {}) => {
60
74
  {
61
75
  test: /\.(j|t)sx$/,
62
76
  exclude: jsExclude,
63
- loader: swcLoader,
64
- options: getSwcOptions({ tsx: true })
77
+ use: [
78
+ {
79
+ loader: swcLoader,
80
+ options: getSwcOptions({ tsx: true })
81
+ },
82
+ reactCompiler && unpackConfig.build?.parallel && {
83
+ loader: path2.resolve(__dirname, "../../core/dist/thread-loader/index.cjs"),
84
+ options: THREAD_OPTIONS
85
+ },
86
+ reactCompiler && {
87
+ loader: path2.resolve(__dirname, "./react-compiler-loader/index.cjs"),
88
+ options: {
89
+ target: getUserDepVersion(unpackConfig.root, "react").split(".")[0]
90
+ }
91
+ }
92
+ ].filter(Boolean)
65
93
  }
66
94
  ]
67
95
  },
@@ -69,7 +97,7 @@ const pluginReact = (options = {}) => {
69
97
  isDevServer() && new ReactRefreshPlugin({
70
98
  overlay: false
71
99
  })
72
- ],
100
+ ].filter(Boolean),
73
101
  resolve: {
74
102
  alias: {
75
103
  "@swc/helpers": path2.dirname(require.resolve("@swc/helpers/package.json"))
package/dist/mpa.cjs CHANGED
@@ -51,37 +51,28 @@ const addMpaSupport = ({
51
51
  }
52
52
  return pageConfig;
53
53
  };
54
- const getPathInJs = (absPath) => {
55
- return JSON.stringify(absPath).slice(1, -1);
56
- };
57
- const getAbsPathForEntry = (_path) => {
58
- const absPath = import_node_path.default.join(unpackConfig.root, _path);
59
- const validPath = import_node_fs.default.existsSync(absPath) ? absPath : _path;
60
- return getPathInJs(validPath);
61
- };
62
54
  const createTempFile = (entry2) => {
63
55
  import_node_fs.default.rmSync(import_node_path.default.join(unpackConfig.root, tempDirectory), { recursive: true, force: true });
64
56
  const globalImport = userOptions.globalImport?.reduce((acc, curr) => {
65
57
  return `${acc}
66
- import '${getAbsPathForEntry(curr)}';`.trimStart();
58
+ import '${(0, import_core.getPathInJs)(import_node_path.default.resolve(unpackConfig.root, curr))}'`;
67
59
  }, "") || "";
68
60
  const { layout } = userOptions;
69
- const layoutImport = layout ? `import Layout from '${getAbsPathForEntry(layout)}';` : "";
61
+ const layoutImport = layout ? `import Layout from '${(0, import_core.getPathInJs)(import_node_path.default.resolve(unpackConfig.root, layout))}'` : "";
70
62
  const layoutJSX = layout ? "<Layout><App /></Layout>" : "<App />";
71
63
  const rootElement = `document.getElementById('${unpackConfig.html?.mountId}')`;
72
64
  const reactDOMSource = "react-dom/client";
73
- const renderer = `ReactDOM.createRoot(${rootElement}).render(${layoutJSX});`;
65
+ const renderer = `ReactDOM.createRoot(${rootElement}).render(${layoutJSX})`;
74
66
  Object.entries(entry2).forEach(([entryName, entryConfig]) => {
75
67
  const filePath = import_node_path.default.join(unpackConfig.root, tempDirectory, `${entryName}.jsx`);
76
68
  const tpl = `
77
- // DO NOT CHANGE IT MANUALLY!
78
- import React from 'react';
79
- import ReactDOM from '${reactDOMSource}';
80
- import App from '${getPathInJs(entryConfig.import[0])}';${layoutImport && `
69
+ import React from 'react'
70
+ import ReactDOM from '${reactDOMSource}'
71
+ import App from '${(0, import_core.getPathInJs)(entryConfig.import[0])}'${layoutImport && `
81
72
  ${layoutImport}`}
82
73
  ${globalImport}
83
74
  ${renderer}
84
- `.trimStart();
75
+ `;
85
76
  const dir = import_node_path.default.dirname(filePath);
86
77
  if (!import_node_fs.default.existsSync(dir)) {
87
78
  import_node_fs.default.mkdirSync(dir, { recursive: true });
package/dist/mpa.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"mpa.d.ts","sourceRoot":"","sources":["../src/mpa.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,KAAK,oBAAoB,EAGzB,KAAK,YAAY,EAMlB,MAAM,gBAAgB,CAAA;AAavB,eAAO,MAAM,aAAa,2CAIvB;IACD,MAAM,EAAE,oBAAoB,CAAA;IAC5B,YAAY,EAAE,YAAY,CAAA;IAC1B,WAAW,EAAE,CAAC,GAAG,OAAO,EAAE,oBAAoB,EAAE,KAAK,oBAAoB,CAAA;CAC1E,KAAG,oBAoHH,CAAA"}
1
+ {"version":3,"file":"mpa.d.ts","sourceRoot":"","sources":["../src/mpa.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,KAAK,oBAAoB,EAGzB,KAAK,YAAY,EAOlB,MAAM,gBAAgB,CAAA;AAavB,eAAO,MAAM,aAAa,2CAIvB;IACD,MAAM,EAAE,oBAAoB,CAAA;IAC5B,YAAY,EAAE,YAAY,CAAA;IAC1B,WAAW,EAAE,CAAC,GAAG,OAAO,EAAE,oBAAoB,EAAE,KAAK,oBAAoB,CAAA;CAC1E,KAAG,oBAyGH,CAAA"}
package/dist/mpa.js CHANGED
@@ -13,6 +13,7 @@ import {
13
13
  TEMPLATE_CONTENT,
14
14
  TEMP_DIR,
15
15
  colors,
16
+ getPathInJs,
16
17
  isDevServer,
17
18
  isFunction,
18
19
  isPlainObject,
@@ -36,37 +37,28 @@ const addMpaSupport = ({
36
37
  }
37
38
  return pageConfig;
38
39
  };
39
- const getPathInJs = (absPath) => {
40
- return JSON.stringify(absPath).slice(1, -1);
41
- };
42
- const getAbsPathForEntry = (_path) => {
43
- const absPath = path2.join(unpackConfig.root, _path);
44
- const validPath = fs.existsSync(absPath) ? absPath : _path;
45
- return getPathInJs(validPath);
46
- };
47
40
  const createTempFile = (entry2) => {
48
41
  fs.rmSync(path2.join(unpackConfig.root, tempDirectory), { recursive: true, force: true });
49
42
  const globalImport = userOptions.globalImport?.reduce((acc, curr) => {
50
43
  return `${acc}
51
- import '${getAbsPathForEntry(curr)}';`.trimStart();
44
+ import '${getPathInJs(path2.resolve(unpackConfig.root, curr))}'`;
52
45
  }, "") || "";
53
46
  const { layout } = userOptions;
54
- const layoutImport = layout ? `import Layout from '${getAbsPathForEntry(layout)}';` : "";
47
+ const layoutImport = layout ? `import Layout from '${getPathInJs(path2.resolve(unpackConfig.root, layout))}'` : "";
55
48
  const layoutJSX = layout ? "<Layout><App /></Layout>" : "<App />";
56
49
  const rootElement = `document.getElementById('${unpackConfig.html?.mountId}')`;
57
50
  const reactDOMSource = "react-dom/client";
58
- const renderer = `ReactDOM.createRoot(${rootElement}).render(${layoutJSX});`;
51
+ const renderer = `ReactDOM.createRoot(${rootElement}).render(${layoutJSX})`;
59
52
  Object.entries(entry2).forEach(([entryName, entryConfig]) => {
60
53
  const filePath = path2.join(unpackConfig.root, tempDirectory, `${entryName}.jsx`);
61
54
  const tpl = `
62
- // DO NOT CHANGE IT MANUALLY!
63
- import React from 'react';
64
- import ReactDOM from '${reactDOMSource}';
65
- import App from '${getPathInJs(entryConfig.import[0])}';${layoutImport && `
55
+ import React from 'react'
56
+ import ReactDOM from '${reactDOMSource}'
57
+ import App from '${getPathInJs(entryConfig.import[0])}'${layoutImport && `
66
58
  ${layoutImport}`}
67
59
  ${globalImport}
68
60
  ${renderer}
69
- `.trimStart();
61
+ `;
70
62
  const dir = path2.dirname(filePath);
71
63
  if (!fs.existsSync(dir)) {
72
64
  fs.mkdirSync(dir, { recursive: true });
@@ -0,0 +1,67 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
24
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
+ mod
26
+ ));
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
+ var react_compiler_loader_exports = {};
29
+ __export(react_compiler_loader_exports, {
30
+ default: () => reactCompilerLoader
31
+ });
32
+ module.exports = __toCommonJS(react_compiler_loader_exports);
33
+ var babel = __toESM(require("@babel/core"));
34
+ var import_plugin_proposal_decorators = __toESM(require("@babel/plugin-proposal-decorators"));
35
+ var import_plugin_transform_class_properties = __toESM(require("@babel/plugin-transform-class-properties"));
36
+ var import_babel_plugin_react_compiler = __toESM(require("babel-plugin-react-compiler"));
37
+ const defaultBabelParsePlugins = ["jsx", "typescript"];
38
+ async function reactCompilerLoader(input, _inputSourceMap) {
39
+ const callback = this.async();
40
+ try {
41
+ const options = this.getOptions();
42
+ const result = await babel.transformAsync(input, {
43
+ sourceFileName: this.resourcePath,
44
+ filename: this.resourcePath,
45
+ cloneInputAst: false,
46
+ plugins: [
47
+ [import_babel_plugin_react_compiler.default, options],
48
+ [import_plugin_proposal_decorators.default, { legacy: true }],
49
+ [import_plugin_transform_class_properties.default]
50
+ ],
51
+ parserOpts: {
52
+ plugins: [...defaultBabelParsePlugins]
53
+ },
54
+ ast: false,
55
+ sourceMaps: true,
56
+ configFile: false,
57
+ babelrc: false
58
+ });
59
+ if (!result) {
60
+ throw new TypeError("babel.transformAsync with react compiler plugin returns null");
61
+ }
62
+ const { code, map } = result;
63
+ callback(null, code ?? void 0, map ?? void 0);
64
+ } catch (e) {
65
+ callback(e);
66
+ }
67
+ }
@@ -0,0 +1,5 @@
1
+ export type ReactCompilerLoaderOption = {
2
+ target: '17' | '18' | '19';
3
+ };
4
+ export default function reactCompilerLoader(this: any, input: string, _inputSourceMap: any): Promise<void>;
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/react-compiler-loader/index.ts"],"names":[],"mappings":"AAKA,MAAM,MAAM,yBAAyB,GAAG;IACtC,MAAM,EAAE,IAAI,GAAG,IAAI,GAAG,IAAI,CAAA;CAC3B,CAAA;AAMD,wBAA8B,mBAAmB,CAAC,IAAI,KAAA,EAAE,KAAK,EAAE,MAAM,EAAE,eAAe,EAAE,GAAG,iBAiC1F"}
@@ -0,0 +1,47 @@
1
+ import { createRequire } from 'node:module';
2
+ var require = createRequire(import.meta['url']);
3
+
4
+ import path from "path";
5
+ import { fileURLToPath } from "url";
6
+ var getFilename = () => fileURLToPath(import.meta.url);
7
+ var getDirname = () => path.dirname(getFilename());
8
+ var __dirname = /* @__PURE__ */ getDirname();
9
+ var __filename = /* @__PURE__ */ getFilename();
10
+ import * as babel from "@babel/core";
11
+ import PluginProposalDecorators from "@babel/plugin-proposal-decorators";
12
+ import PluginTransformClassProperties from "@babel/plugin-transform-class-properties";
13
+ import BabelPluginReactCompiler from "babel-plugin-react-compiler";
14
+ const defaultBabelParsePlugins = ["jsx", "typescript"];
15
+ async function reactCompilerLoader(input, _inputSourceMap) {
16
+ const callback = this.async();
17
+ try {
18
+ const options = this.getOptions();
19
+ const result = await babel.transformAsync(input, {
20
+ sourceFileName: this.resourcePath,
21
+ filename: this.resourcePath,
22
+ cloneInputAst: false,
23
+ plugins: [
24
+ [BabelPluginReactCompiler, options],
25
+ [PluginProposalDecorators, { legacy: true }],
26
+ [PluginTransformClassProperties]
27
+ ],
28
+ parserOpts: {
29
+ plugins: [...defaultBabelParsePlugins]
30
+ },
31
+ ast: false,
32
+ sourceMaps: true,
33
+ configFile: false,
34
+ babelrc: false
35
+ });
36
+ if (!result) {
37
+ throw new TypeError("babel.transformAsync with react compiler plugin returns null");
38
+ }
39
+ const { code, map } = result;
40
+ callback(null, code ?? void 0, map ?? void 0);
41
+ } catch (e) {
42
+ callback(e);
43
+ }
44
+ }
45
+ export {
46
+ reactCompilerLoader as default
47
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unpackjs/plugin-react",
3
- "version": "1.6.0",
3
+ "version": "1.6.2",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "exports": {
@@ -23,15 +23,19 @@
23
23
  "access": "public"
24
24
  },
25
25
  "dependencies": {
26
+ "@babel/core": "7.26.0",
27
+ "@babel/plugin-proposal-decorators": "7.25.9",
28
+ "@babel/plugin-transform-class-properties": "7.25.9",
26
29
  "@pmmmwh/react-refresh-webpack-plugin": "0.5.15",
27
30
  "@rspack/plugin-react-refresh": "1.0.0",
28
- "react-refresh": "0.14.2",
29
- "swc-loader": "0.2.6",
30
- "@swc/core": "1.7.39",
31
+ "@swc/core": "1.7.40",
31
32
  "@swc/helpers": "0.5.13",
33
+ "babel-plugin-react-compiler": "19.0.0-beta-8a03594-20241020",
34
+ "html-webpack-plugin": "5.6.3",
32
35
  "launch-editor": "2.9.1",
33
- "html-webpack-plugin": "5.6.2",
34
- "@unpackjs/core": "^1.6.0"
36
+ "react-refresh": "0.14.2",
37
+ "swc-loader": "0.2.6",
38
+ "@unpackjs/core": "^1.6.2"
35
39
  },
36
40
  "scripts": {
37
41
  "dev": "modern build --watch",