@vxrn/vite-plugin-metro 2.0.0-beta.53.1 → 2.0.0-beta.56.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/index.cjs +0 -2
- package/dist/cjs/index.native.cjs +0 -2
- package/dist/cjs/index.native.js +0 -2
- package/dist/cjs/index.native.js.map +1 -1
- package/dist/cjs/metro-config/getMetroConfigFromViteConfig.cjs +48 -38
- package/dist/cjs/metro-config/getMetroConfigFromViteConfig.native.cjs +62 -42
- package/dist/cjs/metro-config/getMetroConfigFromViteConfig.native.js +62 -42
- package/dist/cjs/metro-config/getMetroConfigFromViteConfig.native.js.map +1 -1
- package/dist/cjs/transformer/babel-transformer.cjs +53 -4
- package/dist/cjs/transformer/babel-transformer.native.cjs +65 -6
- package/dist/cjs/transformer/babel-transformer.native.js +65 -6
- package/dist/cjs/transformer/babel-transformer.native.js.map +1 -1
- package/dist/cjs/transformer/expoVirtualEnv.cjs +92 -0
- package/dist/cjs/transformer/expoVirtualEnv.native.cjs +113 -0
- package/dist/cjs/transformer/expoVirtualEnv.native.js +115 -0
- package/dist/cjs/transformer/expoVirtualEnv.native.js.map +1 -0
- package/dist/cjs/transformer/loadBabelConfig.cjs +1 -1
- package/dist/cjs/transformer/loadBabelConfig.native.cjs +1 -1
- package/dist/cjs/transformer/loadBabelConfig.native.js +1 -1
- package/dist/cjs/transformer/loadBabelConfig.native.js.map +1 -1
- package/dist/cjs/transformer/metroNativeWorker.cjs +8 -20
- package/dist/cjs/transformer/metroNativeWorker.native.cjs +8 -20
- package/dist/cjs/transformer/metroNativeWorker.native.js +8 -20
- package/dist/cjs/transformer/metroNativeWorker.native.js.map +1 -1
- package/dist/cjs/transformer/transformSync.cjs +3 -3
- package/dist/cjs/transformer/transformSync.native.cjs +3 -3
- package/dist/cjs/transformer/transformSync.native.js +3 -3
- package/dist/cjs/transformer/transformSync.native.js.map +1 -1
- package/dist/cjs/utils/exit.cjs +1 -1
- package/dist/cjs/utils/exit.native.cjs +1 -1
- package/dist/cjs/utils/exit.native.js +1 -1
- package/dist/cjs/utils/exit.native.js.map +1 -1
- package/dist/esm/index.js +1 -2
- package/dist/esm/index.mjs +1 -2
- package/dist/esm/index.native.js +1 -2
- package/dist/esm/metro-config/getMetroConfigFromViteConfig.mjs +48 -38
- package/dist/esm/metro-config/getMetroConfigFromViteConfig.mjs.map +1 -1
- package/dist/esm/metro-config/getMetroConfigFromViteConfig.native.js +62 -42
- package/dist/esm/metro-config/getMetroConfigFromViteConfig.native.js.map +1 -1
- package/dist/esm/transformer/babel-transformer.mjs +133 -96
- package/dist/esm/transformer/babel-transformer.mjs.map +1 -1
- package/dist/esm/transformer/babel-transformer.native.js +147 -103
- package/dist/esm/transformer/babel-transformer.native.js.map +1 -1
- package/dist/esm/transformer/expoVirtualEnv.mjs +67 -0
- package/dist/esm/transformer/expoVirtualEnv.mjs.map +1 -0
- package/dist/esm/transformer/expoVirtualEnv.native.js +86 -0
- package/dist/esm/transformer/expoVirtualEnv.native.js.map +1 -0
- package/dist/esm/transformer/loadBabelConfig.mjs +1 -1
- package/dist/esm/transformer/loadBabelConfig.mjs.map +1 -1
- package/dist/esm/transformer/loadBabelConfig.native.js +1 -1
- package/dist/esm/transformer/loadBabelConfig.native.js.map +1 -1
- package/dist/esm/transformer/metroNativeWorker.mjs +8 -20
- package/dist/esm/transformer/metroNativeWorker.mjs.map +1 -1
- package/dist/esm/transformer/metroNativeWorker.native.js +8 -20
- package/dist/esm/transformer/metroNativeWorker.native.js.map +1 -1
- package/dist/esm/transformer/transformSync.mjs +3 -3
- package/dist/esm/transformer/transformSync.mjs.map +1 -1
- package/dist/esm/transformer/transformSync.native.js +3 -3
- package/dist/esm/transformer/transformSync.native.js.map +1 -1
- package/dist/esm/utils/exit.mjs +1 -1
- package/dist/esm/utils/exit.mjs.map +1 -1
- package/dist/esm/utils/exit.native.js +1 -1
- package/dist/esm/utils/exit.native.js.map +1 -1
- package/package.json +7 -9
- package/src/index.ts +0 -2
- package/src/metro-config/getMetroConfigFromViteConfig.test.ts +5 -0
- package/src/metro-config/getMetroConfigFromViteConfig.ts +81 -96
- package/src/plugins/metroPlugin.ts +3 -6
- package/src/transformer/babel-transformer.test.ts +118 -0
- package/src/transformer/babel-transformer.ts +82 -15
- package/src/transformer/expoVirtualEnv.integration.test.ts +106 -0
- package/src/transformer/expoVirtualEnv.test.ts +72 -0
- package/src/transformer/expoVirtualEnv.ts +115 -0
- package/src/transformer/loadBabelConfig.ts +1 -1
- package/src/transformer/metroNativeWorker.test.ts +105 -1
- package/src/transformer/metroNativeWorker.ts +12 -33
- package/src/transformer/transformSync.test.ts +22 -0
- package/src/transformer/transformSync.ts +4 -4
- package/src/utils/exit.ts +2 -2
- package/src/utils/getTerminalReporter.ts +1 -1
- package/src/utils/projectImport.ts +2 -2
- package/types/index.d.ts +0 -2
- package/types/index.d.ts.map +1 -1
- package/types/metro-config/getMetroConfigFromViteConfig.d.ts +2 -2
- package/types/metro-config/getMetroConfigFromViteConfig.d.ts.map +1 -1
- package/types/plugins/metroPlugin.d.ts +2 -4
- package/types/plugins/metroPlugin.d.ts.map +1 -1
- package/types/transformer/babel-transformer.d.ts +10 -1
- package/types/transformer/babel-transformer.d.ts.map +1 -1
- package/types/transformer/expoVirtualEnv.d.ts +8 -0
- package/types/transformer/expoVirtualEnv.d.ts.map +1 -0
- package/types/transformer/metroNativeWorker.d.ts.map +1 -1
- package/types/utils/projectImport.d.ts +2 -2
- package/dist/cjs/plugins/expoManifestRequestHandlerPlugin.cjs +0 -59
- package/dist/cjs/plugins/expoManifestRequestHandlerPlugin.native.cjs +0 -62
- package/dist/cjs/plugins/expoManifestRequestHandlerPlugin.native.js +0 -64
- package/dist/cjs/plugins/expoManifestRequestHandlerPlugin.native.js.map +0 -1
- package/dist/cjs/utils/patchExpoGoManifestHandlerMiddlewareWithCustomMainModuleName.cjs +0 -35
- package/dist/cjs/utils/patchExpoGoManifestHandlerMiddlewareWithCustomMainModuleName.native.cjs +0 -37
- package/dist/cjs/utils/patchExpoGoManifestHandlerMiddlewareWithCustomMainModuleName.native.js +0 -39
- package/dist/cjs/utils/patchExpoGoManifestHandlerMiddlewareWithCustomMainModuleName.native.js.map +0 -1
- package/dist/esm/plugins/expoManifestRequestHandlerPlugin.mjs +0 -40
- package/dist/esm/plugins/expoManifestRequestHandlerPlugin.mjs.map +0 -1
- package/dist/esm/plugins/expoManifestRequestHandlerPlugin.native.js +0 -41
- package/dist/esm/plugins/expoManifestRequestHandlerPlugin.native.js.map +0 -1
- package/dist/esm/utils/patchExpoGoManifestHandlerMiddlewareWithCustomMainModuleName.mjs +0 -15
- package/dist/esm/utils/patchExpoGoManifestHandlerMiddlewareWithCustomMainModuleName.mjs.map +0 -1
- package/dist/esm/utils/patchExpoGoManifestHandlerMiddlewareWithCustomMainModuleName.native.js +0 -15
- package/dist/esm/utils/patchExpoGoManifestHandlerMiddlewareWithCustomMainModuleName.native.js.map +0 -1
- package/src/plugins/expoManifestRequestHandlerPlugin.ts +0 -88
- package/src/utils/patchExpoGoManifestHandlerMiddlewareWithCustomMainModuleName.ts +0 -28
- package/types/plugins/expoManifestRequestHandlerPlugin.d.ts +0 -14
- package/types/plugins/expoManifestRequestHandlerPlugin.d.ts.map +0 -1
- package/types/utils/patchExpoGoManifestHandlerMiddlewareWithCustomMainModuleName.d.ts +0 -5
- package/types/utils/patchExpoGoManifestHandlerMiddlewareWithCustomMainModuleName.d.ts.map +0 -1
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import path from 'node:path'
|
|
2
|
+
import { fileURLToPath } from 'node:url'
|
|
3
|
+
import vm from 'node:vm'
|
|
4
|
+
import { describe, expect, it } from 'vitest'
|
|
5
|
+
|
|
6
|
+
import { transformFromAstSync } from './babel-core'
|
|
7
|
+
// @ts-expect-error: module.exports = has no default export in types, runtime interop provides it
|
|
8
|
+
import babelTransformer from './babel-transformer'
|
|
9
|
+
|
|
10
|
+
const fixturesDir = path.join(
|
|
11
|
+
path.dirname(fileURLToPath(import.meta.url)),
|
|
12
|
+
'__fixtures__'
|
|
13
|
+
)
|
|
14
|
+
const virtualFilename = '/proj/node_modules/expo/virtual/env.js'
|
|
15
|
+
const virtualSource = 'export const env = process.env;'
|
|
16
|
+
|
|
17
|
+
function runBabelTransform(src: string, filename: string, dev: boolean): any {
|
|
18
|
+
// the transformer leaks BABEL_ENV=development when it was previously
|
|
19
|
+
// unset; keep that pre-existing leak out of sibling tests.
|
|
20
|
+
const OLD_BABEL_ENV = process.env.BABEL_ENV
|
|
21
|
+
try {
|
|
22
|
+
return (babelTransformer as any).transform({
|
|
23
|
+
filename,
|
|
24
|
+
src,
|
|
25
|
+
options: {
|
|
26
|
+
projectRoot: fixturesDir,
|
|
27
|
+
dev,
|
|
28
|
+
platform: 'ios',
|
|
29
|
+
customTransformOptions: {
|
|
30
|
+
environment: 'client',
|
|
31
|
+
vite: {
|
|
32
|
+
oneViteMetroBabelConfig: true,
|
|
33
|
+
babelConfig: {
|
|
34
|
+
babelrc: false,
|
|
35
|
+
configFile: false,
|
|
36
|
+
presets: [],
|
|
37
|
+
plugins: [],
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
plugins: [],
|
|
43
|
+
})
|
|
44
|
+
} finally {
|
|
45
|
+
if (OLD_BABEL_ENV === undefined) {
|
|
46
|
+
delete process.env.BABEL_ENV
|
|
47
|
+
} else {
|
|
48
|
+
process.env.BABEL_ENV = OLD_BABEL_ENV
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function codegen(ast: any, filename: string): string {
|
|
54
|
+
const out = transformFromAstSync(ast, virtualSource, {
|
|
55
|
+
filename,
|
|
56
|
+
babelrc: false,
|
|
57
|
+
configFile: false,
|
|
58
|
+
code: true,
|
|
59
|
+
ast: false,
|
|
60
|
+
})
|
|
61
|
+
if (!out?.code) {
|
|
62
|
+
throw new Error('codegen produced no code')
|
|
63
|
+
}
|
|
64
|
+
return out.code
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function runCjs(
|
|
68
|
+
code: string,
|
|
69
|
+
opts: { requireImpl: (...args: any[]) => any; procEnv: Record<string, string> }
|
|
70
|
+
) {
|
|
71
|
+
const module = { exports: {} as any }
|
|
72
|
+
vm.runInNewContext(code, {
|
|
73
|
+
require: opts.requireImpl,
|
|
74
|
+
module,
|
|
75
|
+
exports: module.exports,
|
|
76
|
+
process: { env: opts.procEnv },
|
|
77
|
+
console,
|
|
78
|
+
})
|
|
79
|
+
return module.exports
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const noRequire = () => {
|
|
83
|
+
throw new Error('unexpected require call')
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
describe('babel-transformer optional expo virtual env', () => {
|
|
87
|
+
it('merges .env files over process.env in dev like upstream', () => {
|
|
88
|
+
const result = runBabelTransform(virtualSource, virtualFilename, true)
|
|
89
|
+
expect(result.ast).toBeTruthy()
|
|
90
|
+
const code = codegen(result.ast, virtualFilename)
|
|
91
|
+
expect(code).toContain('require.context')
|
|
92
|
+
expect(code).toContain('.env.development.local')
|
|
93
|
+
const contextModule: any = () => ({
|
|
94
|
+
default: { EXPO_PUBLIC_SENTINEL_DOTENV: 'dotenv-sentinel-value' },
|
|
95
|
+
})
|
|
96
|
+
contextModule.keys = () => ['./.env']
|
|
97
|
+
const requireImpl: any = noRequire
|
|
98
|
+
requireImpl.context = () => contextModule
|
|
99
|
+
const mod = runCjs(code, {
|
|
100
|
+
requireImpl,
|
|
101
|
+
procEnv: { EXPO_PUBLIC_SENTINEL_PROC: 'proc-sentinel-value' },
|
|
102
|
+
})
|
|
103
|
+
expect(mod.env.EXPO_PUBLIC_SENTINEL_DOTENV).toBe('dotenv-sentinel-value')
|
|
104
|
+
expect(mod.env.EXPO_PUBLIC_SENTINEL_PROC).toBe('proc-sentinel-value')
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
it('throws a naming error in production like upstream', () => {
|
|
108
|
+
const result = runBabelTransform(virtualSource, virtualFilename, false)
|
|
109
|
+
expect(result.ast).toBeTruthy()
|
|
110
|
+
const mod = runCjs(codegen(result.ast, virtualFilename), {
|
|
111
|
+
requireImpl: noRequire,
|
|
112
|
+
procEnv: {},
|
|
113
|
+
})
|
|
114
|
+
expect(() => mod.env.EXPO_PUBLIC_ANYTHING).toThrow(
|
|
115
|
+
'not supported in production bundles'
|
|
116
|
+
)
|
|
117
|
+
})
|
|
118
|
+
})
|
|
@@ -5,20 +5,27 @@
|
|
|
5
5
|
* This source code is licensed under the MIT license found in the
|
|
6
6
|
* LICENSE file in the root directory of this source tree.
|
|
7
7
|
*/
|
|
8
|
-
//
|
|
9
|
-
//
|
|
8
|
+
// a fork of the upstream babel-transformer that adds support for web and node
|
|
9
|
+
// environments via `isServer` on the babel caller.
|
|
10
10
|
// See:
|
|
11
11
|
// * https://github.com/facebook/metro/blob/main/packages/metro-babel-transformer/src/index.js
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
import type {
|
|
13
|
+
BabelTransformer,
|
|
14
|
+
BabelTransformerArgs,
|
|
15
|
+
BabelTransformerCacheKeyOptions,
|
|
16
|
+
} from 'metro-babel-transformer'
|
|
14
17
|
import assert from 'node:assert'
|
|
18
|
+
import { createHash } from 'node:crypto'
|
|
19
|
+
import { readFileSync } from 'node:fs'
|
|
20
|
+
import path from 'node:path'
|
|
15
21
|
|
|
16
22
|
import type { TransformOptions } from './babel-core'
|
|
23
|
+
import { substituteExpoVirtualEnvSource } from './expoVirtualEnv'
|
|
17
24
|
import { loadBabelConfig } from './loadBabelConfig'
|
|
18
25
|
import { transformSync } from './transformSync'
|
|
19
26
|
import type { ViteCustomTransformOptions } from './types'
|
|
20
27
|
|
|
21
|
-
export type
|
|
28
|
+
export type MetroBabelCaller = TransformOptions['caller'] & {
|
|
22
29
|
supportsReactCompiler?: boolean
|
|
23
30
|
isReactServer?: boolean
|
|
24
31
|
isHMREnabled?: boolean
|
|
@@ -37,7 +44,7 @@ export type ExpoBabelCaller = TransformOptions['caller'] & {
|
|
|
37
44
|
}
|
|
38
45
|
|
|
39
46
|
const debug = require('debug')(
|
|
40
|
-
'
|
|
47
|
+
'vxrn:metro-config:babel-transformer'
|
|
41
48
|
) as typeof console.log
|
|
42
49
|
|
|
43
50
|
function isCustomTruthy(value: any): boolean {
|
|
@@ -67,7 +74,7 @@ function getBabelCaller({
|
|
|
67
74
|
oneViteMetroBabelConfig,
|
|
68
75
|
}: Pick<BabelTransformerArgs, 'filename' | 'options'> & {
|
|
69
76
|
oneViteMetroBabelConfig: boolean
|
|
70
|
-
}):
|
|
77
|
+
}): MetroBabelCaller {
|
|
71
78
|
const isNodeModule = filename.includes('node_modules')
|
|
72
79
|
const isReactServer = options.customTransformOptions?.environment === 'react-server'
|
|
73
80
|
const isGenericServer = options.customTransformOptions?.environment === 'node'
|
|
@@ -80,7 +87,7 @@ function getBabelCaller({
|
|
|
80
87
|
|
|
81
88
|
if (routerRoot == null) {
|
|
82
89
|
memoizeWarning(
|
|
83
|
-
'Warning: Missing transform.routerRoot option in Metro bundling request, falling back to `app` as routes directory.
|
|
90
|
+
'Warning: Missing transform.routerRoot option in Metro bundling request, falling back to `app` as routes directory.'
|
|
84
91
|
)
|
|
85
92
|
}
|
|
86
93
|
|
|
@@ -119,7 +126,7 @@ function getBabelCaller({
|
|
|
119
126
|
// target environment.
|
|
120
127
|
engine: stringOrUndefined(options.customTransformOptions?.engine),
|
|
121
128
|
|
|
122
|
-
//
|
|
129
|
+
// provide the project root for reading the app babel config
|
|
123
130
|
projectRoot: options.projectRoot,
|
|
124
131
|
oneViteMetroBabelConfig,
|
|
125
132
|
|
|
@@ -146,17 +153,28 @@ function stringOrUndefined(value: unknown): string | undefined {
|
|
|
146
153
|
|
|
147
154
|
const transform: BabelTransformer['transform'] = ({
|
|
148
155
|
filename,
|
|
149
|
-
src,
|
|
156
|
+
src: originalSrc,
|
|
150
157
|
options,
|
|
151
|
-
// `plugins` is used for `functionMapBabelPlugin` from `metro-source-map`.
|
|
158
|
+
// `plugins` is used for `functionMapBabelPlugin` from `metro-source-map`.
|
|
152
159
|
plugins,
|
|
153
160
|
}: BabelTransformerArgs): ReturnType<BabelTransformer['transform']> => {
|
|
161
|
+
// narrow optional Expo compatibility (expo/virtual/env.js and .env files
|
|
162
|
+
// only), shared with the native worker. no-Expo apps pass through byte
|
|
163
|
+
// for byte without resolving any Expo module.
|
|
164
|
+
const src = substituteExpoVirtualEnvSource({
|
|
165
|
+
filename,
|
|
166
|
+
src: originalSrc,
|
|
167
|
+
projectRoot: options.projectRoot,
|
|
168
|
+
dev: options.dev,
|
|
169
|
+
environment: options.customTransformOptions?.environment,
|
|
170
|
+
})
|
|
171
|
+
|
|
154
172
|
const viteCustomTransformOptions = options.customTransformOptions?.vite
|
|
155
173
|
|
|
156
174
|
const customOptionsFromVite: ViteCustomTransformOptions = (() => {
|
|
157
175
|
const c: any = viteCustomTransformOptions
|
|
158
|
-
//
|
|
159
|
-
// customTransformOptions.vite
|
|
176
|
+
// standalone Metro invocations don't set
|
|
177
|
+
// customTransformOptions.vite. the plugins flow entirely through the
|
|
160
178
|
// project's babel.config.cjs in that case. Tolerate the missing field
|
|
161
179
|
// rather than throwing so a single Metro config can serve both the
|
|
162
180
|
// Vite-driven and standalone paths.
|
|
@@ -210,8 +228,8 @@ const transform: BabelTransformer['transform'] = ({
|
|
|
210
228
|
// all (most) of the transforms in their local Babel config.
|
|
211
229
|
// This also helps us keep the transform layers small and focused on a single task. We can also use this to
|
|
212
230
|
// ensure the Babel config caching is more accurate.
|
|
213
|
-
//
|
|
214
|
-
//
|
|
231
|
+
// moving everything babel-related to the preset also keeps the transform
|
|
232
|
+
// requirements explicit for users who opt into babel.
|
|
215
233
|
caller: getBabelCaller({
|
|
216
234
|
filename,
|
|
217
235
|
options,
|
|
@@ -238,8 +256,57 @@ const transform: BabelTransformer['transform'] = ({
|
|
|
238
256
|
}
|
|
239
257
|
}
|
|
240
258
|
|
|
259
|
+
export function getCacheKey(options?: BabelTransformerCacheKeyOptions): string {
|
|
260
|
+
const hash = createHash('sha256')
|
|
261
|
+
hash.update('vxrn-metro-babel-transformer-v1')
|
|
262
|
+
|
|
263
|
+
const projectRoot = options?.projectRoot
|
|
264
|
+
if (!projectRoot) {
|
|
265
|
+
return hash.digest('hex')
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
hash.update(projectRoot)
|
|
269
|
+
|
|
270
|
+
for (const file of [
|
|
271
|
+
'.env',
|
|
272
|
+
'.env.development',
|
|
273
|
+
'.env.local',
|
|
274
|
+
'.env.development.local',
|
|
275
|
+
]) {
|
|
276
|
+
const filename = path.join(projectRoot, file)
|
|
277
|
+
hash.update(filename)
|
|
278
|
+
try {
|
|
279
|
+
hash.update(readFileSync(filename))
|
|
280
|
+
} catch (error) {
|
|
281
|
+
if (
|
|
282
|
+
typeof error === 'object' &&
|
|
283
|
+
error !== null &&
|
|
284
|
+
'code' in error &&
|
|
285
|
+
error.code === 'ENOENT'
|
|
286
|
+
) {
|
|
287
|
+
continue
|
|
288
|
+
}
|
|
289
|
+
throw error
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
hash.update(
|
|
294
|
+
JSON.stringify(
|
|
295
|
+
Object.entries(process.env)
|
|
296
|
+
.filter(
|
|
297
|
+
([key]) =>
|
|
298
|
+
key.startsWith('ONE_PUBLIC_') || key.startsWith('EXPO_PUBLIC_')
|
|
299
|
+
)
|
|
300
|
+
.sort(([left], [right]) => left.localeCompare(right))
|
|
301
|
+
)
|
|
302
|
+
)
|
|
303
|
+
|
|
304
|
+
return hash.digest('hex')
|
|
305
|
+
}
|
|
306
|
+
|
|
241
307
|
const babelTransformer: BabelTransformer = {
|
|
242
308
|
transform,
|
|
309
|
+
getCacheKey,
|
|
243
310
|
}
|
|
244
311
|
|
|
245
312
|
module.exports = babelTransformer
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { execFileSync } from "node:child_process";
|
|
2
|
+
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { afterAll, describe, expect, it } from "vitest";
|
|
5
|
+
|
|
6
|
+
const workspaceRoot = path.resolve(__dirname, "../../../..");
|
|
7
|
+
const metroBin = path.join(workspaceRoot, "node_modules/.bin/metro");
|
|
8
|
+
const fixtureDirs: string[] = [];
|
|
9
|
+
|
|
10
|
+
afterAll(() => {
|
|
11
|
+
for (const fixtureDir of fixtureDirs) {
|
|
12
|
+
rmSync(fixtureDir, { recursive: true, force: true });
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
describe("optional Expo env through One Metro", () => {
|
|
17
|
+
it("executes babel-preset-expo env references in development and production", () => {
|
|
18
|
+
const fixtureDir = mkdtempSync(path.join(workspaceRoot, "tmp-expo-env-"));
|
|
19
|
+
fixtureDirs.push(fixtureDir);
|
|
20
|
+
|
|
21
|
+
writeFileSync(
|
|
22
|
+
path.join(fixtureDir, "package.json"),
|
|
23
|
+
JSON.stringify({ name: "one-expo-env-fixture", private: true }),
|
|
24
|
+
);
|
|
25
|
+
writeFileSync(path.join(fixtureDir, ".watchmanconfig"), "{}\n");
|
|
26
|
+
writeFileSync(
|
|
27
|
+
path.join(fixtureDir, "tsconfig.json"),
|
|
28
|
+
JSON.stringify({ compilerOptions: { baseUrl: ".", paths: {} } }),
|
|
29
|
+
);
|
|
30
|
+
writeFileSync(
|
|
31
|
+
path.join(fixtureDir, "babel.config.cjs"),
|
|
32
|
+
`module.exports = { presets: ['babel-preset-expo'] }\n`,
|
|
33
|
+
);
|
|
34
|
+
writeFileSync(
|
|
35
|
+
path.join(fixtureDir, "metro.config.cjs"),
|
|
36
|
+
`const { withOne } = require('one/metro-config')
|
|
37
|
+
module.exports = withOne(__dirname, { loadViteConfig: false })
|
|
38
|
+
`,
|
|
39
|
+
);
|
|
40
|
+
writeFileSync(
|
|
41
|
+
path.join(fixtureDir, ".env"),
|
|
42
|
+
"EXPO_PUBLIC_SENTINEL=development-dotenv-value\nPRIVATE_SENTINEL=private-value\n",
|
|
43
|
+
);
|
|
44
|
+
writeFileSync(
|
|
45
|
+
path.join(fixtureDir, "index.js"),
|
|
46
|
+
`console.log('observed:' + process.env.EXPO_PUBLIC_SENTINEL)\n`,
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
const bundle = (dev: boolean, sentinel: string, resetCache = false) => {
|
|
50
|
+
const bundlePath = path.join(
|
|
51
|
+
fixtureDir,
|
|
52
|
+
`bundle.${dev ? "dev" : "prod"}.js`,
|
|
53
|
+
);
|
|
54
|
+
execFileSync(
|
|
55
|
+
metroBin,
|
|
56
|
+
[
|
|
57
|
+
"build",
|
|
58
|
+
"index.js",
|
|
59
|
+
"--config",
|
|
60
|
+
"metro.config.cjs",
|
|
61
|
+
"--platform",
|
|
62
|
+
"ios",
|
|
63
|
+
"--dev",
|
|
64
|
+
String(dev),
|
|
65
|
+
"--minify",
|
|
66
|
+
"false",
|
|
67
|
+
"--out",
|
|
68
|
+
bundlePath,
|
|
69
|
+
...(resetCache ? ["--reset-cache"] : []),
|
|
70
|
+
],
|
|
71
|
+
{
|
|
72
|
+
cwd: fixtureDir,
|
|
73
|
+
encoding: "utf8",
|
|
74
|
+
env: { ...process.env, EXPO_PUBLIC_SENTINEL: sentinel },
|
|
75
|
+
timeout: 180_000,
|
|
76
|
+
},
|
|
77
|
+
);
|
|
78
|
+
return {
|
|
79
|
+
code: readFileSync(bundlePath, "utf8"),
|
|
80
|
+
output: execFileSync(process.execPath, [bundlePath], {
|
|
81
|
+
cwd: fixtureDir,
|
|
82
|
+
encoding: "utf8",
|
|
83
|
+
}),
|
|
84
|
+
};
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
const development = bundle(true, "development-process-value", true);
|
|
88
|
+
expect(development.code).toContain("expo/virtual/env.js");
|
|
89
|
+
expect(development.output).toContain("observed:development-dotenv-value");
|
|
90
|
+
expect(development.code).not.toContain("private-value");
|
|
91
|
+
|
|
92
|
+
writeFileSync(
|
|
93
|
+
path.join(fixtureDir, ".env"),
|
|
94
|
+
"EXPO_PUBLIC_SENTINEL=updated-dotenv-value\nPRIVATE_SENTINEL=updated-private-value\n",
|
|
95
|
+
);
|
|
96
|
+
const updatedDevelopment = bundle(true, "updated-process-value");
|
|
97
|
+
expect(updatedDevelopment.output).toContain(
|
|
98
|
+
"observed:updated-dotenv-value",
|
|
99
|
+
);
|
|
100
|
+
expect(updatedDevelopment.code).not.toContain("updated-private-value");
|
|
101
|
+
|
|
102
|
+
const production = bundle(false, "production-process-value");
|
|
103
|
+
expect(production.code).not.toContain("expo/virtual/env.js");
|
|
104
|
+
expect(production.output).toContain("observed:production-process-value");
|
|
105
|
+
}, 180_000);
|
|
106
|
+
});
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import { substituteExpoVirtualEnvSource } from './expoVirtualEnv'
|
|
4
|
+
|
|
5
|
+
const virtualFilename = '/proj/node_modules/expo/virtual/env.js'
|
|
6
|
+
const virtualSource = 'export const env = process.env;'
|
|
7
|
+
|
|
8
|
+
describe('substituteExpoVirtualEnvSource', () => {
|
|
9
|
+
it('generates the require.context import in dev', () => {
|
|
10
|
+
const out = substituteExpoVirtualEnvSource({
|
|
11
|
+
filename: virtualFilename,
|
|
12
|
+
src: virtualSource,
|
|
13
|
+
projectRoot: '/proj',
|
|
14
|
+
dev: true,
|
|
15
|
+
environment: 'client',
|
|
16
|
+
})
|
|
17
|
+
expect(out).toContain('require.context("../../..",false,/^\\.\\/\\.env/)')
|
|
18
|
+
expect(out).toContain('.env.development.local')
|
|
19
|
+
expect(out).toContain('...process.env')
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
it('generates the naming-error proxy in production', () => {
|
|
23
|
+
const out = substituteExpoVirtualEnvSource({
|
|
24
|
+
filename: virtualFilename,
|
|
25
|
+
src: virtualSource,
|
|
26
|
+
projectRoot: '/proj',
|
|
27
|
+
dev: false,
|
|
28
|
+
environment: 'client',
|
|
29
|
+
})
|
|
30
|
+
expect(out).toContain('new Proxy')
|
|
31
|
+
expect(out).toContain('not supported in production bundles')
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
it('leaves the virtual module alone outside client environments', () => {
|
|
35
|
+
for (const environment of ['node', 'react-server']) {
|
|
36
|
+
expect(
|
|
37
|
+
substituteExpoVirtualEnvSource({
|
|
38
|
+
filename: virtualFilename,
|
|
39
|
+
src: virtualSource,
|
|
40
|
+
projectRoot: '/proj',
|
|
41
|
+
dev: true,
|
|
42
|
+
environment,
|
|
43
|
+
})
|
|
44
|
+
).toBe(virtualSource)
|
|
45
|
+
}
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
it('leaves ordinary files alone', () => {
|
|
49
|
+
expect(
|
|
50
|
+
substituteExpoVirtualEnvSource({
|
|
51
|
+
filename: '/proj/src/app.js',
|
|
52
|
+
src: 'const a = 1',
|
|
53
|
+
projectRoot: '/proj',
|
|
54
|
+
dev: true,
|
|
55
|
+
environment: 'client',
|
|
56
|
+
})
|
|
57
|
+
).toBe('const a = 1')
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
it('leaves .env files alone without Expo installed', () => {
|
|
61
|
+
const src = 'EXPO_PUBLIC_A=1\nSECRET=2\n'
|
|
62
|
+
expect(
|
|
63
|
+
substituteExpoVirtualEnvSource({
|
|
64
|
+
filename: '/proj/.env',
|
|
65
|
+
src,
|
|
66
|
+
projectRoot: '/proj-no-expo',
|
|
67
|
+
dev: true,
|
|
68
|
+
environment: 'client',
|
|
69
|
+
})
|
|
70
|
+
).toBe(src)
|
|
71
|
+
})
|
|
72
|
+
})
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { createRequire } from 'node:module'
|
|
2
|
+
import { readFileSync } from 'node:fs'
|
|
3
|
+
import path from 'node:path'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* narrow optional Expo compatibility shared by the native worker and the
|
|
7
|
+
* babel transformer, mirroring @expo/metro-config's transform worker: only
|
|
8
|
+
* expo/virtual/env.js and .env files are special, and everything else
|
|
9
|
+
* passes through untouched. apps without Expo installed never resolve or
|
|
10
|
+
* load any Expo module: the virtual file cannot match without the expo
|
|
11
|
+
* package, and .env files are left alone when the parser is absent.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
type ParseEnvFile = (src: string, isClient: boolean) => Record<string, string>
|
|
15
|
+
|
|
16
|
+
function loadExpoParseEnvFile(projectRoot: string): ParseEnvFile | null {
|
|
17
|
+
try {
|
|
18
|
+
const requireFromProject = createRequire(path.join(projectRoot, 'package.json'))
|
|
19
|
+
const expoMetroConfigPkg = requireFromProject.resolve(
|
|
20
|
+
'@expo/metro-config/package.json'
|
|
21
|
+
)
|
|
22
|
+
const dotEnvDevelopment = requireFromProject(
|
|
23
|
+
path.join(
|
|
24
|
+
path.dirname(expoMetroConfigPkg),
|
|
25
|
+
'build',
|
|
26
|
+
'transform-worker',
|
|
27
|
+
'dot-env-development'
|
|
28
|
+
)
|
|
29
|
+
) as { parseEnvFile?: ParseEnvFile }
|
|
30
|
+
return dotEnvDevelopment?.parseEnvFile ?? null
|
|
31
|
+
} catch {
|
|
32
|
+
return null
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const virtualEnvPattern = /[\\/]expo[\\/]virtual[\\/]env\.js$/
|
|
37
|
+
const dotEnvPattern = /(^|[\\/])\.env(\.(local|(development|production)(\.local)?))?$/
|
|
38
|
+
const developmentEnvFiles = [
|
|
39
|
+
'.env',
|
|
40
|
+
'.env.development',
|
|
41
|
+
'.env.local',
|
|
42
|
+
'.env.development.local',
|
|
43
|
+
]
|
|
44
|
+
|
|
45
|
+
export function substituteExpoVirtualEnvSource({
|
|
46
|
+
filename,
|
|
47
|
+
src,
|
|
48
|
+
projectRoot,
|
|
49
|
+
dev,
|
|
50
|
+
environment,
|
|
51
|
+
}: {
|
|
52
|
+
filename: string
|
|
53
|
+
src: string
|
|
54
|
+
projectRoot: string
|
|
55
|
+
dev: boolean | undefined
|
|
56
|
+
environment: unknown
|
|
57
|
+
}): string {
|
|
58
|
+
const isClientEnvironment = environment !== 'node' && environment !== 'react-server'
|
|
59
|
+
|
|
60
|
+
// parsing the virtual env is client-only, on the server process.env is used directly.
|
|
61
|
+
if (isClientEnvironment && virtualEnvPattern.test(filename)) {
|
|
62
|
+
if (dev) {
|
|
63
|
+
// variables are merged at runtime so HMR can pick up .env edits.
|
|
64
|
+
// standard Metro's Node crawler does not index `.env` because the file
|
|
65
|
+
// has no extension, so seed the module with the same public-only merge.
|
|
66
|
+
// context modules override this snapshot when the active watcher exposes
|
|
67
|
+
// the files, preserving Expo's live-update path.
|
|
68
|
+
const fileEnv: Record<string, string> = {}
|
|
69
|
+
const parseEnvFile = loadExpoParseEnvFile(projectRoot)
|
|
70
|
+
if (parseEnvFile) {
|
|
71
|
+
for (const file of developmentEnvFiles) {
|
|
72
|
+
try {
|
|
73
|
+
Object.assign(
|
|
74
|
+
fileEnv,
|
|
75
|
+
parseEnvFile(readFileSync(path.join(projectRoot, file), 'utf8'), true)
|
|
76
|
+
)
|
|
77
|
+
} catch (error) {
|
|
78
|
+
if (
|
|
79
|
+
typeof error === 'object' &&
|
|
80
|
+
error !== null &&
|
|
81
|
+
'code' in error &&
|
|
82
|
+
error.code === 'ENOENT'
|
|
83
|
+
) {
|
|
84
|
+
continue
|
|
85
|
+
}
|
|
86
|
+
throw error
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
const relativePath = path
|
|
91
|
+
.relative(path.dirname(filename), projectRoot)
|
|
92
|
+
.split(path.sep)
|
|
93
|
+
.join('/')
|
|
94
|
+
return `const dotEnvModules = require.context(${JSON.stringify(relativePath)},false,/^\\.\\/\\.env/);\n\nexport const env = { ...process.env, ...${JSON.stringify(fileEnv)}, ...${JSON.stringify(developmentEnvFiles)}.reduce((acc, file) => {\n const key = './' + file;\n return { ...acc, ...(dotEnvModules.keys().includes(key) ? dotEnvModules(key)?.default : {}) };\n}, {}) };`
|
|
95
|
+
}
|
|
96
|
+
// production inlines every value at its use site, so reaching this
|
|
97
|
+
// module at all is a bug worth naming rather than silently undefined.
|
|
98
|
+
return `\nexport const env = new Proxy({}, {\n get(target, key) {\n throw new Error(\`Attempting to access internal environment variable "\${key}" is not supported in production bundles. Environment variables should be inlined in production by Babel.\`);\n },\n});`
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (dotEnvPattern.test(filename)) {
|
|
102
|
+
// parse .env files to objects with Expo's own installed parser, keeping
|
|
103
|
+
// public names only in client environments. without Expo there is
|
|
104
|
+
// nothing to substitute: no preset generates the require.context import
|
|
105
|
+
// that would load a .env module, so the source passes through exactly
|
|
106
|
+
// as it did before this file existed.
|
|
107
|
+
const parseEnvFile = loadExpoParseEnvFile(projectRoot)
|
|
108
|
+
if (!parseEnvFile) {
|
|
109
|
+
return src
|
|
110
|
+
}
|
|
111
|
+
return `export default ${JSON.stringify(parseEnvFile(src, isClientEnvironment))};`
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return src
|
|
115
|
+
}
|
|
@@ -52,7 +52,7 @@ export const loadBabelConfig = (() => {
|
|
|
52
52
|
if (!babelRC.extends) {
|
|
53
53
|
babelRC.presets = [
|
|
54
54
|
// { plugins: [transformImportMetaGlobPlugin] }, // Added to support Vite's `import.meta.glob`
|
|
55
|
-
require('babel-preset
|
|
55
|
+
require('@react-native/babel-preset'),
|
|
56
56
|
]
|
|
57
57
|
}
|
|
58
58
|
|