metro-transform-worker 0.70.2 → 0.71.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/package.json +11 -11
- package/src/index.js +1 -1
- package/src/index.js.flow +11 -10
- package/src/__mocks__/index.js +0 -13
- package/src.real/__mocks__/index.js +0 -14
- package/src.real/__tests__/__snapshots__/index-test.js.snap +0 -329
- package/src.real/__tests__/index-test.js +0 -647
- package/src.real/index.js +0 -754
- package/src.real/utils/assetTransformer.js +0 -49
- package/src.real/utils/getMinifier.js +0 -32
|
@@ -1,647 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
-
*
|
|
4
|
-
* This source code is licensed under the MIT license found in the
|
|
5
|
-
* LICENSE file in the root directory of this source tree.
|
|
6
|
-
*
|
|
7
|
-
* @emails oncall+metro_bundler
|
|
8
|
-
* @flow strict-local
|
|
9
|
-
* @format
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
'use strict';
|
|
13
|
-
|
|
14
|
-
jest
|
|
15
|
-
.mock('../utils/getMinifier', () => () => ({code, map}) => ({
|
|
16
|
-
code: code.replace('arbitrary(code)', 'minified(code)'),
|
|
17
|
-
map,
|
|
18
|
-
}))
|
|
19
|
-
.mock('metro-transform-plugins', () => ({
|
|
20
|
-
...jest.requireActual('metro-transform-plugins'),
|
|
21
|
-
inlinePlugin: () => ({}),
|
|
22
|
-
constantFoldingPlugin: () => ({}),
|
|
23
|
-
}))
|
|
24
|
-
.mock('metro-minify-uglify');
|
|
25
|
-
|
|
26
|
-
import type {JsTransformerConfig} from '../index';
|
|
27
|
-
|
|
28
|
-
const HermesCompiler = require('metro-hermes-compiler');
|
|
29
|
-
const path = require('path');
|
|
30
|
-
|
|
31
|
-
const babelTransformerPath = require.resolve(
|
|
32
|
-
'metro-react-native-babel-transformer',
|
|
33
|
-
);
|
|
34
|
-
const transformerContents = (() =>
|
|
35
|
-
require('fs').readFileSync(babelTransformerPath))();
|
|
36
|
-
|
|
37
|
-
const HEADER_DEV =
|
|
38
|
-
'__d(function (global, _$$_REQUIRE, _$$_IMPORT_DEFAULT, _$$_IMPORT_ALL, module, exports, _dependencyMap) {';
|
|
39
|
-
const HEADER_PROD = '__d(function (g, r, i, a, m, e, d) {';
|
|
40
|
-
|
|
41
|
-
let fs;
|
|
42
|
-
let Transformer;
|
|
43
|
-
|
|
44
|
-
const baseConfig: JsTransformerConfig = {
|
|
45
|
-
allowOptionalDependencies: false,
|
|
46
|
-
assetPlugins: [],
|
|
47
|
-
assetRegistryPath: '',
|
|
48
|
-
asyncRequireModulePath: 'asyncRequire',
|
|
49
|
-
babelTransformerPath,
|
|
50
|
-
dynamicDepsInPackages: 'reject',
|
|
51
|
-
enableBabelRCLookup: false,
|
|
52
|
-
enableBabelRuntime: true,
|
|
53
|
-
experimentalImportBundleSupport: false,
|
|
54
|
-
globalPrefix: '',
|
|
55
|
-
hermesParser: false,
|
|
56
|
-
minifierConfig: {},
|
|
57
|
-
minifierPath: 'minifyModulePath',
|
|
58
|
-
optimizationSizeLimit: 100000,
|
|
59
|
-
publicPath: '/assets',
|
|
60
|
-
unstable_collectDependenciesPath:
|
|
61
|
-
'metro/src/ModuleGraph/worker/collectDependencies',
|
|
62
|
-
unstable_dependencyMapReservedName: null,
|
|
63
|
-
unstable_compactOutput: false,
|
|
64
|
-
unstable_disableModuleWrapping: false,
|
|
65
|
-
unstable_disableNormalizePseudoGlobals: false,
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
beforeEach(() => {
|
|
69
|
-
jest.resetModules();
|
|
70
|
-
|
|
71
|
-
jest.mock('fs', () => new (require('metro-memory-fs'))());
|
|
72
|
-
|
|
73
|
-
fs = require('fs');
|
|
74
|
-
Transformer = require('../');
|
|
75
|
-
fs.reset();
|
|
76
|
-
|
|
77
|
-
fs.mkdirSync('/root/local', {recursive: true});
|
|
78
|
-
fs.mkdirSync(path.dirname(babelTransformerPath), {recursive: true});
|
|
79
|
-
fs.writeFileSync(babelTransformerPath, transformerContents);
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
it('transforms a simple script', async () => {
|
|
83
|
-
const result = await Transformer.transform(
|
|
84
|
-
baseConfig,
|
|
85
|
-
'/root',
|
|
86
|
-
'local/file.js',
|
|
87
|
-
'someReallyArbitrary(code)',
|
|
88
|
-
{
|
|
89
|
-
dev: true,
|
|
90
|
-
type: 'script',
|
|
91
|
-
},
|
|
92
|
-
);
|
|
93
|
-
|
|
94
|
-
expect(result.output[0].type).toBe('js/script');
|
|
95
|
-
expect(result.output[0].data.code).toBe(
|
|
96
|
-
[
|
|
97
|
-
'(function (global) {',
|
|
98
|
-
' someReallyArbitrary(code);',
|
|
99
|
-
"})(typeof globalThis !== 'undefined' ? globalThis : typeof global !== 'undefined' ? global : typeof window !== 'undefined' ? window : this);",
|
|
100
|
-
].join('\n'),
|
|
101
|
-
);
|
|
102
|
-
expect(result.output[0].data.map).toMatchSnapshot();
|
|
103
|
-
expect(result.output[0].data.functionMap).toMatchSnapshot();
|
|
104
|
-
expect(result.dependencies).toEqual([]);
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
it('transforms a simple module', async () => {
|
|
108
|
-
const result = await Transformer.transform(
|
|
109
|
-
baseConfig,
|
|
110
|
-
'/root',
|
|
111
|
-
'local/file.js',
|
|
112
|
-
'arbitrary(code)',
|
|
113
|
-
{
|
|
114
|
-
dev: true,
|
|
115
|
-
type: 'module',
|
|
116
|
-
},
|
|
117
|
-
);
|
|
118
|
-
|
|
119
|
-
expect(result.output[0].type).toBe('js/module');
|
|
120
|
-
expect(result.output[0].data.code).toBe(
|
|
121
|
-
[HEADER_DEV, ' arbitrary(code);', '});'].join('\n'),
|
|
122
|
-
);
|
|
123
|
-
expect(result.output[0].data.map).toMatchSnapshot();
|
|
124
|
-
expect(result.output[0].data.functionMap).toMatchSnapshot();
|
|
125
|
-
expect(result.dependencies).toEqual([]);
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
it('transforms a module with dependencies', async () => {
|
|
129
|
-
const contents = [
|
|
130
|
-
'"use strict";',
|
|
131
|
-
'require("./a");',
|
|
132
|
-
'arbitrary(code);',
|
|
133
|
-
'const b = require("b");',
|
|
134
|
-
'import c from "./c";',
|
|
135
|
-
].join('\n');
|
|
136
|
-
|
|
137
|
-
const result = await Transformer.transform(
|
|
138
|
-
baseConfig,
|
|
139
|
-
'/root',
|
|
140
|
-
'local/file.js',
|
|
141
|
-
contents,
|
|
142
|
-
{
|
|
143
|
-
dev: true,
|
|
144
|
-
type: 'module',
|
|
145
|
-
},
|
|
146
|
-
);
|
|
147
|
-
|
|
148
|
-
expect(result.output[0].type).toBe('js/module');
|
|
149
|
-
expect(result.output[0].data.code).toBe(
|
|
150
|
-
[
|
|
151
|
-
HEADER_DEV,
|
|
152
|
-
' "use strict";',
|
|
153
|
-
'',
|
|
154
|
-
' var _interopRequireDefault = _$$_REQUIRE(_dependencyMap[0], "@babel/runtime/helpers/interopRequireDefault");',
|
|
155
|
-
'',
|
|
156
|
-
' var _c = _interopRequireDefault(_$$_REQUIRE(_dependencyMap[1], "./c"));',
|
|
157
|
-
'',
|
|
158
|
-
' _$$_REQUIRE(_dependencyMap[2], "./a");',
|
|
159
|
-
'',
|
|
160
|
-
' arbitrary(code);',
|
|
161
|
-
'',
|
|
162
|
-
' var b = _$$_REQUIRE(_dependencyMap[3], "b");',
|
|
163
|
-
'});',
|
|
164
|
-
].join('\n'),
|
|
165
|
-
);
|
|
166
|
-
expect(result.output[0].data.map).toMatchSnapshot();
|
|
167
|
-
expect(result.output[0].data.functionMap).toMatchSnapshot();
|
|
168
|
-
expect(result.dependencies).toEqual([
|
|
169
|
-
{
|
|
170
|
-
data: expect.objectContaining({asyncType: null}),
|
|
171
|
-
name: '@babel/runtime/helpers/interopRequireDefault',
|
|
172
|
-
},
|
|
173
|
-
{data: expect.objectContaining({asyncType: null}), name: './c'},
|
|
174
|
-
{data: expect.objectContaining({asyncType: null}), name: './a'},
|
|
175
|
-
{data: expect.objectContaining({asyncType: null}), name: 'b'},
|
|
176
|
-
]);
|
|
177
|
-
});
|
|
178
|
-
|
|
179
|
-
it('transforms an es module with asyncToGenerator', async () => {
|
|
180
|
-
const result = await Transformer.transform(
|
|
181
|
-
baseConfig,
|
|
182
|
-
'/root',
|
|
183
|
-
'local/file.js',
|
|
184
|
-
'export async function test() {}',
|
|
185
|
-
{
|
|
186
|
-
dev: true,
|
|
187
|
-
type: 'module',
|
|
188
|
-
},
|
|
189
|
-
);
|
|
190
|
-
|
|
191
|
-
expect(result.output[0].type).toBe('js/module');
|
|
192
|
-
expect(result.output[0].data.code).toMatchSnapshot();
|
|
193
|
-
expect(result.output[0].data.map).toHaveLength(6);
|
|
194
|
-
expect(result.output[0].data.functionMap).toMatchSnapshot();
|
|
195
|
-
expect(result.dependencies).toEqual([
|
|
196
|
-
{
|
|
197
|
-
data: expect.objectContaining({asyncType: null}),
|
|
198
|
-
name: '@babel/runtime/helpers/interopRequireDefault',
|
|
199
|
-
},
|
|
200
|
-
{
|
|
201
|
-
data: expect.objectContaining({asyncType: null}),
|
|
202
|
-
name: '@babel/runtime/helpers/asyncToGenerator',
|
|
203
|
-
},
|
|
204
|
-
]);
|
|
205
|
-
});
|
|
206
|
-
|
|
207
|
-
it('transforms async generators', async () => {
|
|
208
|
-
const result = await Transformer.transform(
|
|
209
|
-
baseConfig,
|
|
210
|
-
'/root',
|
|
211
|
-
'local/file.js',
|
|
212
|
-
'export async function* test() { yield "ok"; }',
|
|
213
|
-
{
|
|
214
|
-
dev: true,
|
|
215
|
-
type: 'module',
|
|
216
|
-
},
|
|
217
|
-
);
|
|
218
|
-
|
|
219
|
-
expect(result.output[0].data.code).toMatchSnapshot();
|
|
220
|
-
expect(result.dependencies).toEqual([
|
|
221
|
-
{
|
|
222
|
-
data: expect.objectContaining({asyncType: null}),
|
|
223
|
-
name: '@babel/runtime/helpers/interopRequireDefault',
|
|
224
|
-
},
|
|
225
|
-
{
|
|
226
|
-
data: expect.objectContaining({asyncType: null}),
|
|
227
|
-
name: '@babel/runtime/helpers/awaitAsyncGenerator',
|
|
228
|
-
},
|
|
229
|
-
{
|
|
230
|
-
data: expect.objectContaining({asyncType: null}),
|
|
231
|
-
name: '@babel/runtime/helpers/wrapAsyncGenerator',
|
|
232
|
-
},
|
|
233
|
-
]);
|
|
234
|
-
});
|
|
235
|
-
|
|
236
|
-
it('transforms import/export syntax when experimental flag is on', async () => {
|
|
237
|
-
const contents = ['import c from "./c";'].join('\n');
|
|
238
|
-
|
|
239
|
-
const result = await Transformer.transform(
|
|
240
|
-
baseConfig,
|
|
241
|
-
'/root',
|
|
242
|
-
'local/file.js',
|
|
243
|
-
contents,
|
|
244
|
-
{
|
|
245
|
-
dev: true,
|
|
246
|
-
experimentalImportSupport: true,
|
|
247
|
-
type: 'module',
|
|
248
|
-
},
|
|
249
|
-
);
|
|
250
|
-
|
|
251
|
-
expect(result.output[0].type).toBe('js/module');
|
|
252
|
-
expect(result.output[0].data.code).toBe(
|
|
253
|
-
[
|
|
254
|
-
HEADER_DEV,
|
|
255
|
-
' "use strict";',
|
|
256
|
-
'',
|
|
257
|
-
' var c = _$$_IMPORT_DEFAULT(_dependencyMap[0], "./c");',
|
|
258
|
-
'});',
|
|
259
|
-
].join('\n'),
|
|
260
|
-
);
|
|
261
|
-
expect(result.output[0].data.map).toMatchSnapshot();
|
|
262
|
-
expect(result.output[0].data.functionMap).toMatchSnapshot();
|
|
263
|
-
expect(result.dependencies).toEqual([
|
|
264
|
-
{
|
|
265
|
-
data: expect.objectContaining({
|
|
266
|
-
asyncType: null,
|
|
267
|
-
}),
|
|
268
|
-
name: './c',
|
|
269
|
-
},
|
|
270
|
-
]);
|
|
271
|
-
});
|
|
272
|
-
|
|
273
|
-
it('does not add "use strict" on non-modules', async () => {
|
|
274
|
-
const result = await Transformer.transform(
|
|
275
|
-
baseConfig,
|
|
276
|
-
'/root',
|
|
277
|
-
'node_modules/local/file.js',
|
|
278
|
-
'module.exports = {};',
|
|
279
|
-
{
|
|
280
|
-
dev: true,
|
|
281
|
-
experimentalImportSupport: true,
|
|
282
|
-
type: 'module',
|
|
283
|
-
},
|
|
284
|
-
);
|
|
285
|
-
|
|
286
|
-
expect(result.output[0].type).toBe('js/module');
|
|
287
|
-
expect(result.output[0].data.code).toBe(
|
|
288
|
-
[HEADER_DEV, ' module.exports = {};', '});'].join('\n'),
|
|
289
|
-
);
|
|
290
|
-
});
|
|
291
|
-
|
|
292
|
-
it('preserves require() calls when module wrapping is disabled', async () => {
|
|
293
|
-
const contents = ['require("./c");'].join('\n');
|
|
294
|
-
|
|
295
|
-
const result = await Transformer.transform(
|
|
296
|
-
{
|
|
297
|
-
...baseConfig,
|
|
298
|
-
unstable_disableModuleWrapping: true,
|
|
299
|
-
},
|
|
300
|
-
'/root',
|
|
301
|
-
'local/file.js',
|
|
302
|
-
contents,
|
|
303
|
-
{
|
|
304
|
-
dev: true,
|
|
305
|
-
type: 'module',
|
|
306
|
-
},
|
|
307
|
-
);
|
|
308
|
-
|
|
309
|
-
expect(result.output[0].type).toBe('js/module');
|
|
310
|
-
expect(result.output[0].data.code).toBe('require("./c");');
|
|
311
|
-
});
|
|
312
|
-
|
|
313
|
-
it('reports filename when encountering unsupported dynamic dependency', async () => {
|
|
314
|
-
const contents = [
|
|
315
|
-
'require("./a");',
|
|
316
|
-
'let a = arbitrary(code);',
|
|
317
|
-
'const b = require(a);',
|
|
318
|
-
].join('\n');
|
|
319
|
-
|
|
320
|
-
try {
|
|
321
|
-
await Transformer.transform(
|
|
322
|
-
baseConfig,
|
|
323
|
-
'/root',
|
|
324
|
-
'local/file.js',
|
|
325
|
-
contents,
|
|
326
|
-
{
|
|
327
|
-
dev: true,
|
|
328
|
-
type: 'module',
|
|
329
|
-
},
|
|
330
|
-
);
|
|
331
|
-
throw new Error('should not reach this');
|
|
332
|
-
} catch (error) {
|
|
333
|
-
expect(error.message).toMatchSnapshot();
|
|
334
|
-
}
|
|
335
|
-
});
|
|
336
|
-
|
|
337
|
-
it('supports dynamic dependencies from within `node_modules`', async () => {
|
|
338
|
-
expect(
|
|
339
|
-
(
|
|
340
|
-
await Transformer.transform(
|
|
341
|
-
{
|
|
342
|
-
...baseConfig,
|
|
343
|
-
dynamicDepsInPackages: 'throwAtRuntime',
|
|
344
|
-
},
|
|
345
|
-
'/root',
|
|
346
|
-
'node_modules/foo/bar.js',
|
|
347
|
-
'require(foo.bar);',
|
|
348
|
-
{
|
|
349
|
-
dev: true,
|
|
350
|
-
type: 'module',
|
|
351
|
-
},
|
|
352
|
-
)
|
|
353
|
-
).output[0].data.code,
|
|
354
|
-
).toBe(
|
|
355
|
-
[
|
|
356
|
-
HEADER_DEV,
|
|
357
|
-
' (function (line) {',
|
|
358
|
-
" throw new Error('Dynamic require defined at line ' + line + '; not supported by Metro');",
|
|
359
|
-
' })(1);',
|
|
360
|
-
'});',
|
|
361
|
-
].join('\n'),
|
|
362
|
-
);
|
|
363
|
-
});
|
|
364
|
-
|
|
365
|
-
it('minifies the code correctly', async () => {
|
|
366
|
-
expect(
|
|
367
|
-
(
|
|
368
|
-
await Transformer.transform(
|
|
369
|
-
baseConfig,
|
|
370
|
-
'/root',
|
|
371
|
-
'local/file.js',
|
|
372
|
-
'arbitrary(code);',
|
|
373
|
-
{
|
|
374
|
-
dev: true,
|
|
375
|
-
minify: true,
|
|
376
|
-
type: 'module',
|
|
377
|
-
},
|
|
378
|
-
)
|
|
379
|
-
).output[0].data.code,
|
|
380
|
-
).toBe([HEADER_PROD, ' minified(code);', '});'].join('\n'));
|
|
381
|
-
});
|
|
382
|
-
|
|
383
|
-
it('minifies a JSON file', async () => {
|
|
384
|
-
expect(
|
|
385
|
-
(
|
|
386
|
-
await Transformer.transform(
|
|
387
|
-
baseConfig,
|
|
388
|
-
'/root',
|
|
389
|
-
'local/file.json',
|
|
390
|
-
'arbitrary(code);',
|
|
391
|
-
{
|
|
392
|
-
dev: true,
|
|
393
|
-
minify: true,
|
|
394
|
-
type: 'module',
|
|
395
|
-
},
|
|
396
|
-
)
|
|
397
|
-
).output[0].data.code,
|
|
398
|
-
).toBe(
|
|
399
|
-
[
|
|
400
|
-
'__d(function(global, require, _importDefaultUnused, _importAllUnused, module, exports, _dependencyMapUnused) {',
|
|
401
|
-
' module.exports = minified(code);;',
|
|
402
|
-
'});',
|
|
403
|
-
].join('\n'),
|
|
404
|
-
);
|
|
405
|
-
});
|
|
406
|
-
|
|
407
|
-
it('does not wrap a JSON file when disableModuleWrapping is enabled', async () => {
|
|
408
|
-
expect(
|
|
409
|
-
(
|
|
410
|
-
await Transformer.transform(
|
|
411
|
-
{
|
|
412
|
-
...baseConfig,
|
|
413
|
-
unstable_disableModuleWrapping: true,
|
|
414
|
-
},
|
|
415
|
-
'/root',
|
|
416
|
-
'local/file.json',
|
|
417
|
-
'arbitrary(code);',
|
|
418
|
-
{
|
|
419
|
-
dev: true,
|
|
420
|
-
type: 'module',
|
|
421
|
-
},
|
|
422
|
-
)
|
|
423
|
-
).output[0].data.code,
|
|
424
|
-
).toBe('module.exports = arbitrary(code);;');
|
|
425
|
-
});
|
|
426
|
-
|
|
427
|
-
it('transforms a script to JS source and bytecode', async () => {
|
|
428
|
-
const result = await Transformer.transform(
|
|
429
|
-
baseConfig,
|
|
430
|
-
'/root',
|
|
431
|
-
'local/file.js',
|
|
432
|
-
'someReallyArbitrary(code)',
|
|
433
|
-
{
|
|
434
|
-
dev: true,
|
|
435
|
-
runtimeBytecodeVersion: 1,
|
|
436
|
-
type: 'script',
|
|
437
|
-
},
|
|
438
|
-
);
|
|
439
|
-
|
|
440
|
-
const jsOutput = result.output.find(output => output.type === 'js/script');
|
|
441
|
-
const bytecodeOutput = result.output.find(
|
|
442
|
-
output => output.type === 'bytecode/script',
|
|
443
|
-
);
|
|
444
|
-
|
|
445
|
-
expect(jsOutput.data.code).toBe(
|
|
446
|
-
[
|
|
447
|
-
'(function (global) {',
|
|
448
|
-
' someReallyArbitrary(code);',
|
|
449
|
-
"})(typeof globalThis !== 'undefined' ? globalThis : typeof global !== 'undefined' ? global : typeof window !== 'undefined' ? window : this);",
|
|
450
|
-
].join('\n'),
|
|
451
|
-
);
|
|
452
|
-
|
|
453
|
-
expect(() =>
|
|
454
|
-
HermesCompiler.validateBytecodeModule(bytecodeOutput.data.bytecode, 0),
|
|
455
|
-
).not.toThrow();
|
|
456
|
-
});
|
|
457
|
-
|
|
458
|
-
it('allows replacing the collectDependencies implementation', async () => {
|
|
459
|
-
jest.mock(
|
|
460
|
-
'metro-transform-worker/__virtual__/collectModifiedDependencies',
|
|
461
|
-
() =>
|
|
462
|
-
jest.fn((ast, opts) => {
|
|
463
|
-
const metroCoreCollectDependencies = jest.requireActual(
|
|
464
|
-
'metro/src/ModuleGraph/worker/collectDependencies',
|
|
465
|
-
);
|
|
466
|
-
const collectedDeps = metroCoreCollectDependencies(ast, opts);
|
|
467
|
-
return {
|
|
468
|
-
...collectedDeps,
|
|
469
|
-
dependencies: collectedDeps.dependencies.map(dep => ({
|
|
470
|
-
...dep,
|
|
471
|
-
name: 'modified_' + dep.name,
|
|
472
|
-
})),
|
|
473
|
-
};
|
|
474
|
-
}),
|
|
475
|
-
{virtual: true},
|
|
476
|
-
);
|
|
477
|
-
|
|
478
|
-
const config = {
|
|
479
|
-
...baseConfig,
|
|
480
|
-
unstable_collectDependenciesPath:
|
|
481
|
-
'metro-transform-worker/__virtual__/collectModifiedDependencies',
|
|
482
|
-
};
|
|
483
|
-
const options = {
|
|
484
|
-
dev: true,
|
|
485
|
-
type: 'module',
|
|
486
|
-
};
|
|
487
|
-
const result = await Transformer.transform(
|
|
488
|
-
config,
|
|
489
|
-
'/root',
|
|
490
|
-
'local/file.js',
|
|
491
|
-
'require("foo")',
|
|
492
|
-
options,
|
|
493
|
-
);
|
|
494
|
-
|
|
495
|
-
// $FlowIgnore[cannot-resolve-module] this is a virtual module
|
|
496
|
-
const collectModifiedDependencies = require('metro-transform-worker/__virtual__/collectModifiedDependencies');
|
|
497
|
-
expect(collectModifiedDependencies).toHaveBeenCalledWith(
|
|
498
|
-
expect.objectContaining({type: 'File'}),
|
|
499
|
-
{
|
|
500
|
-
allowOptionalDependencies: config.allowOptionalDependencies,
|
|
501
|
-
asyncRequireModulePath: config.asyncRequireModulePath,
|
|
502
|
-
dynamicRequires: 'reject',
|
|
503
|
-
inlineableCalls: ['_$$_IMPORT_DEFAULT', '_$$_IMPORT_ALL'],
|
|
504
|
-
keepRequireNames: options.dev,
|
|
505
|
-
dependencyMapName: null,
|
|
506
|
-
},
|
|
507
|
-
);
|
|
508
|
-
expect(result.dependencies).toEqual([
|
|
509
|
-
expect.objectContaining({name: 'modified_foo'}),
|
|
510
|
-
]);
|
|
511
|
-
});
|
|
512
|
-
|
|
513
|
-
it('uses a reserved dependency map name and prevents it from being minified', async () => {
|
|
514
|
-
const result = await Transformer.transform(
|
|
515
|
-
{...baseConfig, unstable_dependencyMapReservedName: 'THE_DEP_MAP'},
|
|
516
|
-
'/root',
|
|
517
|
-
'local/file.js',
|
|
518
|
-
'arbitrary(code);',
|
|
519
|
-
{
|
|
520
|
-
dev: false,
|
|
521
|
-
minify: true,
|
|
522
|
-
type: 'module',
|
|
523
|
-
},
|
|
524
|
-
);
|
|
525
|
-
expect(result.output[0].data.code).toMatchInlineSnapshot(`
|
|
526
|
-
"__d(function (g, r, i, a, m, e, THE_DEP_MAP) {
|
|
527
|
-
minified(code);
|
|
528
|
-
});"
|
|
529
|
-
`);
|
|
530
|
-
});
|
|
531
|
-
|
|
532
|
-
it('throws if the reserved dependency map name appears in the input', async () => {
|
|
533
|
-
await expect(
|
|
534
|
-
Transformer.transform(
|
|
535
|
-
{...baseConfig, unstable_dependencyMapReservedName: 'THE_DEP_MAP'},
|
|
536
|
-
'/root',
|
|
537
|
-
'local/file.js',
|
|
538
|
-
'arbitrary(code); /* the code is not allowed to mention THE_DEP_MAP, even in a comment */',
|
|
539
|
-
{
|
|
540
|
-
dev: false,
|
|
541
|
-
minify: true,
|
|
542
|
-
type: 'module',
|
|
543
|
-
},
|
|
544
|
-
),
|
|
545
|
-
).rejects.toThrowErrorMatchingInlineSnapshot(
|
|
546
|
-
`"Source code contains the reserved string \`THE_DEP_MAP\` at character offset 55"`,
|
|
547
|
-
);
|
|
548
|
-
});
|
|
549
|
-
|
|
550
|
-
it('allows disabling the normalizePseudoGlobals pass when minifying', async () => {
|
|
551
|
-
const result = await Transformer.transform(
|
|
552
|
-
{...baseConfig, unstable_disableNormalizePseudoGlobals: true},
|
|
553
|
-
'/root',
|
|
554
|
-
'local/file.js',
|
|
555
|
-
'arbitrary(code);',
|
|
556
|
-
{
|
|
557
|
-
dev: false,
|
|
558
|
-
minify: true,
|
|
559
|
-
type: 'module',
|
|
560
|
-
},
|
|
561
|
-
);
|
|
562
|
-
expect(result.output[0].data.code).toMatchInlineSnapshot(`
|
|
563
|
-
"__d(function (global, _$$_REQUIRE, _$$_IMPORT_DEFAULT, _$$_IMPORT_ALL, module, exports, _dependencyMap) {
|
|
564
|
-
minified(code);
|
|
565
|
-
});"
|
|
566
|
-
`);
|
|
567
|
-
});
|
|
568
|
-
|
|
569
|
-
it('allows emitting compact code when not minifying', async () => {
|
|
570
|
-
const result = await Transformer.transform(
|
|
571
|
-
{...baseConfig, unstable_compactOutput: true},
|
|
572
|
-
'/root',
|
|
573
|
-
'local/file.js',
|
|
574
|
-
'arbitrary(code);',
|
|
575
|
-
{
|
|
576
|
-
dev: false,
|
|
577
|
-
minify: false,
|
|
578
|
-
type: 'module',
|
|
579
|
-
},
|
|
580
|
-
);
|
|
581
|
-
expect(result.output[0].data.code).toMatchInlineSnapshot(
|
|
582
|
-
`"__d(function(global,_$$_REQUIRE,_$$_IMPORT_DEFAULT,_$$_IMPORT_ALL,module,exports,_dependencyMap){arbitrary(code);});"`,
|
|
583
|
-
);
|
|
584
|
-
});
|
|
585
|
-
|
|
586
|
-
it('skips minification in Hermes stable transform profile', async () => {
|
|
587
|
-
const result = await Transformer.transform(
|
|
588
|
-
baseConfig,
|
|
589
|
-
'/root',
|
|
590
|
-
'local/file.js',
|
|
591
|
-
'arbitrary(code);',
|
|
592
|
-
{
|
|
593
|
-
dev: false,
|
|
594
|
-
minify: true,
|
|
595
|
-
type: 'module',
|
|
596
|
-
unstable_transformProfile: 'hermes-canary',
|
|
597
|
-
},
|
|
598
|
-
);
|
|
599
|
-
expect(result.output[0].data.code).toMatchInlineSnapshot(`
|
|
600
|
-
"__d(function (global, _$$_REQUIRE, _$$_IMPORT_DEFAULT, _$$_IMPORT_ALL, module, exports, _dependencyMap) {
|
|
601
|
-
arbitrary(code);
|
|
602
|
-
});"
|
|
603
|
-
`);
|
|
604
|
-
});
|
|
605
|
-
|
|
606
|
-
it('skips minification in Hermes canary transform profile', async () => {
|
|
607
|
-
const result = await Transformer.transform(
|
|
608
|
-
baseConfig,
|
|
609
|
-
'/root',
|
|
610
|
-
'local/file.js',
|
|
611
|
-
'arbitrary(code);',
|
|
612
|
-
{
|
|
613
|
-
dev: false,
|
|
614
|
-
minify: true,
|
|
615
|
-
type: 'module',
|
|
616
|
-
unstable_transformProfile: 'hermes-canary',
|
|
617
|
-
},
|
|
618
|
-
);
|
|
619
|
-
expect(result.output[0].data.code).toMatchInlineSnapshot(`
|
|
620
|
-
"__d(function (global, _$$_REQUIRE, _$$_IMPORT_DEFAULT, _$$_IMPORT_ALL, module, exports, _dependencyMap) {
|
|
621
|
-
arbitrary(code);
|
|
622
|
-
});"
|
|
623
|
-
`);
|
|
624
|
-
});
|
|
625
|
-
|
|
626
|
-
it('counts all line endings correctly', async () => {
|
|
627
|
-
const transformStr = (
|
|
628
|
-
str: $TEMPORARY$string<'one\ntwo\nthree\nfour\nfive\nsix'> | string,
|
|
629
|
-
) =>
|
|
630
|
-
Transformer.transform(baseConfig, '/root', 'local/file.js', str, {
|
|
631
|
-
dev: false,
|
|
632
|
-
minify: false,
|
|
633
|
-
type: 'module',
|
|
634
|
-
});
|
|
635
|
-
|
|
636
|
-
const differentEndingsResult = await transformStr(
|
|
637
|
-
'one\rtwo\r\nthree\nfour\u2028five\u2029six',
|
|
638
|
-
);
|
|
639
|
-
|
|
640
|
-
const standardEndingsResult = await transformStr(
|
|
641
|
-
'one\ntwo\nthree\nfour\nfive\nsix',
|
|
642
|
-
);
|
|
643
|
-
|
|
644
|
-
expect(differentEndingsResult.output[0].data.lineCount).toEqual(
|
|
645
|
-
standardEndingsResult.output[0].data.lineCount,
|
|
646
|
-
);
|
|
647
|
-
});
|