react-native-auto-positioned-popup 1.0.11 → 1.0.13

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
@@ -105,6 +105,100 @@ module.exports = {
105
105
  };
106
106
  ```
107
107
 
108
+ #### Dynamic Control via global.$fake (Recommended Approach)
109
+
110
+ A more advanced approach is to dynamically control source code loading through your project's global configuration file. This method allows runtime switching without modifying environment variables:
111
+
112
+ 1. Create or modify the `global.ts` file in your project root:
113
+
114
+ ```typescript
115
+ // global.ts
116
+ declare global {
117
+ var $fake: boolean;
118
+ // ... other global variables
119
+ }
120
+
121
+ // Set to true to load source code (development mode)
122
+ // Set to false to use compiled files (production mode)
123
+ global.$fake = true; // or false
124
+
125
+ export {};
126
+ ```
127
+
128
+ 2. Read the `global.$fake` value in your `babel.config.js`:
129
+
130
+ ```javascript
131
+ const fs = require('fs');
132
+ const path = require('path');
133
+
134
+ // Check global.$fake value
135
+ const checkFakeMode = () => {
136
+ try {
137
+ const globalPath = path.resolve(__dirname, 'global.ts');
138
+ const globalContent = fs.readFileSync(globalPath, 'utf8');
139
+ // Check if global.$fake is true
140
+ const fakeMatch = globalContent.match(/\$fake\s*=\s*(true|false)/);
141
+ return fakeMatch && fakeMatch[1] === 'true';
142
+ } catch (error) {
143
+ console.warn('Unable to read global.ts, defaulting to false:', error.message);
144
+ return false;
145
+ }
146
+ };
147
+
148
+ const isFakeMode = checkFakeMode();
149
+
150
+ // Base alias configuration
151
+ const baseAlias = {
152
+ // ... your base aliases
153
+ };
154
+
155
+ // If in fake mode, add source code redirects
156
+ const aliasConfig = isFakeMode ? {
157
+ ...baseAlias,
158
+ // Redirect react-native-auto-positioned-popup to source files
159
+ 'react-native-auto-positioned-popup': './node_modules/react-native-auto-positioned-popup/src',
160
+ 'react-native-auto-positioned-popup/lib/index': './node_modules/react-native-auto-positioned-popup/src/index.ts',
161
+ 'react-native-auto-positioned-popup/lib/AutoPositionedPopup': './node_modules/react-native-auto-positioned-popup/src/AutoPositionedPopup.tsx',
162
+ 'react-native-auto-positioned-popup/lib/AutoPositionedPopupProps': './node_modules/react-native-auto-positioned-popup/src/AutoPositionedPopupProps.ts',
163
+ 'react-native-auto-positioned-popup/lib/RootViewContext': './node_modules/react-native-auto-positioned-popup/src/RootViewContext.tsx',
164
+ 'react-native-auto-positioned-popup/lib/KeyboardManager': './node_modules/react-native-auto-positioned-popup/src/KeyboardManager.tsx',
165
+ 'react-native-auto-positioned-popup/lib/AutoPositionedPopup.style': './node_modules/react-native-auto-positioned-popup/src/AutoPositionedPopup.style.ts',
166
+ // If you also use react-native-advanced-flatlist
167
+ 'react-native-advanced-flatlist': './node_modules/react-native-advanced-flatlist/src',
168
+ 'react-native-advanced-flatlist/lib/index': './node_modules/react-native-advanced-flatlist/src/index.ts',
169
+ 'react-native-advanced-flatlist/lib/AdvancedFlatList': './node_modules/react-native-advanced-flatlist/src/AdvancedFlatList.tsx',
170
+ } : baseAlias;
171
+
172
+ console.log(`Babel Config - Fake Mode: ${isFakeMode ? 'ENABLED' : 'DISABLED'}`);
173
+ if (isFakeMode) {
174
+ console.log('✅ Using react-native-auto-positioned-popup SOURCE files (.tsx)');
175
+ } else {
176
+ console.log('📦 Using react-native-auto-positioned-popup COMPILED files (.js)');
177
+ }
178
+
179
+ module.exports = {
180
+ presets: ['module:@react-native/babel-preset'],
181
+ plugins: [
182
+ [
183
+ require.resolve('babel-plugin-module-resolver'),
184
+ {
185
+ root: ['.', './src'],
186
+ extensions: ['.js', '.jsx', '.ts', '.tsx', '.json'],
187
+ alias: aliasConfig,
188
+ },
189
+ ],
190
+ // ... other plugins
191
+ ],
192
+ };
193
+ ```
194
+
195
+ **Advantages of this approach**:
196
+ - ✅ No need to set environment variables, just modify the `global.ts` file
197
+ - ✅ Can switch dynamically at runtime (restart Metro after modifying the file)
198
+ - ✅ More intuitive configuration with all settings centralized in one file
199
+ - ✅ Suitable for team collaboration, different developers can have different local configs
200
+ - ✅ Avoids environment variable compatibility issues across different operating systems
201
+
108
202
  ### TypeScript Configuration
109
203
 
110
204
  When loading source files directly, ensure your `tsconfig.json` includes the necessary paths:
package/README_zh.md CHANGED
@@ -105,6 +105,100 @@ module.exports = {
105
105
  };
106
106
  ```
107
107
 
