vite-plugin-rnw 0.0.8-0 → 0.0.8

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.
Files changed (2) hide show
  1. package/dist/index.js +53 -20
  2. package/package.json +2 -1
package/dist/index.js CHANGED
@@ -3,13 +3,11 @@ import { createFilter } from "vite";
3
3
  import { esbuildFlowPlugin, flowPlugin } from "@bunchtogether/vite-plugin-flow";
4
4
  import commonjs from "vite-plugin-commonjs";
5
5
  import { makeIdFiltersToMatchWithQuery } from "@rolldown/pluginutils";
6
+ import MagicString from "magic-string";
6
7
  import react from "@vitejs/plugin-react";
7
8
 
8
9
  //#region src/transforms.ts
9
10
  /**
10
- * Code transformation utilities for fixing React Native package compatibility issues
11
- */
12
- /**
13
11
  * Analyzes require statements in code and maps them to module imports.
14
12
  * Handles patterns like: varName = require('path').prop or require('path').default
15
13
  */
@@ -30,11 +28,6 @@ function analyzeRequireStatements(originalCode, exportedVars) {
30
28
  * Removes try/catch blocks that contain require statements and export let declarations.
31
29
  * This cleans up the problematic CommonJS patterns.
32
30
  */
33
- function removeTryCatchRequireBlocks(code) {
34
- let transformed = code.replace(/try\s*\{[^{}]*?require\([^)]+\)[^{}]*?\}\s*catch[^{}]*?\{[^{}]*?\}/gs, "");
35
- transformed = transformed.replace(/export let \w+;/g, "");
36
- return transformed;
37
- }
38
31
  /**
39
32
  * Generates direct export statements from the import map.
40
33
  * Creates modern ESM exports like: export { default as varName } from 'module';
@@ -51,19 +44,56 @@ function generateDirectExports(importMap) {
51
44
  /**
52
45
  * Transforms React Native Reanimated webUtils files to fix problematic
53
46
  * export let + try/catch + require patterns by converting them to proper ESM.
54
- *
47
+ *
55
48
  * This transformation handles the incompatible module patterns in React Native Reanimated
56
49
  * that cause build errors in Vite due to mixing CommonJS require() with ESM exports.
57
50
  */
58
- function transformReanimatedWebUtils(toTransform, originalCode, id, isProduction) {
59
- if (!isProduction || !id.includes("node_modules/react-native-reanimated") || !id.includes("ReanimatedModule/js-reanimated/webUtils") || !originalCode.includes("export let") || !originalCode.includes("try") || !originalCode.includes("require")) return toTransform;
60
- const exportedVars = Array.from(originalCode.matchAll(/export let (\w+);/g)).map((match) => match[1]);
61
- if (exportedVars.length === 0) return toTransform;
62
- const importMap = analyzeRequireStatements(originalCode, exportedVars);
63
- toTransform = removeTryCatchRequireBlocks(toTransform);
51
+ function transformReanimatedWebUtilsWithMap(code, id, isProduction, opts) {
52
+ if (!isProduction || !id.includes("node_modules/react-native-reanimated") || !id.includes("ReanimatedModule/js-reanimated/webUtils") || !code.includes("export let") || !code.includes("try") || !code.includes("require")) return {
53
+ code,
54
+ map: null,
55
+ changed: false
56
+ };
57
+ const exportedVars = Array.from(code.matchAll(/export let (\w+);/g)).map((match) => match[1]);
58
+ if (exportedVars.length === 0) return {
59
+ code,
60
+ map: null,
61
+ changed: false
62
+ };
63
+ const importMap = analyzeRequireStatements(code, exportedVars);
64
+ const ms = new MagicString(code);
65
+ let changed = false;
66
+ for (const m of code.matchAll(/try\s*\{[^{}]*?require\([^)]+\)[^{}]*?\}\s*catch[^{}]*?\{[^{}]*?\}/gs)) {
67
+ const start = m.index;
68
+ const end = start + m[0].length;
69
+ ms.remove(start, end);
70
+ changed = true;
71
+ }
72
+ for (const m of code.matchAll(/export let \w+;/g)) {
73
+ const start = m.index;
74
+ const end = start + m[0].length;
75
+ ms.remove(start, end);
76
+ changed = true;
77
+ }
64
78
  const exports = generateDirectExports(importMap);
65
- if (exports) toTransform = `${toTransform}\n${exports}`;
66
- return toTransform;
79
+ if (exports) {
80
+ ms.append(`\n${exports}`);
81
+ changed = true;
82
+ }
83
+ if (!changed) return {
84
+ code,
85
+ map: null,
86
+ changed: false
87
+ };
88
+ return {
89
+ code: ms.toString(),
90
+ map: ms.generateMap({
91
+ source: (opts === null || opts === void 0 ? void 0 : opts.source) ?? id.split("?")[0],
92
+ includeContent: true,
93
+ hires: true
94
+ }),
95
+ changed: true
96
+ };
67
97
  }
68
98
 
69
99
  //#endregion
@@ -141,9 +171,12 @@ function rnw(opts = {}) {
141
171
  async handler(code, id, options) {
142
172
  const [filepath] = id.split("?");
143
173
  if (!filter(filepath)) return;
144
- let toTransform = code;
145
- toTransform = transformReanimatedWebUtils(toTransform, code, id, isProduction);
146
- return { code: toTransform };
174
+ const transformResult = transformReanimatedWebUtilsWithMap(code, id, isProduction, { source: filepath });
175
+ if (!transformResult.changed) return null;
176
+ return {
177
+ code: transformResult.code,
178
+ map: transformResult.map
179
+ };
147
180
  }
148
181
  }
149
182
  };
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.0.8-0",
2
+ "version": "0.0.8",
3
3
  "name": "vite-plugin-rnw",
4
4
  "type": "module",
5
5
  "author": "Daniel Williams",
@@ -48,6 +48,7 @@
48
48
  "@bunchtogether/vite-plugin-flow": "^1.0.2",
49
49
  "@rolldown/pluginutils": "1.0.0-beta.46",
50
50
  "@vitejs/plugin-react": "^5.1.0",
51
+ "magic-string": "^0.30.11",
51
52
  "vite-plugin-commonjs": "^0.10.4"
52
53
  }
53
54
  }