metro-transform-worker 0.63.0 → 0.64.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.
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its 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
+ * @flow strict-local
8
+ * @format
9
+ */
10
+
11
+ 'use strict';
12
+
13
+ import type {Minifier} from '../index.js';
14
+
15
+ function getMinifier(minifierPath: string): Minifier {
16
+ // Note: minifierPath should be an absolute path OR a module name here!
17
+ // The options allow relative paths but they HAVE to be normalized at
18
+ // any entry point that accepts them...
19
+ try {
20
+ // $FlowFixMe TODO t0 cannot do require with literal
21
+ return require(minifierPath);
22
+ } catch (e) {
23
+ throw new Error(
24
+ 'A problem occurred while trying to fetch the minifier. Path: "' +
25
+ minifierPath +
26
+ '", error message: ' +
27
+ e.message,
28
+ );
29
+ }
30
+ }
31
+
32
+ module.exports = getMinifier;
@@ -1,306 +0,0 @@
1
- // Jest Snapshot v1, https://goo.gl/fbAQLP
2
-
3
- exports[`reports filename when encountering unsupported dynamic dependency 1`] = `"local/file.js:Invalid call at line 3: require(a)"`;
4
-
5
- exports[`transforms a module with dependencies 1`] = `
6
- Array [
7
- Array [
8
- 2,
9
- 0,
10
- 1,
11
- 0,
12
- ],
13
- Array [
14
- 6,
15
- 0,
16
- 5,
17
- 0,
18
- ],
19
- Array [
20
- 8,
21
- 0,
22
- 2,
23
- 0,
24
- "require",
25
- ],
26
- Array [
27
- 8,
28
- 2,
29
- 2,
30
- 0,
31
- "require",
32
- ],
33
- Array [
34
- 8,
35
- 13,
36
- 2,
37
- 7,
38
- ],
39
- Array [
40
- 8,
41
- 39,
42
- 2,
43
- 0,
44
- ],
45
- Array [
46
- 10,
47
- 0,
48
- 3,
49
- 0,
50
- "arbitrary",
51
- ],
52
- Array [
53
- 10,
54
- 2,
55
- 3,
56
- 0,
57
- "arbitrary",
58
- ],
59
- Array [
60
- 10,
61
- 11,
62
- 3,
63
- 9,
64
- ],
65
- Array [
66
- 10,
67
- 12,
68
- 3,
69
- 10,
70
- "code",
71
- ],
72
- Array [
73
- 10,
74
- 16,
75
- 3,
76
- 9,
77
- ],
78
- Array [
79
- 10,
80
- 17,
81
- 3,
82
- 0,
83
- ],
84
- Array [
85
- 12,
86
- 0,
87
- 4,
88
- 0,
89
- ],
90
- Array [
91
- 12,
92
- 6,
93
- 4,
94
- 6,
95
- "b",
96
- ],
97
- Array [
98
- 12,
99
- 7,
100
- 4,
101
- 7,
102
- ],
103
- Array [
104
- 12,
105
- 10,
106
- 4,
107
- 10,
108
- "require",
109
- ],
110
- Array [
111
- 12,
112
- 21,
113
- 4,
114
- 17,
115
- ],
116
- Array [
117
- 12,
118
- 45,
119
- 4,
120
- 0,
121
- ],
122
- ]
123
- `;
124
-
125
- exports[`transforms a module with dependencies 2`] = `
126
- Object {
127
- "mappings": "AAA",
128
- "names": Array [
129
- "<global>",
130
- ],
131
- }
132
- `;
133
-
134
- exports[`transforms a simple module 1`] = `
135
- Array [
136
- Array [
137
- 2,
138
- 0,
139
- 1,
140
- 0,
141
- "arbitrary",
142
- ],
143
- Array [
144
- 2,
145
- 2,
146
- 1,
147
- 0,
148
- "arbitrary",
149
- ],
150
- Array [
151
- 2,
152
- 11,
153
- 1,
154
- 9,
155
- ],
156
- Array [
157
- 2,
158
- 12,
159
- 1,
160
- 10,
161
- "code",
162
- ],
163
- Array [
164
- 2,
165
- 16,
166
- 1,
167
- 9,
168
- ],
169
- Array [
170
- 2,
171
- 17,
172
- 1,
173
- 0,
174
- ],
175
- ]
176
- `;
177
-
178
- exports[`transforms a simple module 2`] = `
179
- Object {
180
- "mappings": "AAA",
181
- "names": Array [
182
- "<global>",
183
- ],
184
- }
185
- `;
186
-
187
- exports[`transforms a simple script 1`] = `
188
- Array [
189
- Array [
190
- 2,
191
- 0,
192
- 1,
193
- 0,
194
- "someReallyArbitrary",
195
- ],
196
- Array [
197
- 2,
198
- 2,
199
- 1,
200
- 0,
201
- "someReallyArbitrary",
202
- ],
203
- Array [
204
- 2,
205
- 21,
206
- 1,
207
- 19,
208
- ],
209
- Array [
210
- 2,
211
- 22,
212
- 1,
213
- 20,
214
- "code",
215
- ],
216
- Array [
217
- 2,
218
- 26,
219
- 1,
220
- 19,
221
- ],
222
- Array [
223
- 2,
224
- 27,
225
- 1,
226
- 0,
227
- ],
228
- ]
229
- `;
230
-
231
- exports[`transforms a simple script 2`] = `
232
- Object {
233
- "mappings": "AAA",
234
- "names": Array [
235
- "<global>",
236
- ],
237
- }
238
- `;
239
-
240
- exports[`transforms an es module with regenerator 1`] = `
241
- "__d(function (global, _$$_REQUIRE, _$$_IMPORT_DEFAULT, _$$_IMPORT_ALL, module, exports, _dependencyMap) {
242
- var _interopRequireDefault = _$$_REQUIRE(_dependencyMap[0], \\"@babel/runtime/helpers/interopRequireDefault\\");
243
-
244
- Object.defineProperty(exports, \\"__esModule\\", {
245
- value: true
246
- });
247
- exports.test = test;
248
-
249
- var _regenerator = _interopRequireDefault(_$$_REQUIRE(_dependencyMap[1], \\"@babel/runtime/regenerator\\"));
250
-
251
- function test() {
252
- return _regenerator.default.async(function test$(_context) {
253
- while (1) {
254
- switch (_context.prev = _context.next) {
255
- case 0:
256
- case \\"end\\":
257
- return _context.stop();
258
- }
259
- }
260
- }, null, this);
261
- }
262
- });"
263
- `;
264
-
265
- exports[`transforms an es module with regenerator 2`] = `
266
- Object {
267
- "mappings": "AAA,OC",
268
- "names": Array [
269
- "<global>",
270
- "test",
271
- ],
272
- }
273
- `;
274
-
275
- exports[`transforms import/export syntax when experimental flag is on 1`] = `
276
- Array [
277
- Array [
278
- 4,
279
- 0,
280
- 1,
281
- 0,
282
- ],
283
- Array [
284
- 4,
285
- 6,
286
- 1,
287
- 7,
288
- "c",
289
- ],
290
- Array [
291
- 4,
292
- 7,
293
- 1,
294
- 0,
295
- ],
296
- ]
297
- `;
298
-
299
- exports[`transforms import/export syntax when experimental flag is on 2`] = `
300
- Object {
301
- "mappings": "AAA",
302
- "names": Array [
303
- "<global>",
304
- ],
305
- }
306
- `;
@@ -1,380 +0,0 @@
1
- /**
2
- * Copyright (c) Facebook, Inc. and its 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
- * @format
9
- */
10
-
11
- 'use strict';
12
-
13
- jest
14
- .mock('../utils/getMinifier', () => () => ({code, map}) => ({
15
- code: code.replace('arbitrary(code)', 'minified(code)'),
16
- map,
17
- }))
18
- .mock('metro-transform-plugins', () => ({
19
- ...jest.requireActual('metro-transform-plugins'),
20
- inlinePlugin: () => ({}),
21
- constantFoldingPlugin: () => ({}),
22
- }))
23
- .mock('metro-minify-uglify');
24
-
25
- const HermesCompiler = require('metro-hermes-compiler');
26
-
27
- const path = require('path');
28
-
29
- const babelTransformerPath = require.resolve(
30
- 'metro-react-native-babel-transformer',
31
- );
32
- const transformerContents = require('fs').readFileSync(babelTransformerPath);
33
-
34
- const HEADER_DEV =
35
- '__d(function (global, _$$_REQUIRE, _$$_IMPORT_DEFAULT, _$$_IMPORT_ALL, module, exports, _dependencyMap) {';
36
- const HEADER_PROD = '__d(function (g, r, i, a, m, e, d) {';
37
-
38
- let fs;
39
- let mkdirp;
40
- let Transformer;
41
-
42
- const baseOptions = {
43
- assetExts: [],
44
- assetPlugins: [],
45
- assetRegistryPath: '',
46
- asyncRequireModulePath: 'asyncRequire',
47
- babelTransformerPath,
48
- dynamicDepsInPackages: 'reject',
49
- enableBabelRuntime: true,
50
- globalPrefix: '',
51
- minifierConfig: {},
52
- minifierPath: 'minifyModulePath',
53
- optimizationSizeLimit: 100000,
54
- };
55
-
56
- beforeEach(() => {
57
- jest.resetModules();
58
-
59
- jest.mock('fs', () => new (require('metro-memory-fs'))());
60
-
61
- fs = require('fs');
62
- mkdirp = require('mkdirp');
63
- Transformer = require('../');
64
- fs.reset();
65
-
66
- mkdirp.sync('/root/local');
67
- mkdirp.sync(path.dirname(babelTransformerPath));
68
- fs.writeFileSync(babelTransformerPath, transformerContents);
69
- });
70
-
71
- it('transforms a simple script', async () => {
72
- const result = await Transformer.transform(
73
- baseOptions,
74
- '/root',
75
- 'local/file.js',
76
- 'someReallyArbitrary(code)',
77
- {
78
- dev: true,
79
- type: 'script',
80
- },
81
- );
82
-
83
- expect(result.output[0].type).toBe('js/script');
84
- expect(result.output[0].data.code).toBe(
85
- [
86
- '(function (global) {',
87
- ' someReallyArbitrary(code);',
88
- "})(typeof globalThis !== 'undefined' ? globalThis : typeof global !== 'undefined' ? global : typeof window !== 'undefined' ? window : this);",
89
- ].join('\n'),
90
- );
91
- expect(result.output[0].data.map).toMatchSnapshot();
92
- expect(result.output[0].data.functionMap).toMatchSnapshot();
93
- expect(result.dependencies).toEqual([]);
94
- });
95
-
96
- it('transforms a simple module', async () => {
97
- const result = await Transformer.transform(
98
- baseOptions,
99
- '/root',
100
- 'local/file.js',
101
- 'arbitrary(code)',
102
- {
103
- dev: true,
104
- type: 'module',
105
- },
106
- );
107
-
108
- expect(result.output[0].type).toBe('js/module');
109
- expect(result.output[0].data.code).toBe(
110
- [HEADER_DEV, ' arbitrary(code);', '});'].join('\n'),
111
- );
112
- expect(result.output[0].data.map).toMatchSnapshot();
113
- expect(result.output[0].data.functionMap).toMatchSnapshot();
114
- expect(result.dependencies).toEqual([]);
115
- });
116
-
117
- it('transforms a module with dependencies', async () => {
118
- const contents = [
119
- "'use strict';",
120
- 'require("./a");',
121
- 'arbitrary(code);',
122
- 'const b = require("b");',
123
- 'import c from "./c";',
124
- ].join('\n');
125
-
126
- const result = await Transformer.transform(
127
- baseOptions,
128
- '/root',
129
- 'local/file.js',
130
- contents,
131
- {
132
- dev: true,
133
- type: 'module',
134
- },
135
- );
136
-
137
- expect(result.output[0].type).toBe('js/module');
138
- expect(result.output[0].data.code).toBe(
139
- [
140
- HEADER_DEV,
141
- " 'use strict';",
142
- '',
143
- ' var _interopRequireDefault = _$$_REQUIRE(_dependencyMap[0], "@babel/runtime/helpers/interopRequireDefault");',
144
- '',
145
- ' var _c = _interopRequireDefault(_$$_REQUIRE(_dependencyMap[1], "./c"));',
146
- '',
147
- ' _$$_REQUIRE(_dependencyMap[2], "./a");',
148
- '',
149
- ' arbitrary(code);',
150
- '',
151
- ' var b = _$$_REQUIRE(_dependencyMap[3], "b");',
152
- '});',
153
- ].join('\n'),
154
- );
155
- expect(result.output[0].data.map).toMatchSnapshot();
156
- expect(result.output[0].data.functionMap).toMatchSnapshot();
157
- expect(result.dependencies).toEqual([
158
- {
159
- data: expect.objectContaining({asyncType: null}),
160
- name: '@babel/runtime/helpers/interopRequireDefault',
161
- },
162
- {data: expect.objectContaining({asyncType: null}), name: './c'},
163
- {data: expect.objectContaining({asyncType: null}), name: './a'},
164
- {data: expect.objectContaining({asyncType: null}), name: 'b'},
165
- ]);
166
- });
167
-
168
- it('transforms an es module with regenerator', async () => {
169
- const result = await Transformer.transform(
170
- baseOptions,
171
- '/root',
172
- 'local/file.js',
173
- 'export async function test() {}',
174
- {
175
- dev: true,
176
- type: 'module',
177
- },
178
- );
179
-
180
- expect(result.output[0].type).toBe('js/module');
181
- const code = result.output[0].data.code.split('\n');
182
- // Ignore a small difference in regenerator output
183
- if (code[code.length - 3] === ' }, null, null, null, Promise);') {
184
- code[code.length - 3] = ' }, null, this);';
185
- }
186
- expect(code.join('\n')).toMatchSnapshot();
187
- expect(result.output[0].data.map).toHaveLength(13);
188
- expect(result.output[0].data.functionMap).toMatchSnapshot();
189
- expect(result.dependencies).toEqual([
190
- {
191
- data: expect.objectContaining({asyncType: null}),
192
- name: '@babel/runtime/helpers/interopRequireDefault',
193
- },
194
- {
195
- data: expect.objectContaining({asyncType: null}),
196
- name: '@babel/runtime/regenerator',
197
- },
198
- ]);
199
- });
200
-
201
- it('transforms import/export syntax when experimental flag is on', async () => {
202
- const contents = ['import c from "./c";'].join('\n');
203
-
204
- const result = await Transformer.transform(
205
- baseOptions,
206
- '/root',
207
- 'local/file.js',
208
- contents,
209
- {
210
- dev: true,
211
- experimentalImportSupport: true,
212
- type: 'module',
213
- },
214
- );
215
-
216
- expect(result.output[0].type).toBe('js/module');
217
- expect(result.output[0].data.code).toBe(
218
- [
219
- HEADER_DEV,
220
- ' "use strict";',
221
- '',
222
- ' var c = _$$_IMPORT_DEFAULT(_dependencyMap[0], "./c");',
223
- '});',
224
- ].join('\n'),
225
- );
226
- expect(result.output[0].data.map).toMatchSnapshot();
227
- expect(result.output[0].data.functionMap).toMatchSnapshot();
228
- expect(result.dependencies).toEqual([
229
- {
230
- data: expect.objectContaining({
231
- asyncType: null,
232
- }),
233
- name: './c',
234
- },
235
- ]);
236
- });
237
-
238
- it('does not add "use strict" on non-modules', async () => {
239
- const result = await Transformer.transform(
240
- baseOptions,
241
- '/root',
242
- 'node_modules/local/file.js',
243
- 'module.exports = {};',
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
- [HEADER_DEV, ' module.exports = {};', '});'].join('\n'),
254
- );
255
- });
256
-
257
- it('reports filename when encountering unsupported dynamic dependency', async () => {
258
- const contents = [
259
- 'require("./a");',
260
- 'let a = arbitrary(code);',
261
- 'const b = require(a);',
262
- ].join('\n');
263
-
264
- try {
265
- await Transformer.transform(
266
- baseOptions,
267
- '/root',
268
- 'local/file.js',
269
- contents,
270
- {
271
- dev: true,
272
- type: 'module',
273
- },
274
- );
275
- throw new Error('should not reach this');
276
- } catch (error) {
277
- expect(error.message).toMatchSnapshot();
278
- }
279
- });
280
-
281
- it('supports dynamic dependencies from within `node_modules`', async () => {
282
- expect(
283
- (
284
- await Transformer.transform(
285
- {
286
- ...baseOptions,
287
- dynamicDepsInPackages: 'throwAtRuntime',
288
- },
289
- '/root',
290
- 'node_modules/foo/bar.js',
291
- 'require(foo.bar);',
292
- {
293
- dev: true,
294
- type: 'module',
295
- },
296
- )
297
- ).output[0].data.code,
298
- ).toBe(
299
- [
300
- HEADER_DEV,
301
- ' (function (line) {',
302
- " throw new Error('Dynamic require defined at line ' + line + '; not supported by Metro');",
303
- ' })(1);',
304
- '});',
305
- ].join('\n'),
306
- );
307
- });
308
-
309
- it('minifies the code correctly', async () => {
310
- expect(
311
- (
312
- await Transformer.transform(
313
- baseOptions,
314
- '/root',
315
- 'local/file.js',
316
- 'arbitrary(code);',
317
- {
318
- dev: true,
319
- minify: true,
320
- type: 'module',
321
- },
322
- )
323
- ).output[0].data.code,
324
- ).toBe([HEADER_PROD, ' minified(code);', '});'].join('\n'));
325
- });
326
-
327
- it('minifies a JSON file', async () => {
328
- expect(
329
- (
330
- await Transformer.transform(
331
- baseOptions,
332
- '/root',
333
- 'local/file.json',
334
- 'arbitrary(code);',
335
- {
336
- dev: true,
337
- minify: true,
338
- type: 'module',
339
- },
340
- )
341
- ).output[0].data.code,
342
- ).toBe(
343
- [
344
- '__d(function(global, require, _aUnused, _bUnused, module, exports, _cUnused) {',
345
- ' module.exports = minified(code);;',
346
- '});',
347
- ].join('\n'),
348
- );
349
- });
350
-
351
- it('transforms a script to JS source and bytecode', async () => {
352
- const result = await Transformer.transform(
353
- baseOptions,
354
- '/root',
355
- 'local/file.js',
356
- 'someReallyArbitrary(code)',
357
- {
358
- dev: true,
359
- runtimeBytecodeVersion: 1,
360
- type: 'script',
361
- },
362
- );
363
-
364
- const jsOutput = result.output.find(output => output.type === 'js/script');
365
- const bytecodeOutput = result.output.find(
366
- output => output.type === 'bytecode/script',
367
- );
368
-
369
- expect(jsOutput.data.code).toBe(
370
- [
371
- '(function (global) {',
372
- ' someReallyArbitrary(code);',
373
- "})(typeof globalThis !== 'undefined' ? globalThis : typeof global !== 'undefined' ? global : typeof window !== 'undefined' ? window : this);",
374
- ].join('\n'),
375
- );
376
-
377
- expect(() =>
378
- HermesCompiler.validateBytecodeModule(bytecodeOutput.data.bytecode, 0),
379
- ).not.toThrow();
380
- });