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