@vxrn/vite-plugin-metro 1.27.1 → 2.0.0-beta.45.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/dist/cjs/babel-plugins/import-meta-env-plugin.cjs +84 -102
- package/dist/cjs/babel-plugins/import-meta-env-plugin.native.cjs +108 -0
- package/dist/cjs/babel-plugins/import-meta-env-plugin.native.js +90 -107
- package/dist/cjs/babel-plugins/import-meta-env-plugin.native.js.map +1 -1
- package/dist/cjs/env/platformEnv.cjs +55 -57
- package/dist/cjs/env/platformEnv.native.cjs +93 -0
- package/dist/cjs/env/platformEnv.native.js +72 -75
- package/dist/cjs/env/platformEnv.native.js.map +1 -1
- package/dist/cjs/index.cjs +22 -24
- package/dist/cjs/index.native.cjs +41 -0
- package/dist/cjs/index.native.js +22 -23
- package/dist/cjs/index.native.js.map +1 -1
- package/dist/cjs/metro-config/getMetroBabelConfigFromViteConfig.cjs +48 -51
- package/dist/cjs/metro-config/getMetroBabelConfigFromViteConfig.native.cjs +101 -0
- package/dist/cjs/metro-config/getMetroBabelConfigFromViteConfig.native.js +88 -94
- package/dist/cjs/metro-config/getMetroBabelConfigFromViteConfig.native.js.map +1 -1
- package/dist/cjs/metro-config/getMetroConfigFromViteConfig.cjs +246 -271
- package/dist/cjs/metro-config/getMetroConfigFromViteConfig.native.cjs +295 -0
- package/dist/cjs/metro-config/getMetroConfigFromViteConfig.native.js +257 -281
- package/dist/cjs/metro-config/getMetroConfigFromViteConfig.native.js.map +1 -1
- package/dist/cjs/metro-config/patchMetroServerWithViteConfigAndMetroPluginOptions.cjs +38 -44
- package/dist/cjs/metro-config/patchMetroServerWithViteConfigAndMetroPluginOptions.native.cjs +53 -0
- package/dist/cjs/metro-config/patchMetroServerWithViteConfigAndMetroPluginOptions.native.js +39 -44
- package/dist/cjs/metro-config/patchMetroServerWithViteConfigAndMetroPluginOptions.native.js.map +1 -1
- package/dist/cjs/metro-config/types.cjs +9 -11
- package/dist/cjs/metro-config/types.native.cjs +18 -0
- package/dist/cjs/metro-config/types.native.js +9 -10
- package/dist/cjs/metro-config/types.native.js.map +1 -1
- package/dist/cjs/plugins/expoManifestRequestHandlerPlugin.cjs +44 -56
- package/dist/cjs/plugins/expoManifestRequestHandlerPlugin.native.cjs +62 -0
- package/dist/cjs/plugins/expoManifestRequestHandlerPlugin.native.js +45 -63
- package/dist/cjs/plugins/expoManifestRequestHandlerPlugin.native.js.map +1 -1
- package/dist/cjs/plugins/metroPlugin.cjs +210 -248
- package/dist/cjs/plugins/metroPlugin.native.cjs +242 -0
- package/dist/cjs/plugins/metroPlugin.native.js +216 -253
- package/dist/cjs/plugins/metroPlugin.native.js.map +1 -1
- package/dist/cjs/rn-commands/bundle/assetCatalogIOS.cjs +56 -65
- package/dist/cjs/rn-commands/bundle/assetCatalogIOS.native.cjs +121 -0
- package/dist/cjs/rn-commands/bundle/assetCatalogIOS.native.js +94 -106
- package/dist/cjs/rn-commands/bundle/assetCatalogIOS.native.js.map +1 -1
- package/dist/cjs/rn-commands/bundle/assetPathUtils.cjs +51 -55
- package/dist/cjs/rn-commands/bundle/assetPathUtils.native.cjs +72 -0
- package/dist/cjs/rn-commands/bundle/assetPathUtils.native.js +51 -54
- package/dist/cjs/rn-commands/bundle/assetPathUtils.native.js.map +1 -1
- package/dist/cjs/rn-commands/bundle/buildBundle.cjs +37 -46
- package/dist/cjs/rn-commands/bundle/buildBundle.native.cjs +57 -0
- package/dist/cjs/rn-commands/bundle/buildBundle.native.js +39 -47
- package/dist/cjs/rn-commands/bundle/buildBundle.native.js.map +1 -1
- package/dist/cjs/rn-commands/bundle/buildBundleWithConfig.cjs +57 -68
- package/dist/cjs/rn-commands/bundle/buildBundleWithConfig.native.cjs +81 -0
- package/dist/cjs/rn-commands/bundle/buildBundleWithConfig.native.js +59 -70
- package/dist/cjs/rn-commands/bundle/buildBundleWithConfig.native.js.map +1 -1
- package/dist/cjs/rn-commands/bundle/filterPlatformAssetScales.cjs +37 -39
- package/dist/cjs/rn-commands/bundle/filterPlatformAssetScales.native.cjs +70 -0
- package/dist/cjs/rn-commands/bundle/filterPlatformAssetScales.native.js +56 -59
- package/dist/cjs/rn-commands/bundle/filterPlatformAssetScales.native.js.map +1 -1
- package/dist/cjs/rn-commands/bundle/getAssetDestPathAndroid.cjs +20 -29
- package/dist/cjs/rn-commands/bundle/getAssetDestPathAndroid.native.cjs +39 -0
- package/dist/cjs/rn-commands/bundle/getAssetDestPathAndroid.native.js +20 -28
- package/dist/cjs/rn-commands/bundle/getAssetDestPathAndroid.native.js.map +1 -1
- package/dist/cjs/rn-commands/bundle/getAssetDestPathIOS.cjs +20 -33
- package/dist/cjs/rn-commands/bundle/getAssetDestPathIOS.native.cjs +38 -0
- package/dist/cjs/rn-commands/bundle/getAssetDestPathIOS.native.js +20 -32
- package/dist/cjs/rn-commands/bundle/getAssetDestPathIOS.native.js.map +1 -1
- package/dist/cjs/rn-commands/bundle/getBuildBundleFn.cjs +23 -32
- package/dist/cjs/rn-commands/bundle/getBuildBundleFn.native.cjs +38 -0
- package/dist/cjs/rn-commands/bundle/getBuildBundleFn.native.js +23 -31
- package/dist/cjs/rn-commands/bundle/getBuildBundleFn.native.js.map +1 -1
- package/dist/cjs/rn-commands/bundle/index.cjs +15 -17
- package/dist/cjs/rn-commands/bundle/index.native.cjs +30 -0
- package/dist/cjs/rn-commands/bundle/index.native.js +15 -16
- package/dist/cjs/rn-commands/bundle/index.native.js.map +1 -1
- package/dist/cjs/rn-commands/bundle/saveAssets.cjs +85 -96
- package/dist/cjs/rn-commands/bundle/saveAssets.native.cjs +129 -0
- package/dist/cjs/rn-commands/bundle/saveAssets.native.js +102 -114
- package/dist/cjs/rn-commands/bundle/saveAssets.native.js.map +1 -1
- package/dist/cjs/rn-commands/bundle/types.cjs +9 -11
- package/dist/cjs/rn-commands/bundle/types.native.cjs +18 -0
- package/dist/cjs/rn-commands/bundle/types.native.js +9 -10
- package/dist/cjs/rn-commands/bundle/types.native.js.map +1 -1
- package/dist/cjs/rn-commands/index.cjs +17 -26
- package/dist/cjs/rn-commands/index.native.cjs +32 -0
- package/dist/cjs/rn-commands/index.native.js +17 -25
- package/dist/cjs/rn-commands/index.native.js.map +1 -1
- package/dist/cjs/transformer/babel-core.cjs +16 -18
- package/dist/cjs/transformer/babel-core.native.cjs +30 -0
- package/dist/cjs/transformer/babel-core.native.js +16 -17
- package/dist/cjs/transformer/babel-core.native.js.map +1 -1
- package/dist/cjs/transformer/babel-transformer.cjs +101 -152
- package/dist/cjs/transformer/babel-transformer.native.cjs +131 -0
- package/dist/cjs/transformer/babel-transformer.native.js +106 -159
- package/dist/cjs/transformer/babel-transformer.native.js.map +1 -1
- package/dist/cjs/transformer/loadBabelConfig.cjs +44 -49
- package/dist/cjs/transformer/loadBabelConfig.native.cjs +64 -0
- package/dist/cjs/transformer/loadBabelConfig.native.js +48 -52
- package/dist/cjs/transformer/loadBabelConfig.native.js.map +1 -1
- package/dist/cjs/transformer/metroNativeWorker.cjs +1442 -1480
- package/dist/cjs/transformer/metroNativeWorker.native.cjs +2536 -0
- package/dist/cjs/transformer/metroNativeWorker.native.js +2416 -2552
- package/dist/cjs/transformer/metroNativeWorker.native.js.map +1 -1
- package/dist/cjs/transformer/transformSync.cjs +47 -63
- package/dist/cjs/transformer/transformSync.native.cjs +74 -0
- package/dist/cjs/transformer/transformSync.native.js +49 -64
- package/dist/cjs/transformer/transformSync.native.js.map +1 -1
- package/dist/cjs/transformer/types.cjs +9 -11
- package/dist/cjs/transformer/types.native.cjs +18 -0
- package/dist/cjs/transformer/types.native.js +9 -10
- package/dist/cjs/transformer/types.native.js.map +1 -1
- package/dist/cjs/utils/exit.cjs +141 -139
- package/dist/cjs/utils/exit.native.cjs +266 -0
- package/dist/cjs/utils/exit.native.js +228 -241
- package/dist/cjs/utils/exit.native.js.map +1 -1
- package/dist/cjs/utils/getTerminalReporter.cjs +35 -47
- package/dist/cjs/utils/getTerminalReporter.native.cjs +57 -0
- package/dist/cjs/utils/getTerminalReporter.native.js +38 -49
- package/dist/cjs/utils/getTerminalReporter.native.js.map +1 -1
- package/dist/cjs/utils/metroCacheManager.cjs +84 -99
- package/dist/cjs/utils/metroCacheManager.native.cjs +130 -0
- package/dist/cjs/utils/metroCacheManager.native.js +102 -118
- package/dist/cjs/utils/metroCacheManager.native.js.map +1 -1
- package/dist/cjs/utils/patchExpoGoManifestHandlerMiddlewareWithCustomMainModuleName.cjs +24 -28
- package/dist/cjs/utils/patchExpoGoManifestHandlerMiddlewareWithCustomMainModuleName.native.cjs +37 -0
- package/dist/cjs/utils/patchExpoGoManifestHandlerMiddlewareWithCustomMainModuleName.native.js +24 -27
- package/dist/cjs/utils/patchExpoGoManifestHandlerMiddlewareWithCustomMainModuleName.native.js.map +1 -1
- package/dist/cjs/utils/projectImport.cjs +40 -51
- package/dist/cjs/utils/projectImport.native.cjs +64 -0
- package/dist/cjs/utils/projectImport.native.js +41 -58
- package/dist/cjs/utils/projectImport.native.js.map +1 -1
- package/dist/esm/babel-plugins/import-meta-env-plugin.mjs +72 -86
- package/dist/esm/babel-plugins/import-meta-env-plugin.mjs.map +1 -1
- package/dist/esm/babel-plugins/import-meta-env-plugin.native.js +78 -92
- package/dist/esm/babel-plugins/import-meta-env-plugin.native.js.map +1 -1
- package/dist/esm/env/platformEnv.mjs +40 -39
- package/dist/esm/env/platformEnv.mjs.map +1 -1
- package/dist/esm/env/platformEnv.native.js +57 -58
- package/dist/esm/env/platformEnv.native.js.map +1 -1
- package/dist/esm/index.js +3 -3
- package/dist/esm/index.mjs +3 -3
- package/dist/esm/index.native.js +3 -3
- package/dist/esm/metro-config/getMetroBabelConfigFromViteConfig.mjs +35 -33
- package/dist/esm/metro-config/getMetroBabelConfigFromViteConfig.mjs.map +1 -1
- package/dist/esm/metro-config/getMetroBabelConfigFromViteConfig.native.js +75 -77
- package/dist/esm/metro-config/getMetroBabelConfigFromViteConfig.native.js.map +1 -1
- package/dist/esm/metro-config/getMetroConfigFromViteConfig.mjs +230 -246
- package/dist/esm/metro-config/getMetroConfigFromViteConfig.mjs.map +1 -1
- package/dist/esm/metro-config/getMetroConfigFromViteConfig.native.js +241 -257
- package/dist/esm/metro-config/getMetroConfigFromViteConfig.native.js.map +1 -1
- package/dist/esm/metro-config/patchMetroServerWithViteConfigAndMetroPluginOptions.mjs +26 -26
- package/dist/esm/metro-config/patchMetroServerWithViteConfigAndMetroPluginOptions.mjs.map +1 -1
- package/dist/esm/metro-config/patchMetroServerWithViteConfigAndMetroPluginOptions.native.js +27 -27
- package/dist/esm/metro-config/patchMetroServerWithViteConfigAndMetroPluginOptions.native.js.map +1 -1
- package/dist/esm/metro-config/types.mjs +0 -2
- package/dist/esm/metro-config/types.native.js +0 -2
- package/dist/esm/plugins/expoManifestRequestHandlerPlugin.mjs +32 -38
- package/dist/esm/plugins/expoManifestRequestHandlerPlugin.mjs.map +1 -1
- package/dist/esm/plugins/expoManifestRequestHandlerPlugin.native.js +33 -46
- package/dist/esm/plugins/expoManifestRequestHandlerPlugin.native.js.map +1 -1
- package/dist/esm/plugins/metroPlugin.mjs +195 -222
- package/dist/esm/plugins/metroPlugin.mjs.map +1 -1
- package/dist/esm/plugins/metroPlugin.native.js +201 -228
- package/dist/esm/plugins/metroPlugin.native.js.map +1 -1
- package/dist/esm/rn-commands/bundle/assetCatalogIOS.mjs +38 -38
- package/dist/esm/rn-commands/bundle/assetCatalogIOS.mjs.map +1 -1
- package/dist/esm/rn-commands/bundle/assetCatalogIOS.native.js +76 -80
- package/dist/esm/rn-commands/bundle/assetCatalogIOS.native.js.map +1 -1
- package/dist/esm/rn-commands/bundle/assetPathUtils.mjs +38 -37
- package/dist/esm/rn-commands/bundle/assetPathUtils.mjs.map +1 -1
- package/dist/esm/rn-commands/bundle/assetPathUtils.native.js +38 -37
- package/dist/esm/rn-commands/bundle/assetPathUtils.native.js.map +1 -1
- package/dist/esm/rn-commands/bundle/buildBundle.mjs +25 -28
- package/dist/esm/rn-commands/bundle/buildBundle.mjs.map +1 -1
- package/dist/esm/rn-commands/bundle/buildBundle.native.js +27 -30
- package/dist/esm/rn-commands/bundle/buildBundle.native.js.map +1 -1
- package/dist/esm/rn-commands/bundle/buildBundleWithConfig.mjs +43 -43
- package/dist/esm/rn-commands/bundle/buildBundleWithConfig.mjs.map +1 -1
- package/dist/esm/rn-commands/bundle/buildBundleWithConfig.native.js +45 -46
- package/dist/esm/rn-commands/bundle/buildBundleWithConfig.native.js.map +1 -1
- package/dist/esm/rn-commands/bundle/filterPlatformAssetScales.mjs +24 -21
- package/dist/esm/rn-commands/bundle/filterPlatformAssetScales.mjs.map +1 -1
- package/dist/esm/rn-commands/bundle/filterPlatformAssetScales.native.js +43 -42
- package/dist/esm/rn-commands/bundle/filterPlatformAssetScales.native.js.map +1 -1
- package/dist/esm/rn-commands/bundle/getAssetDestPathAndroid.mjs +5 -3
- package/dist/esm/rn-commands/bundle/getAssetDestPathAndroid.mjs.map +1 -1
- package/dist/esm/rn-commands/bundle/getAssetDestPathAndroid.native.js +5 -3
- package/dist/esm/rn-commands/bundle/getAssetDestPathAndroid.native.js.map +1 -1
- package/dist/esm/rn-commands/bundle/getAssetDestPathIOS.mjs +5 -7
- package/dist/esm/rn-commands/bundle/getAssetDestPathIOS.mjs.map +1 -1
- package/dist/esm/rn-commands/bundle/getAssetDestPathIOS.native.js +5 -7
- package/dist/esm/rn-commands/bundle/getAssetDestPathIOS.native.js.map +1 -1
- package/dist/esm/rn-commands/bundle/getBuildBundleFn.mjs +11 -14
- package/dist/esm/rn-commands/bundle/getBuildBundleFn.mjs.map +1 -1
- package/dist/esm/rn-commands/bundle/getBuildBundleFn.native.js +11 -14
- package/dist/esm/rn-commands/bundle/getBuildBundleFn.native.js.map +1 -1
- package/dist/esm/rn-commands/bundle/index.mjs +2 -2
- package/dist/esm/rn-commands/bundle/index.native.js +2 -2
- package/dist/esm/rn-commands/bundle/saveAssets.mjs +70 -70
- package/dist/esm/rn-commands/bundle/saveAssets.mjs.map +1 -1
- package/dist/esm/rn-commands/bundle/saveAssets.native.js +87 -89
- package/dist/esm/rn-commands/bundle/saveAssets.native.js.map +1 -1
- package/dist/esm/rn-commands/bundle/types.mjs +0 -2
- package/dist/esm/rn-commands/bundle/types.native.js +0 -2
- package/dist/esm/rn-commands/index.mjs +2 -2
- package/dist/esm/rn-commands/index.native.js +2 -2
- package/dist/esm/transformer/babel-core.mjs +2 -2
- package/dist/esm/transformer/babel-core.native.js +2 -2
- package/dist/esm/transformer/babel-transformer.mjs +108 -144
- package/dist/esm/transformer/babel-transformer.mjs.map +1 -1
- package/dist/esm/transformer/babel-transformer.native.js +114 -155
- package/dist/esm/transformer/babel-transformer.native.js.map +1 -1
- package/dist/esm/transformer/loadBabelConfig.mjs +29 -23
- package/dist/esm/transformer/loadBabelConfig.mjs.map +1 -1
- package/dist/esm/transformer/loadBabelConfig.native.js +33 -27
- package/dist/esm/transformer/loadBabelConfig.native.js.map +1 -1
- package/dist/esm/transformer/metroNativeWorker.mjs +1405 -1434
- package/dist/esm/transformer/metroNativeWorker.mjs.map +1 -1
- package/dist/esm/transformer/metroNativeWorker.native.js +2379 -2507
- package/dist/esm/transformer/metroNativeWorker.native.js.map +1 -1
- package/dist/esm/transformer/transformSync.mjs +32 -37
- package/dist/esm/transformer/transformSync.mjs.map +1 -1
- package/dist/esm/transformer/transformSync.native.js +34 -39
- package/dist/esm/transformer/transformSync.native.js.map +1 -1
- package/dist/esm/transformer/types.mjs +0 -2
- package/dist/esm/transformer/types.native.js +0 -2
- package/dist/esm/utils/exit.mjs +123 -112
- package/dist/esm/utils/exit.mjs.map +1 -1
- package/dist/esm/utils/exit.native.js +210 -215
- package/dist/esm/utils/exit.native.js.map +1 -1
- package/dist/esm/utils/getTerminalReporter.mjs +20 -21
- package/dist/esm/utils/getTerminalReporter.mjs.map +1 -1
- package/dist/esm/utils/getTerminalReporter.native.js +23 -24
- package/dist/esm/utils/getTerminalReporter.native.js.map +1 -1
- package/dist/esm/utils/metroCacheManager.mjs +73 -82
- package/dist/esm/utils/metroCacheManager.mjs.map +1 -1
- package/dist/esm/utils/metroCacheManager.native.js +91 -102
- package/dist/esm/utils/metroCacheManager.native.js.map +1 -1
- package/dist/esm/utils/patchExpoGoManifestHandlerMiddlewareWithCustomMainModuleName.mjs +11 -10
- package/dist/esm/utils/patchExpoGoManifestHandlerMiddlewareWithCustomMainModuleName.mjs.map +1 -1
- package/dist/esm/utils/patchExpoGoManifestHandlerMiddlewareWithCustomMainModuleName.native.js +11 -10
- package/dist/esm/utils/patchExpoGoManifestHandlerMiddlewareWithCustomMainModuleName.native.js.map +1 -1
- package/dist/esm/utils/projectImport.mjs +23 -25
- package/dist/esm/utils/projectImport.mjs.map +1 -1
- package/dist/esm/utils/projectImport.native.js +24 -33
- package/dist/esm/utils/projectImport.native.js.map +1 -1
- package/package.json +5 -6
- package/src/metro-config/getMetroConfigFromViteConfig.ts +11 -18
- package/src/transformer/metroNativeWorker.test.ts +110 -49
- package/src/transformer/metroNativeWorker.ts +3 -11
- package/types/babel-plugins/import-meta-env-plugin.d.ts.map +1 -1
- package/types/metro-config/getMetroConfigFromViteConfig.d.ts.map +1 -1
- package/types/rn-commands/bundle/assetPathUtils.d.ts.map +1 -1
- package/types/rn-commands/bundle/filterPlatformAssetScales.d.ts.map +1 -1
- package/types/rn-commands/bundle/getAssetDestPathAndroid.d.ts.map +1 -1
- package/types/rn-commands/bundle/getAssetDestPathIOS.d.ts.map +1 -1
- package/types/rn-commands/bundle/saveAssets.d.ts.map +1 -1
- package/types/transformer/metroNativeWorker.d.ts.map +1 -1
- package/dist/cjs/transformer/metroNativeWorker.test.cjs +0 -1054
- package/dist/cjs/transformer/metroNativeWorker.test.native.js +0 -1170
- package/dist/cjs/transformer/metroNativeWorker.test.native.js.map +0 -1
- package/dist/cjs/transformer/transformSync.test.cjs +0 -36
- package/dist/cjs/transformer/transformSync.test.native.js +0 -49
- package/dist/cjs/transformer/transformSync.test.native.js.map +0 -1
- package/dist/esm/index.js.map +0 -1
- package/dist/esm/index.mjs.map +0 -1
- package/dist/esm/index.native.js.map +0 -1
- package/dist/esm/metro-config/types.mjs.map +0 -1
- package/dist/esm/metro-config/types.native.js.map +0 -1
- package/dist/esm/rn-commands/bundle/index.mjs.map +0 -1
- package/dist/esm/rn-commands/bundle/index.native.js.map +0 -1
- package/dist/esm/rn-commands/bundle/types.mjs.map +0 -1
- package/dist/esm/rn-commands/bundle/types.native.js.map +0 -1
- package/dist/esm/rn-commands/index.mjs.map +0 -1
- package/dist/esm/rn-commands/index.native.js.map +0 -1
- package/dist/esm/transformer/babel-core.mjs.map +0 -1
- package/dist/esm/transformer/babel-core.native.js.map +0 -1
- package/dist/esm/transformer/metroNativeWorker.test.mjs +0 -1010
- package/dist/esm/transformer/metroNativeWorker.test.mjs.map +0 -1
- package/dist/esm/transformer/metroNativeWorker.test.native.js +0 -1124
- package/dist/esm/transformer/metroNativeWorker.test.native.js.map +0 -1
- package/dist/esm/transformer/transformSync.test.mjs +0 -37
- package/dist/esm/transformer/transformSync.test.mjs.map +0 -1
- package/dist/esm/transformer/transformSync.test.native.js +0 -47
- package/dist/esm/transformer/transformSync.test.native.js.map +0 -1
- package/dist/esm/transformer/types.mjs.map +0 -1
- package/dist/esm/transformer/types.native.js.map +0 -1
- package/types/transformer/metroNativeWorker.test.d.ts +0 -2
- package/types/transformer/metroNativeWorker.test.d.ts.map +0 -1
- package/types/transformer/transformSync.test.d.ts +0 -2
- package/types/transformer/transformSync.test.d.ts.map +0 -1
|
@@ -1,1010 +0,0 @@
|
|
|
1
|
-
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
-
import * as vm from "node:vm";
|
|
3
|
-
import { parseSync } from "oxc-parser";
|
|
4
|
-
import * as fs from "node:fs";
|
|
5
|
-
import * as os from "node:os";
|
|
6
|
-
import * as path from "node:path";
|
|
7
|
-
import { createRequire } from "node:module";
|
|
8
|
-
import { applyEnvironmentGuard, applyInlineEnvVars, assertNoUnportedBabelPlugins, extractDependencies, getCacheKey, getRemoveServerCodeRouterRoot, transform, wrapModule } from "./metroNativeWorker.mjs";
|
|
9
|
-
import { buildMetroConfigInputFromViteConfig, getMetroConfigFromViteConfig } from "../metro-config/getMetroConfigFromViteConfig.mjs";
|
|
10
|
-
var __require = /* @__PURE__ */(x => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
11
|
-
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
12
|
-
}) : x)(function (x) {
|
|
13
|
-
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
14
|
-
throw Error("Calling `require` for \"" + x + "\" in an environment that doesn't expose the `require` function. See https://rolldown.rs/in-depth/bundling-cjs#require-external-modules for more details.");
|
|
15
|
-
});
|
|
16
|
-
const babelCalls = {
|
|
17
|
-
transform: 0,
|
|
18
|
-
transformSync: 0,
|
|
19
|
-
transformAsync: 0,
|
|
20
|
-
transformFromAstSync: 0,
|
|
21
|
-
parse: 0,
|
|
22
|
-
parseSync: 0
|
|
23
|
-
};
|
|
24
|
-
vi.mock("@babel/core", async importOriginal => {
|
|
25
|
-
const actual = await importOriginal();
|
|
26
|
-
return {
|
|
27
|
-
...actual,
|
|
28
|
-
transform: vi.fn((...args) => {
|
|
29
|
-
babelCalls.transform++;
|
|
30
|
-
return actual.transform(...args);
|
|
31
|
-
}),
|
|
32
|
-
transformSync: vi.fn((...args) => {
|
|
33
|
-
babelCalls.transformSync++;
|
|
34
|
-
return actual.transformSync(...args);
|
|
35
|
-
}),
|
|
36
|
-
transformAsync: vi.fn((...args) => {
|
|
37
|
-
babelCalls.transformAsync++;
|
|
38
|
-
return actual.transformAsync(...args);
|
|
39
|
-
}),
|
|
40
|
-
transformFromAstSync: vi.fn((...args) => {
|
|
41
|
-
babelCalls.transformFromAstSync++;
|
|
42
|
-
return actual.transformFromAstSync(...args);
|
|
43
|
-
}),
|
|
44
|
-
parse: vi.fn((...args) => {
|
|
45
|
-
babelCalls.parse++;
|
|
46
|
-
return actual.parse(...args);
|
|
47
|
-
}),
|
|
48
|
-
parseSync: vi.fn((...args) => {
|
|
49
|
-
babelCalls.parseSync++;
|
|
50
|
-
return actual.parseSync(...args);
|
|
51
|
-
})
|
|
52
|
-
};
|
|
53
|
-
});
|
|
54
|
-
beforeEach(() => {
|
|
55
|
-
babelCalls.transform = 0;
|
|
56
|
-
babelCalls.transformSync = 0;
|
|
57
|
-
babelCalls.transformAsync = 0;
|
|
58
|
-
babelCalls.transformFromAstSync = 0;
|
|
59
|
-
babelCalls.parse = 0;
|
|
60
|
-
babelCalls.parseSync = 0;
|
|
61
|
-
});
|
|
62
|
-
function assertZeroBabelCalls() {
|
|
63
|
-
expect(babelCalls.transform).toBe(0);
|
|
64
|
-
expect(babelCalls.transformSync).toBe(0);
|
|
65
|
-
expect(babelCalls.transformAsync).toBe(0);
|
|
66
|
-
expect(babelCalls.transformFromAstSync).toBe(0);
|
|
67
|
-
expect(babelCalls.parse).toBe(0);
|
|
68
|
-
expect(babelCalls.parseSync).toBe(0);
|
|
69
|
-
}
|
|
70
|
-
describe("metroNativeWorker", () => {
|
|
71
|
-
it.each([["data.json", true], ["data.json", false], ["data.js", false]])("minifies %s with source maps enabled=%s without stale mappings", async (filename, sourceMap) => {
|
|
72
|
-
const result = await transform({
|
|
73
|
-
minifierPath: createRequire(import.meta.url).resolve("metro-minify-terser"),
|
|
74
|
-
minifierConfig: {
|
|
75
|
-
sourceMap
|
|
76
|
-
}
|
|
77
|
-
}, "/project", filename, Buffer.from(filename.endsWith(".json") ? "{\"answer\":42}" : "module.exports = { answer: 42 }"), {
|
|
78
|
-
dev: false,
|
|
79
|
-
minify: true,
|
|
80
|
-
platform: "ios",
|
|
81
|
-
type: "module"
|
|
82
|
-
});
|
|
83
|
-
const module = {
|
|
84
|
-
exports: {}
|
|
85
|
-
};
|
|
86
|
-
const {
|
|
87
|
-
code,
|
|
88
|
-
map
|
|
89
|
-
} = result.output[0].data;
|
|
90
|
-
vm.runInNewContext(code, {
|
|
91
|
-
__d(factory) {
|
|
92
|
-
factory({}, () => {}, () => {}, () => {}, module, module.exports, []);
|
|
93
|
-
}
|
|
94
|
-
});
|
|
95
|
-
expect(module.exports.answer).toBe(42);
|
|
96
|
-
expect(map.every(entry => entry.length === 2)).toBe(true);
|
|
97
|
-
assertZeroBabelCalls();
|
|
98
|
-
});
|
|
99
|
-
it("minifies production modules with executable output and original source locations", async () => {
|
|
100
|
-
const source = "exports.answer = function answer(value) {\n return value + 42\n}\n";
|
|
101
|
-
const {
|
|
102
|
-
code,
|
|
103
|
-
map
|
|
104
|
-
} = (await transform({
|
|
105
|
-
minifierPath: createRequire(import.meta.url).resolve("metro-minify-terser"),
|
|
106
|
-
minifierConfig: {
|
|
107
|
-
mangle: true
|
|
108
|
-
}
|
|
109
|
-
}, "/project", "answer.js", Buffer.from(source), {
|
|
110
|
-
dev: false,
|
|
111
|
-
minify: true,
|
|
112
|
-
platform: "ios",
|
|
113
|
-
type: "module"
|
|
114
|
-
})).output[0].data;
|
|
115
|
-
const module = {
|
|
116
|
-
exports: {}
|
|
117
|
-
};
|
|
118
|
-
vm.runInNewContext(code, {
|
|
119
|
-
__d(factory) {
|
|
120
|
-
factory({}, () => {}, () => {}, () => {}, module, module.exports, []);
|
|
121
|
-
}
|
|
122
|
-
});
|
|
123
|
-
expect(module.exports.answer(8)).toBe(50);
|
|
124
|
-
const msm = await import("metro-source-map");
|
|
125
|
-
const fullMap = msm.fromRawMappings([{
|
|
126
|
-
code,
|
|
127
|
-
path: "answer.js",
|
|
128
|
-
source,
|
|
129
|
-
map
|
|
130
|
-
}]).toMap();
|
|
131
|
-
const offset = code.indexOf("42");
|
|
132
|
-
expect(offset).toBeGreaterThan(-1);
|
|
133
|
-
const prefix = code.slice(0, offset).split("\n");
|
|
134
|
-
const position = new msm.Consumer(fullMap).originalPositionFor({
|
|
135
|
-
line: prefix.length,
|
|
136
|
-
column: prefix.at(-1).length
|
|
137
|
-
});
|
|
138
|
-
expect(position.source).toBe("answer.js");
|
|
139
|
-
expect(position.line).toBe(2);
|
|
140
|
-
expect(position.column).toBe(17);
|
|
141
|
-
assertZeroBabelCalls();
|
|
142
|
-
});
|
|
143
|
-
it("extracts all dependencies with oxc-parser and zero Babel", () => {
|
|
144
|
-
const deps = extractDependencies(`
|
|
145
|
-
import React, { useState } from 'react'
|
|
146
|
-
import * as RN from 'react-native'
|
|
147
|
-
import './global.css'
|
|
148
|
-
import type { UserType } from './user-types'
|
|
149
|
-
export { Button } from './Button'
|
|
150
|
-
export * from './icons'
|
|
151
|
-
export type { ThemeType } from './theme-types'
|
|
152
|
-
const helper = require('./helper')
|
|
153
|
-
async function loadAsync() {
|
|
154
|
-
const dyn = await import('./dynamic-module')
|
|
155
|
-
}
|
|
156
|
-
`, "TestComponent.tsx");
|
|
157
|
-
assertZeroBabelCalls();
|
|
158
|
-
const names = deps.map(d => d.name);
|
|
159
|
-
expect(names).toContain("react");
|
|
160
|
-
expect(names).toContain("react-native");
|
|
161
|
-
expect(names).toContain("./global.css");
|
|
162
|
-
expect(names).toContain("./Button");
|
|
163
|
-
expect(names).toContain("./icons");
|
|
164
|
-
expect(names).toContain("./helper");
|
|
165
|
-
expect(names).toContain("./dynamic-module");
|
|
166
|
-
expect(names).not.toContain("./user-types");
|
|
167
|
-
expect(names).not.toContain("./theme-types");
|
|
168
|
-
const reactDep = deps.find(d => d.name === "react");
|
|
169
|
-
expect(reactDep.data.isESMImportAtSource).toBe(true);
|
|
170
|
-
expect(reactDep.data.asyncType).toBeNull();
|
|
171
|
-
expect(reactDep.data.locs.length).toBeGreaterThan(0);
|
|
172
|
-
const loc = reactDep.data.locs[0];
|
|
173
|
-
expect(loc.start.line).toBeGreaterThan(0);
|
|
174
|
-
expect(loc.start.column).toBeGreaterThanOrEqual(0);
|
|
175
|
-
expect(loc.end.line).toBeGreaterThanOrEqual(loc.start.line);
|
|
176
|
-
expect(loc.end.column).toBeGreaterThanOrEqual(0);
|
|
177
|
-
const helperDep = deps.find(d => d.name === "./helper");
|
|
178
|
-
expect(helperDep.data.isESMImportAtSource).toBe(false);
|
|
179
|
-
expect(helperDep.data.asyncType).toBeNull();
|
|
180
|
-
expect(deps.find(d => d.name === "./dynamic-module").data.asyncType).toBe("async");
|
|
181
|
-
});
|
|
182
|
-
it("marks requires inside a try block optional", () => {
|
|
183
|
-
const code = `
|
|
184
|
-
let worklets
|
|
185
|
-
try {
|
|
186
|
-
worklets = require('react-native-worklets-core')
|
|
187
|
-
} catch {}
|
|
188
|
-
const always = require('./always-there')
|
|
189
|
-
`;
|
|
190
|
-
expect(extractDependencies(code, "setup.js").find(d => d.name === "react-native-worklets-core").data.isOptional).toBeUndefined();
|
|
191
|
-
const on = extractDependencies(code, "setup.js", {
|
|
192
|
-
allowOptionalDependencies: true
|
|
193
|
-
});
|
|
194
|
-
expect(on.find(d => d.name === "react-native-worklets-core").data.isOptional).toBe(true);
|
|
195
|
-
expect(on.find(d => d.name === "./always-there").data.isOptional).toBeUndefined();
|
|
196
|
-
expect(extractDependencies(code, "setup.js", {
|
|
197
|
-
allowOptionalDependencies: {
|
|
198
|
-
exclude: ["react-native-worklets-core"]
|
|
199
|
-
}
|
|
200
|
-
}).find(d => d.name === "react-native-worklets-core").data.isOptional).toBeUndefined();
|
|
201
|
-
});
|
|
202
|
-
it("wraps modules in Metro CommonJS format with and without moduleId", () => {
|
|
203
|
-
const code = "exports.val = 123;";
|
|
204
|
-
const wrappedWithoutId = wrapModule(code);
|
|
205
|
-
expect(wrappedWithoutId).toContain("__d(function (global, _$$_REQUIRE, _$$_IMPORT_DEFAULT, _$$_IMPORT_ALL, module, exports, _dependencyMap) {");
|
|
206
|
-
expect(wrappedWithoutId).toContain("var require = _$$_REQUIRE;");
|
|
207
|
-
expect(wrappedWithoutId).toContain("exports.val = 123;");
|
|
208
|
-
expect(wrappedWithoutId.endsWith(");")).toBe(true);
|
|
209
|
-
expect(wrapModule(code, {
|
|
210
|
-
moduleId: 99,
|
|
211
|
-
dependencyIds: [1, 2, 3]
|
|
212
|
-
})).toContain(", 99, [1,2,3]);");
|
|
213
|
-
});
|
|
214
|
-
it("transforms TSX with React JSX, Hermes lowering, and ZERO Babel", async () => {
|
|
215
|
-
const result = await transform({}, "/project", "Greeter.tsx", Buffer.from(`
|
|
216
|
-
import React from 'react'
|
|
217
|
-
import { View, Text } from 'react-native'
|
|
218
|
-
const helper = require('./helper')
|
|
219
|
-
|
|
220
|
-
export class Greeter extends React.Component {
|
|
221
|
-
greeting = 'Hello from Native'
|
|
222
|
-
|
|
223
|
-
async fetchGreeting() {
|
|
224
|
-
return this.greeting
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
render() {
|
|
228
|
-
return (
|
|
229
|
-
<View testID="greeter">
|
|
230
|
-
<Text>{this.greeting}</Text>
|
|
231
|
-
</View>
|
|
232
|
-
)
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
`, "utf8"), {
|
|
236
|
-
dev: true,
|
|
237
|
-
platform: "ios",
|
|
238
|
-
type: "module"
|
|
239
|
-
});
|
|
240
|
-
assertZeroBabelCalls();
|
|
241
|
-
expect(result.output).toHaveLength(1);
|
|
242
|
-
const output = result.output[0];
|
|
243
|
-
expect(output.type).toBe("js/module");
|
|
244
|
-
expect(output.data.lineCount).toBeGreaterThan(0);
|
|
245
|
-
expect(output.data.map.length).toBeGreaterThan(0);
|
|
246
|
-
const code = output.data.code;
|
|
247
|
-
expect(code).toContain("__d(function (global, _$$_REQUIRE, _$$_IMPORT_DEFAULT, _$$_IMPORT_ALL, module, exports, _dependencyMap) {");
|
|
248
|
-
expect(code).toContain("var require = _$$_REQUIRE;");
|
|
249
|
-
expect(code).not.toContain("<View");
|
|
250
|
-
expect(code).not.toContain("<Text");
|
|
251
|
-
expect(code).toContain("this.greeting = \"Hello from Native\"");
|
|
252
|
-
const depNames = result.dependencies.map(d => d.name);
|
|
253
|
-
expect(depNames).toContain("react");
|
|
254
|
-
expect(depNames).toContain("react-native");
|
|
255
|
-
expect(depNames).toContain("./helper");
|
|
256
|
-
expect(depNames).toContain("react/jsx-dev-runtime");
|
|
257
|
-
});
|
|
258
|
-
it("lowers async generators, including the method shorthand Hermes also rejects", async () => {
|
|
259
|
-
for (const [filename, sourceCode] of Object.entries({
|
|
260
|
-
"cursor.ts": `
|
|
261
|
-
export class Cursor {
|
|
262
|
-
async *rows() {
|
|
263
|
-
yield await Promise.resolve(1)
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
`,
|
|
267
|
-
"stream.ts": `
|
|
268
|
-
export async function* stream() {
|
|
269
|
-
yield 1
|
|
270
|
-
}
|
|
271
|
-
`,
|
|
272
|
-
"drain.ts": `
|
|
273
|
-
export async function drain(src) {
|
|
274
|
-
for await (const x of src) {
|
|
275
|
-
console.log(x)
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
`
|
|
279
|
-
})) {
|
|
280
|
-
const result = await transform({}, "/project", filename, Buffer.from(sourceCode, "utf8"), {
|
|
281
|
-
dev: true,
|
|
282
|
-
platform: "ios",
|
|
283
|
-
type: "module"
|
|
284
|
-
});
|
|
285
|
-
assertZeroBabelCalls();
|
|
286
|
-
const code = result.output[0].data.code;
|
|
287
|
-
expect(code, filename).not.toMatch(/\basync\s+function\s*\*/);
|
|
288
|
-
expect(code, filename).not.toMatch(/\basync\s*\*\s*[\w$[]/);
|
|
289
|
-
expect(code, filename).not.toMatch(/\bfor\s+await\s*\(/);
|
|
290
|
-
}
|
|
291
|
-
});
|
|
292
|
-
it("handles JSON files without Babel", async () => {
|
|
293
|
-
const jsonSource = JSON.stringify({
|
|
294
|
-
name: "my-package",
|
|
295
|
-
version: "1.0.0"
|
|
296
|
-
});
|
|
297
|
-
const result = await transform({}, "/project", "package.json", Buffer.from(jsonSource, "utf8"), {
|
|
298
|
-
dev: true,
|
|
299
|
-
platform: "ios",
|
|
300
|
-
type: "module"
|
|
301
|
-
});
|
|
302
|
-
assertZeroBabelCalls();
|
|
303
|
-
expect(result.dependencies).toEqual([]);
|
|
304
|
-
expect(result.output).toHaveLength(1);
|
|
305
|
-
expect(result.output[0].type).toBe("js/module");
|
|
306
|
-
expect(result.output[0].data.code).toContain("module.exports = {\"name\":\"my-package\",\"version\":\"1.0.0\"}");
|
|
307
|
-
expect(result.output[0].data.code).toContain("__d(function (global, require");
|
|
308
|
-
});
|
|
309
|
-
it("handles Asset files without Babel", async () => {
|
|
310
|
-
const result = await transform({
|
|
311
|
-
assetRegistryPath: "react-native/Libraries/Image/AssetRegistry",
|
|
312
|
-
publicPath: "/assets"
|
|
313
|
-
}, "/project", "logo.png", Buffer.from("fake-png-binary-data", "utf8"), {
|
|
314
|
-
dev: true,
|
|
315
|
-
platform: "ios",
|
|
316
|
-
type: "asset"
|
|
317
|
-
});
|
|
318
|
-
assertZeroBabelCalls();
|
|
319
|
-
expect(result.output[0].type).toBe("js/module/asset");
|
|
320
|
-
expect(result.output[0].data.code).toContain("registerAsset(");
|
|
321
|
-
expect(result.output[0].data.code).toContain("__d(function");
|
|
322
|
-
expect(result.dependencies.map(d => d.name)).toContain("react-native/Libraries/Image/AssetRegistry");
|
|
323
|
-
});
|
|
324
|
-
it("handles Flow files by stripping types without Babel", async () => {
|
|
325
|
-
const result = await transform({}, "/project", "math.js", Buffer.from(`
|
|
326
|
-
// @flow
|
|
327
|
-
function add(a: number, b: number): number {
|
|
328
|
-
return a + b;
|
|
329
|
-
}
|
|
330
|
-
export default add;
|
|
331
|
-
`, "utf8"), {
|
|
332
|
-
dev: false,
|
|
333
|
-
platform: "ios",
|
|
334
|
-
type: "module"
|
|
335
|
-
});
|
|
336
|
-
assertZeroBabelCalls();
|
|
337
|
-
expect(result.output[0].data.code).not.toContain(": number");
|
|
338
|
-
expect(result.output[0].data.code).toContain("function add(a, b)");
|
|
339
|
-
});
|
|
340
|
-
it("generates a deterministic cache key without Babel", () => {
|
|
341
|
-
const key1 = getCacheKey({
|
|
342
|
-
globalPrefix: "__one_",
|
|
343
|
-
minifierPath: "terser"
|
|
344
|
-
}, {
|
|
345
|
-
projectRoot: "/app"
|
|
346
|
-
});
|
|
347
|
-
const key2 = getCacheKey({
|
|
348
|
-
globalPrefix: "__one_",
|
|
349
|
-
minifierPath: "terser"
|
|
350
|
-
}, {
|
|
351
|
-
projectRoot: "/app"
|
|
352
|
-
});
|
|
353
|
-
const key3 = getCacheKey({
|
|
354
|
-
globalPrefix: "__other_"
|
|
355
|
-
}, {
|
|
356
|
-
projectRoot: "/app"
|
|
357
|
-
});
|
|
358
|
-
assertZeroBabelCalls();
|
|
359
|
-
expect(key1).toBe(key2);
|
|
360
|
-
expect(key1).not.toBe(key3);
|
|
361
|
-
expect(typeof key1).toBe("string");
|
|
362
|
-
expect(key1.length).toBe(64);
|
|
363
|
-
});
|
|
364
|
-
it("correctly sets transformerPath in buildMetroConfigInputFromViteConfig when active", async () => {
|
|
365
|
-
const mockViteConfig = {
|
|
366
|
-
root: process.cwd()
|
|
367
|
-
};
|
|
368
|
-
delete process.env.ONE_METRO_NATIVE_TRANSFORMS;
|
|
369
|
-
const configDefault = await buildMetroConfigInputFromViteConfig(mockViteConfig, {});
|
|
370
|
-
expect(configDefault.defaultConfig.transformerPath).toContain("metroNativeWorker");
|
|
371
|
-
expect(configDefault.defaultConfig.transformer.transformerPath).toBeUndefined();
|
|
372
|
-
expect((await buildMetroConfigInputFromViteConfig(mockViteConfig, {
|
|
373
|
-
nativeTransforms: false
|
|
374
|
-
})).defaultConfig.transformerPath).not.toContain("metroNativeWorker");
|
|
375
|
-
process.env.ONE_METRO_NATIVE_TRANSFORMS = "0";
|
|
376
|
-
expect((await buildMetroConfigInputFromViteConfig(mockViteConfig, {})).defaultConfig.transformerPath).not.toContain("metroNativeWorker");
|
|
377
|
-
process.env.ONE_METRO_NATIVE_TRANSFORMS = "1";
|
|
378
|
-
expect((await buildMetroConfigInputFromViteConfig(mockViteConfig, {
|
|
379
|
-
nativeTransforms: false
|
|
380
|
-
})).defaultConfig.transformerPath).toContain("metroNativeWorker");
|
|
381
|
-
delete process.env.ONE_METRO_NATIVE_TRANSFORMS;
|
|
382
|
-
});
|
|
383
|
-
it("falls back to default Metro babel transformer when a user babel config exists", async () => {
|
|
384
|
-
const tempDir = path.join(process.cwd(), `.tmp-metro-test-${Date.now()}`);
|
|
385
|
-
fs.mkdirSync(tempDir, {
|
|
386
|
-
recursive: true
|
|
387
|
-
});
|
|
388
|
-
fs.writeFileSync(path.join(tempDir, "package.json"), "{}");
|
|
389
|
-
try {
|
|
390
|
-
const mockViteConfig = {
|
|
391
|
-
root: tempDir
|
|
392
|
-
};
|
|
393
|
-
delete process.env.ONE_METRO_NATIVE_TRANSFORMS;
|
|
394
|
-
expect((await buildMetroConfigInputFromViteConfig(mockViteConfig, {})).defaultConfig.transformerPath).toContain("metroNativeWorker");
|
|
395
|
-
const babelConfigPath = path.join(tempDir, "babel.config.js");
|
|
396
|
-
fs.writeFileSync(babelConfigPath, "// @one-generated\nmodule.exports = {}");
|
|
397
|
-
expect((await buildMetroConfigInputFromViteConfig(mockViteConfig, {})).defaultConfig.transformerPath).toContain("metroNativeWorker");
|
|
398
|
-
fs.writeFileSync(babelConfigPath, "module.exports = { plugins: [\"my-plugin\"] }");
|
|
399
|
-
expect((await buildMetroConfigInputFromViteConfig(mockViteConfig, {})).defaultConfig.transformerPath).not.toContain("metroNativeWorker");
|
|
400
|
-
process.env.ONE_METRO_NATIVE_TRANSFORMS = "1";
|
|
401
|
-
expect((await buildMetroConfigInputFromViteConfig(mockViteConfig, {})).defaultConfig.transformerPath).toContain("metroNativeWorker");
|
|
402
|
-
delete process.env.ONE_METRO_NATIVE_TRANSFORMS;
|
|
403
|
-
expect((await buildMetroConfigInputFromViteConfig(mockViteConfig, {
|
|
404
|
-
nativeTransforms: true
|
|
405
|
-
})).defaultConfig.transformerPath).toContain("metroNativeWorker");
|
|
406
|
-
} finally {
|
|
407
|
-
delete process.env.ONE_METRO_NATIVE_TRANSFORMS;
|
|
408
|
-
fs.rmSync(tempDir, {
|
|
409
|
-
recursive: true,
|
|
410
|
-
force: true
|
|
411
|
-
});
|
|
412
|
-
}
|
|
413
|
-
});
|
|
414
|
-
it("warns naming the ignored user babel config when native transforms are forced", async () => {
|
|
415
|
-
const tempDir = fs.realpathSync(fs.mkdtempSync(path.join(process.cwd(), ".tmp-metro-test-")));
|
|
416
|
-
fs.writeFileSync(path.join(tempDir, "package.json"), "{}");
|
|
417
|
-
const babelConfigPath = path.join(tempDir, "babel.config.js");
|
|
418
|
-
fs.writeFileSync(babelConfigPath, "module.exports = { plugins: [] }");
|
|
419
|
-
try {
|
|
420
|
-
delete process.env.ONE_METRO_NATIVE_TRANSFORMS;
|
|
421
|
-
const mockViteConfig = {
|
|
422
|
-
root: tempDir
|
|
423
|
-
};
|
|
424
|
-
const warnsWithConfig = spy => spy.mock.calls.some(args => args.join(" ").includes(babelConfigPath));
|
|
425
|
-
const optionWarn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
426
|
-
try {
|
|
427
|
-
expect((await buildMetroConfigInputFromViteConfig(mockViteConfig, {
|
|
428
|
-
nativeTransforms: true
|
|
429
|
-
})).defaultConfig.transformerPath).toContain("metroNativeWorker");
|
|
430
|
-
expect(warnsWithConfig(optionWarn)).toBe(true);
|
|
431
|
-
} finally {
|
|
432
|
-
optionWarn.mockRestore();
|
|
433
|
-
}
|
|
434
|
-
const envWarn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
435
|
-
try {
|
|
436
|
-
process.env.ONE_METRO_NATIVE_TRANSFORMS = "1";
|
|
437
|
-
expect((await buildMetroConfigInputFromViteConfig(mockViteConfig, {})).defaultConfig.transformerPath).toContain("metroNativeWorker");
|
|
438
|
-
expect(warnsWithConfig(envWarn)).toBe(true);
|
|
439
|
-
} finally {
|
|
440
|
-
envWarn.mockRestore();
|
|
441
|
-
delete process.env.ONE_METRO_NATIVE_TRANSFORMS;
|
|
442
|
-
}
|
|
443
|
-
const respectedWarn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
444
|
-
try {
|
|
445
|
-
expect((await buildMetroConfigInputFromViteConfig(mockViteConfig, {})).defaultConfig.transformerPath).not.toContain("metroNativeWorker");
|
|
446
|
-
expect(warnsWithConfig(respectedWarn)).toBe(false);
|
|
447
|
-
} finally {
|
|
448
|
-
respectedWarn.mockRestore();
|
|
449
|
-
}
|
|
450
|
-
} finally {
|
|
451
|
-
delete process.env.ONE_METRO_NATIVE_TRANSFORMS;
|
|
452
|
-
fs.rmSync(tempDir, {
|
|
453
|
-
recursive: true,
|
|
454
|
-
force: true
|
|
455
|
-
});
|
|
456
|
-
}
|
|
457
|
-
});
|
|
458
|
-
it("selects the same transformer in both metro config builders", async () => {
|
|
459
|
-
const tempDir = fs.realpathSync(fs.mkdtempSync(path.join(process.cwd(), ".tmp-metro-test-")));
|
|
460
|
-
fs.writeFileSync(path.join(tempDir, "package.json"), "{}");
|
|
461
|
-
try {
|
|
462
|
-
delete process.env.ONE_METRO_NATIVE_TRANSFORMS;
|
|
463
|
-
const mockViteConfig = {
|
|
464
|
-
root: tempDir
|
|
465
|
-
};
|
|
466
|
-
const nativeInput = await buildMetroConfigInputFromViteConfig(mockViteConfig, {});
|
|
467
|
-
const nativeFull = await getMetroConfigFromViteConfig(mockViteConfig, {});
|
|
468
|
-
expect(nativeInput.defaultConfig.transformerPath).toContain("metroNativeWorker");
|
|
469
|
-
expect(nativeFull.transformerPath).toContain("metroNativeWorker");
|
|
470
|
-
const babelConfigPath = path.join(tempDir, "babel.config.js");
|
|
471
|
-
fs.writeFileSync(babelConfigPath, "module.exports = { plugins: [] }");
|
|
472
|
-
const fallbackInput = await buildMetroConfigInputFromViteConfig(mockViteConfig, {});
|
|
473
|
-
const fallbackFull = await getMetroConfigFromViteConfig(mockViteConfig, {});
|
|
474
|
-
expect(fallbackInput.defaultConfig.transformerPath).not.toContain("metroNativeWorker");
|
|
475
|
-
expect(fallbackFull.transformerPath).not.toContain("metroNativeWorker");
|
|
476
|
-
const forcedWarn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
477
|
-
try {
|
|
478
|
-
const forcedInput = await buildMetroConfigInputFromViteConfig(mockViteConfig, {
|
|
479
|
-
nativeTransforms: true
|
|
480
|
-
});
|
|
481
|
-
const forcedFull = await getMetroConfigFromViteConfig(mockViteConfig, {
|
|
482
|
-
nativeTransforms: true
|
|
483
|
-
});
|
|
484
|
-
expect(forcedInput.defaultConfig.transformerPath).toContain("metroNativeWorker");
|
|
485
|
-
expect(forcedFull.transformerPath).toContain("metroNativeWorker");
|
|
486
|
-
expect(forcedWarn.mock.calls.some(args => args.join(" ").includes(babelConfigPath))).toBe(true);
|
|
487
|
-
} finally {
|
|
488
|
-
forcedWarn.mockRestore();
|
|
489
|
-
}
|
|
490
|
-
} finally {
|
|
491
|
-
delete process.env.ONE_METRO_NATIVE_TRANSFORMS;
|
|
492
|
-
fs.rmSync(tempDir, {
|
|
493
|
-
recursive: true,
|
|
494
|
-
force: true
|
|
495
|
-
});
|
|
496
|
-
}
|
|
497
|
-
});
|
|
498
|
-
it("transforms and executes a real 2-module Metro bundle through metro-runtime require polyfill", async () => {
|
|
499
|
-
const depSource = "exports.value = 42;";
|
|
500
|
-
const aSource = "const dep = require(\"./dep\"); module.exports = { doubled: dep.value * 2 };";
|
|
501
|
-
const depRes = await transform({}, "/project", "dep.js", Buffer.from(depSource, "utf8"), {
|
|
502
|
-
dev: false,
|
|
503
|
-
platform: "ios",
|
|
504
|
-
type: "module",
|
|
505
|
-
moduleId: 2,
|
|
506
|
-
dependencyIds: []
|
|
507
|
-
});
|
|
508
|
-
const aRes = await transform({}, "/project", "a.js", Buffer.from(aSource, "utf8"), {
|
|
509
|
-
dev: false,
|
|
510
|
-
platform: "ios",
|
|
511
|
-
type: "module",
|
|
512
|
-
moduleId: 1,
|
|
513
|
-
dependencyIds: [2]
|
|
514
|
-
});
|
|
515
|
-
assertZeroBabelCalls();
|
|
516
|
-
expect(aRes.dependencies).toHaveLength(1);
|
|
517
|
-
expect(aRes.dependencies[0].name).toBe("./dep");
|
|
518
|
-
const aEmittedCode = aRes.output[0].data.code;
|
|
519
|
-
expect(aEmittedCode).toContain("require(_dependencyMap[0], \"./dep\")");
|
|
520
|
-
const polyfillPath = __require.resolve("metro-runtime/src/polyfills/require.js");
|
|
521
|
-
const polyfillCode = fs.readFileSync(polyfillPath, "utf8");
|
|
522
|
-
const contextObj = {
|
|
523
|
-
__DEV__: false,
|
|
524
|
-
__METRO_GLOBAL_PREFIX__: "",
|
|
525
|
-
console
|
|
526
|
-
};
|
|
527
|
-
contextObj.global = contextObj;
|
|
528
|
-
vm.createContext(contextObj);
|
|
529
|
-
vm.runInContext(polyfillCode, contextObj);
|
|
530
|
-
vm.runInContext(depRes.output[0].data.code, contextObj);
|
|
531
|
-
vm.runInContext(aEmittedCode, contextObj);
|
|
532
|
-
expect(vm.runInContext("global.__r(1)", contextObj)).toEqual({
|
|
533
|
-
doubled: 84
|
|
534
|
-
});
|
|
535
|
-
});
|
|
536
|
-
it("transforms and executes a 2-module Metro bundle with ESM imports in dev mode", async () => {
|
|
537
|
-
const depSource = "export const greeting = \"Hello Metro Native\";";
|
|
538
|
-
const aSource = "import { greeting } from \"./dep\"; export const message = greeting.toUpperCase();";
|
|
539
|
-
const depRes = await transform({}, "/project", "dep.js", Buffer.from(depSource, "utf8"), {
|
|
540
|
-
dev: true,
|
|
541
|
-
platform: "ios",
|
|
542
|
-
type: "module",
|
|
543
|
-
moduleId: 2,
|
|
544
|
-
dependencyIds: []
|
|
545
|
-
});
|
|
546
|
-
const aRes = await transform({}, "/project", "a.js", Buffer.from(aSource, "utf8"), {
|
|
547
|
-
dev: true,
|
|
548
|
-
platform: "ios",
|
|
549
|
-
type: "module",
|
|
550
|
-
moduleId: 1,
|
|
551
|
-
dependencyIds: [2]
|
|
552
|
-
});
|
|
553
|
-
assertZeroBabelCalls();
|
|
554
|
-
const polyfillPath = __require.resolve("metro-runtime/src/polyfills/require.js");
|
|
555
|
-
const polyfillCode = fs.readFileSync(polyfillPath, "utf8");
|
|
556
|
-
const contextObj = {
|
|
557
|
-
__DEV__: true,
|
|
558
|
-
__METRO_GLOBAL_PREFIX__: "",
|
|
559
|
-
console
|
|
560
|
-
};
|
|
561
|
-
contextObj.global = contextObj;
|
|
562
|
-
vm.createContext(contextObj);
|
|
563
|
-
vm.runInContext(polyfillCode, contextObj);
|
|
564
|
-
vm.runInContext(depRes.output[0].data.code, contextObj);
|
|
565
|
-
vm.runInContext(aRes.output[0].data.code, contextObj);
|
|
566
|
-
expect(vm.runInContext("global.__r(1)", contextObj).message).toBe("HELLO METRO NATIVE");
|
|
567
|
-
});
|
|
568
|
-
it("respects lexical scope shadowing for require calls (Astra probe)", async () => {
|
|
569
|
-
const res = await transform({}, "/project", "shadowed.js", Buffer.from("function f(require){return require(\"./local\")}; module.exports=f(x=>x);", "utf8"), {
|
|
570
|
-
dev: false,
|
|
571
|
-
platform: "ios",
|
|
572
|
-
type: "module",
|
|
573
|
-
moduleId: 1,
|
|
574
|
-
dependencyIds: []
|
|
575
|
-
});
|
|
576
|
-
assertZeroBabelCalls();
|
|
577
|
-
expect(res.dependencies).toHaveLength(0);
|
|
578
|
-
const emitted = res.output[0].data.code;
|
|
579
|
-
expect(emitted).not.toContain("_dependencyMap[0]");
|
|
580
|
-
expect(emitted).toMatch(/require\d*\("\.\/local"\)/);
|
|
581
|
-
const polyfillPath = __require.resolve("metro-runtime/src/polyfills/require.js");
|
|
582
|
-
const polyfillCode = fs.readFileSync(polyfillPath, "utf8");
|
|
583
|
-
const contextObj = {
|
|
584
|
-
__DEV__: false,
|
|
585
|
-
__METRO_GLOBAL_PREFIX__: "",
|
|
586
|
-
console
|
|
587
|
-
};
|
|
588
|
-
contextObj.global = contextObj;
|
|
589
|
-
vm.createContext(contextObj);
|
|
590
|
-
vm.runInContext(polyfillCode, contextObj);
|
|
591
|
-
vm.runInContext(emitted, contextObj);
|
|
592
|
-
expect(vm.runInContext("global.__r(1)", contextObj)).toBe("./local");
|
|
593
|
-
});
|
|
594
|
-
it("preserves distinct indexed identity for duplicate and mixed sync/async dependencies", async () => {
|
|
595
|
-
const res = await transform({}, "/project", "index.js", Buffer.from(`
|
|
596
|
-
const a = require('./dep');
|
|
597
|
-
const b = require('./dep');
|
|
598
|
-
async function load() {
|
|
599
|
-
const c = await import('./dep');
|
|
600
|
-
}
|
|
601
|
-
`, "utf8"), {
|
|
602
|
-
dev: false,
|
|
603
|
-
platform: "ios",
|
|
604
|
-
type: "module"
|
|
605
|
-
});
|
|
606
|
-
assertZeroBabelCalls();
|
|
607
|
-
expect(res.dependencies.map(d => d.name)).toContain("./dep");
|
|
608
|
-
const syncDep = res.dependencies.find(d => d.name === "./dep" && d.data.asyncType === null);
|
|
609
|
-
const asyncDep = res.dependencies.find(d => d.name === "./dep" && d.data.asyncType === "async");
|
|
610
|
-
expect(syncDep).toBeDefined();
|
|
611
|
-
expect(asyncDep).toBeDefined();
|
|
612
|
-
expect(syncDep.data.index).not.toEqual(asyncDep.data.index);
|
|
613
|
-
const emitted = res.output[0].data.code;
|
|
614
|
-
const syncIndex = syncDep.data.index;
|
|
615
|
-
expect(emitted).toContain(`require(_dependencyMap[${syncIndex}], "./dep")`);
|
|
616
|
-
});
|
|
617
|
-
it("conforms to Metro map ABI with raw mapping segment tuples and accurate symbolication", async () => {
|
|
618
|
-
const res = await transform({}, "/project", "math.ts", Buffer.from(`
|
|
619
|
-
export function add(a: number, b: number): number {
|
|
620
|
-
return a + b;
|
|
621
|
-
}
|
|
622
|
-
`, "utf8"), {
|
|
623
|
-
dev: false,
|
|
624
|
-
platform: "ios",
|
|
625
|
-
type: "module"
|
|
626
|
-
});
|
|
627
|
-
assertZeroBabelCalls();
|
|
628
|
-
const map = res.output[0].data.map;
|
|
629
|
-
expect(Array.isArray(map)).toBe(true);
|
|
630
|
-
expect(map.length).toBeGreaterThan(0);
|
|
631
|
-
for (const tuple of map) {
|
|
632
|
-
expect(Array.isArray(tuple)).toBe(true);
|
|
633
|
-
expect(typeof tuple[0]).toBe("number");
|
|
634
|
-
expect(typeof tuple[1]).toBe("number");
|
|
635
|
-
expect(tuple).not.toHaveProperty("version");
|
|
636
|
-
expect(tuple).not.toHaveProperty("mappings");
|
|
637
|
-
}
|
|
638
|
-
const msm = await import("metro-source-map");
|
|
639
|
-
const fullMap = msm.fromRawMappings([{
|
|
640
|
-
code: res.output[0].data.code,
|
|
641
|
-
source: "math.ts",
|
|
642
|
-
map
|
|
643
|
-
}]).toMap();
|
|
644
|
-
const consumer = new msm.Consumer(fullMap);
|
|
645
|
-
const addLineIndex = res.output[0].data.code.split("\n").findIndex(l => l.includes("function add"));
|
|
646
|
-
expect(addLineIndex).toBeGreaterThan(0);
|
|
647
|
-
expect(consumer.originalPositionFor({
|
|
648
|
-
line: addLineIndex + 1,
|
|
649
|
-
column: 16
|
|
650
|
-
}).line).toBe(2);
|
|
651
|
-
});
|
|
652
|
-
it("models JS hoisting in ScopeTracker for functions and vars (Astra probe)", async () => {
|
|
653
|
-
const res = await transform({}, "/project", "hoisted.js", Buffer.from("function f(){const x=require(\"./local\"); function require(x){return x}; return x} module.exports=f();", "utf8"), {
|
|
654
|
-
dev: false,
|
|
655
|
-
platform: "ios",
|
|
656
|
-
type: "module",
|
|
657
|
-
moduleId: 1,
|
|
658
|
-
dependencyIds: []
|
|
659
|
-
});
|
|
660
|
-
assertZeroBabelCalls();
|
|
661
|
-
expect(res.dependencies).toHaveLength(0);
|
|
662
|
-
const emitted = res.output[0].data.code;
|
|
663
|
-
expect(emitted).not.toContain("_dependencyMap[0]");
|
|
664
|
-
expect(emitted).toMatch(/require\d*\("\.\/local"\)/);
|
|
665
|
-
const polyfillPath = __require.resolve("metro-runtime/src/polyfills/require.js");
|
|
666
|
-
const polyfillCode = fs.readFileSync(polyfillPath, "utf8");
|
|
667
|
-
const contextObj = {
|
|
668
|
-
__DEV__: false,
|
|
669
|
-
__METRO_GLOBAL_PREFIX__: "",
|
|
670
|
-
console
|
|
671
|
-
};
|
|
672
|
-
contextObj.global = contextObj;
|
|
673
|
-
vm.createContext(contextObj);
|
|
674
|
-
vm.runInContext(polyfillCode, contextObj);
|
|
675
|
-
vm.runInContext(emitted, contextObj);
|
|
676
|
-
expect(vm.runInContext("global.__r(1)", contextObj)).toBe("./local");
|
|
677
|
-
});
|
|
678
|
-
it("rewrites dynamic import to Metro asyncRequire and executes promise resolution in dev and release", async () => {
|
|
679
|
-
const depSource = "exports.message = \"hello dynamic\";";
|
|
680
|
-
const mainSource = "module.exports = () => import(\"./dep\");";
|
|
681
|
-
const depRes = await transform({}, "/project", "dep.js", Buffer.from(depSource, "utf8"), {
|
|
682
|
-
dev: true,
|
|
683
|
-
platform: "ios",
|
|
684
|
-
type: "module",
|
|
685
|
-
moduleId: 2,
|
|
686
|
-
dependencyIds: []
|
|
687
|
-
});
|
|
688
|
-
const mainRes = await transform({}, "/project", "main.js", Buffer.from(mainSource, "utf8"), {
|
|
689
|
-
dev: true,
|
|
690
|
-
platform: "ios",
|
|
691
|
-
type: "module",
|
|
692
|
-
moduleId: 0,
|
|
693
|
-
dependencyIds: [2, 1]
|
|
694
|
-
});
|
|
695
|
-
assertZeroBabelCalls();
|
|
696
|
-
const depNames = mainRes.dependencies.map(d => d.name);
|
|
697
|
-
expect(depNames).toContain("./dep");
|
|
698
|
-
expect(depNames).toContain("metro-runtime/src/modules/asyncRequire");
|
|
699
|
-
expect(mainRes.dependencies.find(d => d.name === "./dep").data.asyncType).toBe("async");
|
|
700
|
-
const emittedMain = mainRes.output[0].data.code;
|
|
701
|
-
expect(emittedMain).not.toContain("import(\"./dep\")");
|
|
702
|
-
expect(emittedMain).toContain("require(_dependencyMap[1], \"metro-runtime/src/modules/asyncRequire\")(_dependencyMap[0], _dependencyMap.paths, \"./dep\")");
|
|
703
|
-
const polyfillPath = __require.resolve("metro-runtime/src/polyfills/require.js");
|
|
704
|
-
const polyfillCode = fs.readFileSync(polyfillPath, "utf8");
|
|
705
|
-
const asyncRequirePath = __require.resolve("metro-runtime/src/modules/asyncRequire.js");
|
|
706
|
-
const wrappedAsyncRequire = wrapModule(fs.readFileSync(asyncRequirePath, "utf8"), {
|
|
707
|
-
moduleId: 1,
|
|
708
|
-
dependencyIds: []
|
|
709
|
-
});
|
|
710
|
-
for (const isDev of [true, false]) {
|
|
711
|
-
const contextObj = {
|
|
712
|
-
__DEV__: isDev,
|
|
713
|
-
__METRO_GLOBAL_PREFIX__: "",
|
|
714
|
-
console
|
|
715
|
-
};
|
|
716
|
-
contextObj.global = contextObj;
|
|
717
|
-
vm.createContext(contextObj);
|
|
718
|
-
vm.runInContext(polyfillCode, contextObj);
|
|
719
|
-
vm.runInContext(wrappedAsyncRequire, contextObj);
|
|
720
|
-
vm.runInContext(depRes.output[0].data.code, contextObj);
|
|
721
|
-
vm.runInContext(emittedMain, contextObj);
|
|
722
|
-
const mainFn = vm.runInContext("global.__r(0)", contextObj);
|
|
723
|
-
expect(typeof mainFn).toBe("function");
|
|
724
|
-
const promise = mainFn();
|
|
725
|
-
expect(promise instanceof Promise || promise && typeof promise.then === "function").toBe(true);
|
|
726
|
-
const resolved = await promise;
|
|
727
|
-
expect(resolved.message).toBe("hello dynamic");
|
|
728
|
-
expect(resolved.default.message).toBe("hello dynamic");
|
|
729
|
-
}
|
|
730
|
-
});
|
|
731
|
-
it("preserves accurate sourcemap for tokens after rewritten require calls on the same line", async () => {
|
|
732
|
-
const sourceCode = "const a = require(\"./dep\"), b = 42;";
|
|
733
|
-
const resWrapped = await transform({}, "/project", "app.js", Buffer.from(sourceCode, "utf8"), {
|
|
734
|
-
dev: false,
|
|
735
|
-
platform: "ios",
|
|
736
|
-
type: "module"
|
|
737
|
-
});
|
|
738
|
-
assertZeroBabelCalls();
|
|
739
|
-
const msm = await import("metro-source-map");
|
|
740
|
-
const fullMapWrapped = msm.fromRawMappings([{
|
|
741
|
-
code: resWrapped.output[0].data.code,
|
|
742
|
-
source: "app.js",
|
|
743
|
-
map: resWrapped.output[0].data.map
|
|
744
|
-
}]).toMap();
|
|
745
|
-
const consumerWrapped = new msm.Consumer(fullMapWrapped);
|
|
746
|
-
const linesWrapped = resWrapped.output[0].data.code.split("\n");
|
|
747
|
-
const lineIndexWrapped = linesWrapped.findIndex(l => l.includes("b = 42"));
|
|
748
|
-
expect(lineIndexWrapped).toBeGreaterThanOrEqual(0);
|
|
749
|
-
const generatedColWrapped = linesWrapped[lineIndexWrapped].indexOf("b = 42");
|
|
750
|
-
expect(generatedColWrapped).toBeGreaterThan(0);
|
|
751
|
-
const posWrapped = consumerWrapped.originalPositionFor({
|
|
752
|
-
line: lineIndexWrapped + 1,
|
|
753
|
-
column: generatedColWrapped
|
|
754
|
-
});
|
|
755
|
-
expect(posWrapped.line).toBe(1);
|
|
756
|
-
expect(posWrapped.column).toBe(28);
|
|
757
|
-
expect(sourceCode.slice(Number(posWrapped.column))).toContain("b = 42;");
|
|
758
|
-
const resUnwrapped = await transform({
|
|
759
|
-
unstable_disableModuleWrapping: true
|
|
760
|
-
}, "/project", "app.js", Buffer.from(sourceCode, "utf8"), {
|
|
761
|
-
dev: false,
|
|
762
|
-
platform: "ios",
|
|
763
|
-
type: "module"
|
|
764
|
-
});
|
|
765
|
-
const fullMapUnwrapped = msm.fromRawMappings([{
|
|
766
|
-
code: resUnwrapped.output[0].data.code,
|
|
767
|
-
source: "app.js",
|
|
768
|
-
map: resUnwrapped.output[0].data.map
|
|
769
|
-
}]).toMap();
|
|
770
|
-
const consumerUnwrapped = new msm.Consumer(fullMapUnwrapped);
|
|
771
|
-
const linesUnwrapped = resUnwrapped.output[0].data.code.split("\n");
|
|
772
|
-
const lineIndexUnwrapped = linesUnwrapped.findIndex(l => l.includes("b = 42"));
|
|
773
|
-
expect(lineIndexUnwrapped).toBeGreaterThanOrEqual(0);
|
|
774
|
-
const generatedColUnwrapped = linesUnwrapped[lineIndexUnwrapped].indexOf("b = 42");
|
|
775
|
-
const posUnwrapped = consumerUnwrapped.originalPositionFor({
|
|
776
|
-
line: lineIndexUnwrapped + 1,
|
|
777
|
-
column: generatedColUnwrapped
|
|
778
|
-
});
|
|
779
|
-
expect(posUnwrapped.line).toBe(1);
|
|
780
|
-
expect(posUnwrapped.column).toBe(28);
|
|
781
|
-
});
|
|
782
|
-
it("routes a require.context whose directory is held in a variable through the dependency map", async () => {
|
|
783
|
-
const result = await transform({}, "/project", "entry.js", Buffer.from(`
|
|
784
|
-
const routeRoot = './app'
|
|
785
|
-
export const ctx = require.context(routeRoot, true, /\\.tsx$/)
|
|
786
|
-
`, "utf8"), {
|
|
787
|
-
dev: true,
|
|
788
|
-
platform: "ios",
|
|
789
|
-
type: "module"
|
|
790
|
-
});
|
|
791
|
-
expect(result.dependencies.find(d => d.data.contextParams != null)?.name).toBe("./app");
|
|
792
|
-
const calls = [];
|
|
793
|
-
let ctx;
|
|
794
|
-
vm.runInNewContext(result.output[0].data.code, {
|
|
795
|
-
__d: factory => {
|
|
796
|
-
const module = {
|
|
797
|
-
exports: {}
|
|
798
|
-
};
|
|
799
|
-
const req = (...args) => {
|
|
800
|
-
calls.push(args);
|
|
801
|
-
return {
|
|
802
|
-
keys: () => []
|
|
803
|
-
};
|
|
804
|
-
};
|
|
805
|
-
req.context = () => {
|
|
806
|
-
throw new Error("fallbackRequireContext reached");
|
|
807
|
-
};
|
|
808
|
-
factory({}, req, () => ({}), () => ({}), module, module.exports, ["./app"]);
|
|
809
|
-
ctx = module.exports.ctx;
|
|
810
|
-
}
|
|
811
|
-
});
|
|
812
|
-
expect(ctx).toBeDefined();
|
|
813
|
-
expect(calls[0][1]).toBe("./app");
|
|
814
|
-
});
|
|
815
|
-
it("gives loop bodies per-iteration bindings that survive a runtime without them", async () => {
|
|
816
|
-
const asHermesWouldSeeIt = (await transform({}, "/project", "loops.ts", Buffer.from(`
|
|
817
|
-
export function collect() {
|
|
818
|
-
const out = []
|
|
819
|
-
for (let i = 0; i < 2; i++) out.push(() => i)
|
|
820
|
-
for (const label of ['a', 'b']) out.push(() => label)
|
|
821
|
-
for (const key in { x: 1, y: 2 }) out.push(() => key)
|
|
822
|
-
return out.map((f) => f())
|
|
823
|
-
}
|
|
824
|
-
`, "utf8"), {
|
|
825
|
-
dev: true,
|
|
826
|
-
platform: "ios",
|
|
827
|
-
type: "module"
|
|
828
|
-
})).output[0].data.code.replace(/\bfor\s*\(\s*(let|const)\b/g, "for (var");
|
|
829
|
-
let collect;
|
|
830
|
-
vm.runInNewContext(asHermesWouldSeeIt, {
|
|
831
|
-
__d: factory => {
|
|
832
|
-
const module = {
|
|
833
|
-
exports: {}
|
|
834
|
-
};
|
|
835
|
-
factory({}, () => ({}), () => ({}), () => ({}), module, module.exports, []);
|
|
836
|
-
collect = module.exports.collect;
|
|
837
|
-
}
|
|
838
|
-
});
|
|
839
|
-
expect(collect()).toEqual([0, 1, "a", "b", "x", "y"]);
|
|
840
|
-
});
|
|
841
|
-
});
|
|
842
|
-
describe("one native transform ports", () => {
|
|
843
|
-
it("inlines process.env.ONE_SERVER_URL so a native bundle can find its loaders", () => {
|
|
844
|
-
process.env.ONE_SERVER_URL = "https://example.test";
|
|
845
|
-
try {
|
|
846
|
-
expect(applyInlineEnvVars("export const url = process.env.ONE_SERVER_URL;", "loader.ts", false)).toBe("export const url = \"https://example.test\";");
|
|
847
|
-
} finally {
|
|
848
|
-
process.env.ONE_SERVER_URL = void 0;
|
|
849
|
-
}
|
|
850
|
-
});
|
|
851
|
-
it("leaves ONE_SERVER_URL alone when it is only a string", () => {
|
|
852
|
-
const src = "export const key = \"process.env.ONE_SERVER_URL\";";
|
|
853
|
-
expect(applyInlineEnvVars(src, "a.ts", false)).toBe(src);
|
|
854
|
-
});
|
|
855
|
-
it("inlines import.meta.env reads, which oxc otherwise lowers to an empty object", () => {
|
|
856
|
-
const out = applyInlineEnvVars(`export const key = import.meta.env.VITE_POSTHOG_API_KEY;
|
|
857
|
-
export const target = import.meta.env?.TAMAGUI_TARGET;
|
|
858
|
-
export const missing = import.meta.env.NOT_SET;
|
|
859
|
-
export const all = { ...import.meta.env };`, "env.ts", true, {
|
|
860
|
-
DEV: false,
|
|
861
|
-
VITE_POSTHOG_API_KEY: "pk_live",
|
|
862
|
-
TAMAGUI_TARGET: "native"
|
|
863
|
-
});
|
|
864
|
-
expect(out).toContain("export const key = \"pk_live\";");
|
|
865
|
-
expect(out).toContain("export const target = \"native\";");
|
|
866
|
-
expect(out).toContain("export const missing = undefined;");
|
|
867
|
-
expect(out).toContain("\"VITE_POSTHOG_API_KEY\":\"pk_live\"");
|
|
868
|
-
expect(out).not.toContain("import.meta");
|
|
869
|
-
});
|
|
870
|
-
it("inlines process.env keys the vite env map defines", () => {
|
|
871
|
-
const out = applyInlineEnvVars("export const t = process.env.TAMAGUI_TARGET; export const u = process.env.UNKNOWN_THING;", "env.ts", true, {
|
|
872
|
-
TAMAGUI_TARGET: "native"
|
|
873
|
-
});
|
|
874
|
-
expect(out).toContain("export const t = \"native\";");
|
|
875
|
-
expect(out).toContain("process.env.UNKNOWN_THING");
|
|
876
|
-
});
|
|
877
|
-
it("never rewrites an env read in an assignment target", () => {
|
|
878
|
-
const src = "process.env.VITE_ENVIRONMENT = \"ssr\";";
|
|
879
|
-
expect(applyInlineEnvVars(src, "a.ts", true, {
|
|
880
|
-
VITE_ENVIRONMENT: "ios"
|
|
881
|
-
})).toBe(src);
|
|
882
|
-
});
|
|
883
|
-
it("folds branches the inlining made constant so metro never resolves dead imports", () => {
|
|
884
|
-
const out = applyInlineEnvVars(`if (import.meta.env.SSR) { require('./server-only-thing') } else { load(import.meta.env.API) }`, "branch.ts", true, {
|
|
885
|
-
SSR: false,
|
|
886
|
-
API: "https://api.test"
|
|
887
|
-
});
|
|
888
|
-
expect(out).not.toContain("server-only-thing");
|
|
889
|
-
expect(out).toContain("load(\"https://api.test\")");
|
|
890
|
-
});
|
|
891
|
-
it("folds a ternary and keeps a branch it cannot resolve", () => {
|
|
892
|
-
expect(applyInlineEnvVars("const a = import.meta.env.DEV ? x : y;", "a.ts", true, {
|
|
893
|
-
DEV: false
|
|
894
|
-
})).toContain("const a = y;");
|
|
895
|
-
const dynamic = "const a = flag ? x : y;";
|
|
896
|
-
expect(applyInlineEnvVars(dynamic, "a.ts", true, {
|
|
897
|
-
DEV: false
|
|
898
|
-
})).toBe(dynamic);
|
|
899
|
-
});
|
|
900
|
-
it("runs the user native transforms that stand in for their babel plugins", async () => {
|
|
901
|
-
const result = await transform({}, import.meta.dirname, "ota.ts", Buffer.from("export const id = __EXAMPLE_BUILD_ID; export const from = __EXAMPLE_FILENAME;", "utf8"), {
|
|
902
|
-
dev: true,
|
|
903
|
-
platform: "ios",
|
|
904
|
-
type: "module",
|
|
905
|
-
customTransformOptions: {
|
|
906
|
-
vite: {
|
|
907
|
-
nativeTransformModules: ["./__fixtures__/exampleNativeTransform.cjs"]
|
|
908
|
-
}
|
|
909
|
-
}
|
|
910
|
-
});
|
|
911
|
-
assertZeroBabelCalls();
|
|
912
|
-
expect(result.output[0].data.code).toContain("\"ios-build\"");
|
|
913
|
-
expect(result.output[0].data.code).not.toContain("__EXAMPLE_BUILD_ID");
|
|
914
|
-
expect(result.output[0].data.code).toContain(JSON.stringify(path.join(import.meta.dirname, "ota.ts")));
|
|
915
|
-
});
|
|
916
|
-
it("refuses to build when a user babel plugin has no native port", () => {
|
|
917
|
-
const withPlugins = plugins => ({
|
|
918
|
-
customTransformOptions: {
|
|
919
|
-
vite: {
|
|
920
|
-
babelConfig: {
|
|
921
|
-
plugins
|
|
922
|
-
}
|
|
923
|
-
}
|
|
924
|
-
}
|
|
925
|
-
});
|
|
926
|
-
expect(() => assertNoUnportedBabelPlugins(withPlugins([["@vxrn/vite-plugin-metro/babel-plugins/import-meta-env-plugin", {
|
|
927
|
-
env: {}
|
|
928
|
-
}], "one/babel-plugin-environment-guard", "hot-updater/babel-plugin"]))).toThrow(/hot-updater\/babel-plugin/);
|
|
929
|
-
expect(() => assertNoUnportedBabelPlugins(withPlugins([["@vxrn/vite-plugin-metro/babel-plugins/import-meta-env-plugin", {
|
|
930
|
-
env: {}
|
|
931
|
-
}], "one/babel-plugin-environment-guard", ["one/babel-plugin-remove-server-code", {
|
|
932
|
-
routerRoot: "app"
|
|
933
|
-
}], ["babel-plugin-module-resolver", {
|
|
934
|
-
alias: {}
|
|
935
|
-
}], ["one/babel-plugin-one-router-metro", {}], "one/babel-plugin-inline-one-server-url", "babel-plugin-react-compiler", "react-native-reanimated/plugin"]))).not.toThrow();
|
|
936
|
-
});
|
|
937
|
-
it("turns a server-only import into a throw and drops native-only", () => {
|
|
938
|
-
const out = applyEnvironmentGuard(`import 'native-only'
|
|
939
|
-
|
|
940
|
-
export const a = 1`, "route.ts");
|
|
941
|
-
expect(out).not.toContain("native-only");
|
|
942
|
-
expect(out).toContain("server-only cannot be imported in a native environment");
|
|
943
|
-
const parsed = parseSync("route.ts", out, {
|
|
944
|
-
lang: "ts"
|
|
945
|
-
});
|
|
946
|
-
expect(parsed.errors).toEqual([]);
|
|
947
|
-
expect(parsed.program.body.map(n => n.type)).toContain("ThrowStatement");
|
|
948
|
-
});
|
|
949
|
-
it("reads the router root off one's remove-server-code plugin entry", () => {
|
|
950
|
-
expect(getRemoveServerCodeRouterRoot({
|
|
951
|
-
customTransformOptions: {
|
|
952
|
-
vite: {
|
|
953
|
-
babelConfig: {
|
|
954
|
-
plugins: [["one/babel-plugin-remove-server-code", {
|
|
955
|
-
routerRoot: "src/app"
|
|
956
|
-
}]]
|
|
957
|
-
}
|
|
958
|
-
}
|
|
959
|
-
}
|
|
960
|
-
})).toBe("src/app");
|
|
961
|
-
expect(getRemoveServerCodeRouterRoot({
|
|
962
|
-
customTransformOptions: {}
|
|
963
|
-
})).toBe(void 0);
|
|
964
|
-
});
|
|
965
|
-
});
|
|
966
|
-
describe("metro babel fallback config names", () => {
|
|
967
|
-
it("extends json babel configs just like the compiler detection does", async () => {
|
|
968
|
-
for (const name of ["babel.config.json", ".babelrc.json"]) {
|
|
969
|
-
vi.resetModules();
|
|
970
|
-
const {
|
|
971
|
-
loadBabelConfig: loadBabelConfig2
|
|
972
|
-
} = await import("./loadBabelConfig.mjs");
|
|
973
|
-
const tempDir2 = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), "vxrn-babel-json-")));
|
|
974
|
-
try {
|
|
975
|
-
fs.writeFileSync(path.join(tempDir2, name), JSON.stringify({
|
|
976
|
-
plugins: []
|
|
977
|
-
}));
|
|
978
|
-
expect(loadBabelConfig2({
|
|
979
|
-
projectRoot: tempDir2
|
|
980
|
-
})).toEqual({
|
|
981
|
-
extends: path.join(tempDir2, name)
|
|
982
|
-
});
|
|
983
|
-
} finally {
|
|
984
|
-
fs.rmSync(tempDir2, {
|
|
985
|
-
recursive: true,
|
|
986
|
-
force: true
|
|
987
|
-
});
|
|
988
|
-
}
|
|
989
|
-
}
|
|
990
|
-
vi.resetModules();
|
|
991
|
-
const {
|
|
992
|
-
loadBabelConfig
|
|
993
|
-
} = await import("./loadBabelConfig.mjs");
|
|
994
|
-
const tempDir = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), "vxrn-babel-json-")));
|
|
995
|
-
try {
|
|
996
|
-
fs.writeFileSync(path.join(tempDir, "babel.config.js"), "module.exports = { plugins: [] }");
|
|
997
|
-
expect(loadBabelConfig({
|
|
998
|
-
projectRoot: tempDir
|
|
999
|
-
})).toEqual({
|
|
1000
|
-
extends: path.join(tempDir, "babel.config.js")
|
|
1001
|
-
});
|
|
1002
|
-
} finally {
|
|
1003
|
-
fs.rmSync(tempDir, {
|
|
1004
|
-
recursive: true,
|
|
1005
|
-
force: true
|
|
1006
|
-
});
|
|
1007
|
-
}
|
|
1008
|
-
});
|
|
1009
|
-
});
|
|
1010
|
-
//# sourceMappingURL=metroNativeWorker.test.mjs.map
|