@kbach/react-native 1.0.0-beta.31 → 1.0.0-beta.33

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/package.json +1 -1
  2. package/src/babel-plugin.js +95 -25
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kbach/react-native",
3
- "version": "1.0.0-beta.31",
3
+ "version": "1.0.0-beta.33",
4
4
  "description": "Kbach's utility-class styling engine for React Native — the same class names as @kbach/react, resolved to a StyleSheet-ready style object via a Kotlin/JNI TurboModule (native), the same WASM engine @kbach/react uses (Expo Web / react-native-web), or a pure-JS fallback engine (Expo Go). Ships its own native Android module — one npm install, no separate native package. Beta. Native is Android-only (no iOS bridge yet); works with the React Native CLI, an Expo dev client, AND Expo Go (a reduced-coverage pure-JS fallback, since Expo Go can't load any custom native module).",
5
5
  "keywords": [
6
6
  "css",
@@ -25,7 +25,7 @@ const template = require('@babel/template').default;
25
25
  * synchronous) native-bridge call inside the jsx-runtime instead (see
26
26
  * nativeBridge.ts), so this plugin's only job is the pragma injection —
27
27
  * PLUS one more, added later: auto-applying `kbach.config.js` if one
28
- * exists (see `getConfigStatement`'s own doc comment), so a project never
28
+ * exists (see `buildConfigStatement`'s own doc comment), so a project never
29
29
  * needs its own explicit `applyKbachConfig(kbachConfig)` call at all. That
30
30
  * addition deliberately still stops short of old-kbach's own version of
31
31
  * this (an mtime-polling live-reload of the config, re-checked on every
@@ -37,18 +37,59 @@ const template = require('@babel/template').default;
37
37
  // Keyed by the root directory checked — undefined = not checked yet for
38
38
  // that root, null = checked, no kbach.config.js found there. A Babel/Metro
39
39
  // worker process transforms many files over its lifetime — this makes the
40
- // existsSync + template-build work happen ONCE per root, not on every
41
- // single file `pre()` runs for.
42
- const _configStatementCache = new Map();
40
+ // existsSync check happen ONCE per root, not on every single file `pre()`
41
+ // runs for. Caches only the resolved PATH (or null) — never the built AST
42
+ // statement itself; see `buildConfigStatement`'s own doc comment for why
43
+ // that distinction is load-bearing, not a style preference.
44
+ const _configPathCache = new Map();
45
+
46
+ function resolveConfigPath(root) {
47
+ if (_configPathCache.has(root)) return _configPathCache.get(root);
48
+ const configPath = path.join(root, 'kbach.config.js');
49
+ const resolved = fs.existsSync(configPath) ? configPath : null;
50
+ _configPathCache.set(root, resolved);
51
+ return resolved;
52
+ }
53
+
54
+ // Windows' filesystem is case-insensitive but case-preserving, and Metro's
55
+ // own `file.opts.filename` isn't guaranteed to share the exact casing/
56
+ // separator normalization `path.join` produces — comparing case-sensitively
57
+ // risked a false NEGATIVE (kbach.config.js not recognized as itself,
58
+ // re-introducing the self-require bug `buildConfigStatement`'s doc comment
59
+ // describes) far more than a false positive risks colliding two genuinely
60
+ // different files, which would require two paths differing ONLY by case in
61
+ // the same project — effectively never happens in practice.
62
+ function samePath(a, b) {
63
+ return !!a && !!b && path.resolve(a).toLowerCase() === path.resolve(b).toLowerCase();
64
+ }
43
65
 
44
66
  /**
45
- * Builds the (uncompiled) AST statement that applies `kbach.config.js`,
46
- * cached per `root` after the first call — `null` if no such file exists
47
- * there. `root` defaults to `process.cwd()` (how `metro start`/`expo
48
- * start` are conventionally invoked) at the plugin's own call site below;
49
- * pass `{ configRoot: '...' }` as this plugin's Babel options if your
50
- * `kbach.config.js` lives somewhere else (a monorepo where Metro's actual
51
- * working directory isn't the app's own root, for instance).
67
+ * Builds a FRESH (uncompiled) AST statement that applies `kbach.config.js`
68
+ * — a NEW node on every single call, deliberately never cached or reused
69
+ * across files. `root` defaults to `process.cwd()` (how `metro start`/
70
+ * `expo start` are conventionally invoked) at the plugin's own call site
71
+ * below; pass `{ configRoot: '...' }` as this plugin's Babel options if
72
+ * your `kbach.config.js` lives somewhere else (a monorepo where Metro's
73
+ * actual working directory isn't the app's own root, for instance).
74
+ *
75
+ * `null` if no such file exists at `root` (checked via the cached
76
+ * `resolveConfigPath` above — `fs.existsSync` itself is cheap enough to
77
+ * memoize per root without any of the risk rebuilding the AST fresh here
78
+ * guards against).
79
+ *
80
+ * An EARLIER version of this function built the statement once per root
81
+ * and cached/reused that SAME AST node object across every file's `pre()`
82
+ * call. That's unsafe with Babel: a node spliced into one file's AST gets
83
+ * mutated in place as THAT file's own transform pipeline runs (location
84
+ * info, scope bindings, hoisting markers, ...) — sharing one mutable node
85
+ * instance across many independently-processed file ASTs corrupts it after
86
+ * the first file. Confirmed as the actual cause of a real bundling failure
87
+ * (not a hypothetical): Metro's own dependency collector needs a require()
88
+ * call's location info to validate/instrument it, and once the shared
89
+ * node's location got corrupted by an earlier file's pass, Metro rejected
90
+ * a LATER file's copy with "Invalid call at line <unknown>". Rebuilding
91
+ * fresh per call fixes this at negligible cost — `@babel/template`'s own
92
+ * parse is cheap, and this runs once per file Metro transforms regardless.
52
93
  *
53
94
  * The injected statement is a plain `require(...).applyKbachConfig(require(...))`
54
95
  * — no try/catch around it. `kbach.config.js`'s own path is only ever
@@ -64,21 +105,14 @@ const _configStatementCache = new Map();
64
105
  * all) — simpler than building the equivalent `@babel/types` node graph by
65
106
  * hand for one straightforward statement.
66
107
  */
67
- function getConfigStatement(root) {
68
- if (_configStatementCache.has(root)) return _configStatementCache.get(root);
69
-
70
- const configPath = path.join(root, 'kbach.config.js');
71
- if (!fs.existsSync(configPath)) {
72
- _configStatementCache.set(root, null);
73
- return null;
74
- }
108
+ function buildConfigStatement(root) {
109
+ const configPath = resolveConfigPath(root);
110
+ if (configPath === null) return null;
75
111
 
76
112
  const buildStatement = template(
77
113
  `require('@kbach/react-native').applyKbachConfig(require(${JSON.stringify(configPath)}));`,
78
114
  );
79
- const statement = buildStatement();
80
- _configStatementCache.set(root, statement);
81
- return statement;
115
+ return buildStatement();
82
116
  }
83
117
 
84
118
  module.exports = function kbachReactNativeBabelPlugin(_babel, options) {
@@ -90,13 +124,49 @@ module.exports = function kbachReactNativeBabelPlugin(_babel, options) {
90
124
  const filename = file.opts && file.opts.filename;
91
125
  if (filename && filename.indexOf('node_modules') !== -1) return;
92
126
 
127
+ // Metro/Expo's own internal polyfills (the Node.js-external-require
128
+ // shim, the assets-registry shim, node:/weak-ref virtualizations, ...)
129
+ // are synthetic "virtual modules" with no real file on disk. Expo's
130
+ // own Metro config marks every one of these with an embedded NUL
131
+ // control character in its module ID (Rollup's/Metro's own
132
+ // convention for "this isn't a real file"). These never sit inside
133
+ // node_modules, so the check above doesn't catch them — and Metro
134
+ // places them directly in the bundle PRELUDE, entirely outside any
135
+ // wrapped module, where `require` isn't defined as a real function
136
+ // at all. Confirmed as a real, reproducible runtime crash (not a
137
+ // hypothetical): injecting this plugin's applyKbachConfig() require()
138
+ // call into that exact polyfill produced Expo's own "[runtime not
139
+ // ready]: ReferenceError: Property 'require' doesn't exist" at app
140
+ // startup, verified by exporting a real dev bundle and finding the
141
+ // offending, un-instrumented require() call sitting inside
142
+ // withMetroMultiPlatform.js's own external-require polyfill — a file
143
+ // this plugin was never meant to touch at all.
144
+ //
145
+ // The control character is NOT at the start of `filename`, so
146
+ // `startsWith`/`charCodeAt(0)` (what an earlier version of this
147
+ // check used) never matches — confirmed by actually logging Babel's
148
+ // real `filename` for this file, not assumed: Metro joins the
149
+ // project root onto the raw virtual module ID first, so the control
150
+ // character ends up in the MIDDLE of the full string, right after
151
+ // the trailing path separator, not at index 0. A search anywhere in
152
+ // the string (rather than just its start) is what actually catches
153
+ // it — see this file's own test suite for the exact filename shape
154
+ // this was verified against.
155
+ if (filename && filename.indexOf(String.fromCharCode(0)) !== -1) return;
156
+
93
157
  const comments = file.ast.comments || (file.ast.comments = []);
94
158
  const alreadySet = comments.some((c) => /@jsxImportSource|@jsxRuntime/.test(c.value));
95
159
  if (!alreadySet) {
96
160
  comments.unshift({ type: 'CommentLine', value: ' @jsxImportSource @kbach/react-native' });
97
161
  }
98
162
 
99
- const configStatement = getConfigStatement(configRoot);
163
+ // Never inject the auto-apply statement into kbach.config.js itself —
164
+ // it would require() its own absolute path, a real self-reference
165
+ // cycle in Metro's dependency graph for a statement that makes no
166
+ // sense there anyway (a config file applying itself to itself).
167
+ if (samePath(filename, resolveConfigPath(configRoot))) return;
168
+
169
+ const configStatement = buildConfigStatement(configRoot);
100
170
  if (configStatement) {
101
171
  file.ast.program.body.unshift(configStatement);
102
172
  }
@@ -105,7 +175,7 @@ module.exports = function kbachReactNativeBabelPlugin(_babel, options) {
105
175
  };
106
176
  };
107
177
 
108
- /** Exported for tests only — clears the per-root config-file cache between test cases. */
178
+ /** Exported for tests only — clears the per-root config-path cache between test cases. */
109
179
  module.exports._resetForTests = function resetForTests() {
110
- _configStatementCache.clear();
180
+ _configPathCache.clear();
111
181
  };