108
+ #### 通过 global.$fake 动态控制(推荐方式)
109
+
110
+ 更高级的用法是通过项目的全局配置文件动态控制是否加载源码。这种方式允许在运行时切换而无需修改环境变量:
111
+
112
+ 1. 在项目根目录创建或修改 `global.ts` 文件:
113
+
114
+ ```typescript
115
+ // global.ts
116
+ declare global {
117
+ var $fake: boolean;
118
+ // ... 其他全局变量
119
+ }
120
+
121
+ // 设置为 true 时加载源码(开发模式)
122
+ // 设置为 false 时使用编译文件(生产模式)
123
+ global.$fake = true; // 或 false
124
+
125
+ export {};
126
+ ```
127
+
128
+ 2. 在 `babel.config.js` 中读取 `global.$fake` 的值:
129
+
130
+ ```javascript
131
+ const fs = require('fs');
132
+ const path = require('path');
133
+
134
+ // 检查 global.$fake 的值
135
+ const checkFakeMode = () => {
136
+ try {
137
+ const globalPath = path.resolve(__dirname, 'global.ts');
138
+ const globalContent = fs.readFileSync(globalPath, 'utf8');
139
+ // 检查 global.$fake 是否为 true
140
+ const fakeMatch = globalContent.match(/\$fake\s*=\s*(true|false)/);
141
+ return fakeMatch && fakeMatch[1] === 'true';
142
+ } catch (error) {
143
+ console.warn('Unable to read global.ts, defaulting to false:', error.message);
144
+ return false;
145
+ }
146
+ };
147
+
148
+ const isFakeMode = checkFakeMode();
149
+
150
+ // 基础别名配置
151
+ const baseAlias = {
152
+ // ... 你的基础别名
153
+ };
154
+
155
+ // 如果是 fake 模式,添加源码重定向
156
+ const aliasConfig = isFakeMode ? {
157
+ ...baseAlias,
158
+ // 重定向 react-native-auto-positioned-popup 到源码文件
159
+ 'react-native-auto-positioned-popup': './node_modules/react-native-auto-positioned-popup/src',
160
+ 'react-native-auto-positioned-popup/lib/index': './node_modules/react-native-auto-positioned-popup/src/index.ts',
161
+ 'react-native-auto-positioned-popup/lib/AutoPositionedPopup': './node_modules/react-native-auto-positioned-popup/src/AutoPositionedPopup.tsx',
162
+ 'react-native-auto-positioned-popup/lib/AutoPositionedPopupProps': './node_modules/react-native-auto-positioned-popup/src/AutoPositionedPopupProps.ts',
163
+ 'react-native-auto-positioned-popup/lib/RootViewContext': './node_modules/react-native-auto-positioned-popup/src/RootViewContext.tsx',
164
+ 'react-native-auto-positioned-popup/lib/KeyboardManager': './node_modules/react-native-auto-positioned-popup/src/KeyboardManager.tsx',
165
+ 'react-native-auto-positioned-popup/lib/AutoPositionedPopup.style': './node_modules/react-native-auto-positioned-popup/src/AutoPositionedPopup.style.ts',
166
+ // 如果你也使用 react-native-advanced-flatlist
167
+ 'react-native-advanced-flatlist': './node_modules/react-native-advanced-flatlist/src',
168
+ 'react-native-advanced-flatlist/lib/index': './node_modules/react-native-advanced-flatlist/src/index.ts',
169
+ 'react-native-advanced-flatlist/lib/AdvancedFlatList': './node_modules/react-native-advanced-flatlist/src/AdvancedFlatList.tsx',
170
+ } : baseAlias;
171
+
172
+ console.log(`Babel Config - Fake Mode: ${isFakeMode ? 'ENABLED' : 'DISABLED'}`);
173
+ if (isFakeMode) {
174
+ console.log('✅ Using react-native-auto-positioned-popup SOURCE files (.tsx)');
175
+ } else {
176
+ console.log('📦 Using react-native-auto-positioned-popup COMPILED files (.js)');
177
+ }
178
+
179
+ module.exports = {
180
+ presets: ['module:@react-native/babel-preset'],
181
+ plugins: [
182
+ [
183
+ require.resolve('babel-plugin-module-resolver'),
184
+ {
185
+ root: ['.', './src'],
186
+ extensions: ['.js', '.jsx', '.ts', '.tsx', '.json'],
187
+ alias: aliasConfig,
188
+ },
189
+ ],
190
+ // ... 其他插件
191
+ ],
192
+ };
193
+ ```
194
+
195
+ **这种方式的优势**:
196
+ - ✅ 无需设置环境变量,只需修改 `global.ts` 文件
197
+ - ✅ 可以在运行时动态切换(修改文件后重启 Metro)
198
+ - ✅ 更直观的配置方式,所有配置集中在一个文件
199
+ - ✅ 适合团队协作,不同开发者可以有不同的本地配置
200
+ - ✅ 避免了环境变量在不同操作系统的兼容性问题
201
+
108
202
  ### TypeScript 配置
109
203
 
110
204
  当直接加载源文件时,确保你的 `tsconfig.json` 包含必要的路径:
@@ -1 +1 @@
1
- {"version":3,"file":"AutoPositionedPopup.d.ts","sourceRoot":"","sources":["../src/AutoPositionedPopup.tsx"],"names":[],"mappings":"AAAA,OAAO,KAYN,MAAM,OAAO,CAAC;AAcf,OAAO,EAAC,wBAAwB,EAAqB,MAAM,4BAA4B,CAAC;AAwRxF,QAAA,MAAM,mBAAmB,qFA2vBxB,CAAC;AAEF,eAAe,mBAAmB,CAAC"}
1
+ {"version":3,"file":"AutoPositionedPopup.d.ts","sourceRoot":"","sources":["../src/AutoPositionedPopup.tsx"],"names":[],"mappings":"AAAA,OAAO,KAYN,MAAM,OAAO,CAAC;AAcf,OAAO,EAAC,wBAAwB,EAAqB,MAAM,4BAA4B,CAAC;AAuRxF,QAAA,MAAM,mBAAmB,qFA+4BxB,CAAC;AAEF,eAAe,mBAAmB,CAAC"}