|
| 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 | + * @flow strict-local |
| 8 | + * @format |
| 9 | + */ |
| 10 | + |
| 11 | +'use strict'; |
| 12 | + |
| 13 | +const generate = require('@babel/generator').default; |
| 14 | +const path = require('node:path'); |
| 15 | + |
| 16 | +const PROJECT_ROOT = path.sep === '/' ? '/my/project' : 'C:\\my\\project'; |
| 17 | +const PROJECT_BABEL_CONFIG = path.join( |
| 18 | + __dirname, |
| 19 | + '__fixtures__', |
| 20 | + 'babel.config.js', |
| 21 | +); |
| 22 | +const SRC = "import foo from './foo';\nexport const bar = foo;"; |
| 23 | + |
| 24 | +// The transformer memoizes its resolved Babel config in a module-level |
| 25 | +// closure, so a fresh module instance is required for every distinct config. |
| 26 | +beforeEach(() => { |
| 27 | + jest.resetModules(); |
| 28 | +}); |
| 29 | + |
| 30 | +function transformToCode({ |
| 31 | + experimentalImportSupport, |
| 32 | + extendsBabelConfigPath, |
| 33 | +}: { |
| 34 | + experimentalImportSupport: boolean, |
| 35 | + extendsBabelConfigPath?: string, |
| 36 | +}): string { |
| 37 | + const {transform} = require('../index.js'); |
| 38 | + const {ast} = transform({ |
| 39 | + filename: path.join(PROJECT_ROOT, 'App.js'), |
| 40 | + src: SRC, |
| 41 | + plugins: [], |
| 42 | + options: { |
| 43 | + dev: true, |
| 44 | + enableBabelRuntime: false, |
| 45 | + enableBabelRCLookup: false, |
| 46 | + experimentalImportSupport, |
| 47 | + extendsBabelConfigPath, |
| 48 | + globalPrefix: '__metro__', |
| 49 | + hot: false, |
| 50 | + minify: false, |
| 51 | + platform: 'ios', |
| 52 | + publicPath: 'test', |
| 53 | + projectRoot: PROJECT_ROOT, |
| 54 | + }, |
| 55 | + }); |
| 56 | + return generate(ast).code; |
| 57 | +} |
| 58 | + |
| 59 | +function isLowered(code: string): boolean { |
| 60 | + return code.includes('require(') && !/^import\b/m.test(code); |
| 61 | +} |
| 62 | + |
| 63 | +describe.each([ |
| 64 | + ['no project Babel config', undefined], |
| 65 | + ['a project Babel config naming the preset', PROJECT_BABEL_CONFIG], |
| 66 | +])('with %s', (_name, extendsBabelConfigPath) => { |
| 67 | + test('lowers import/export when experimentalImportSupport is off', () => { |
| 68 | + const code = transformToCode({ |
| 69 | + experimentalImportSupport: false, |
| 70 | + extendsBabelConfigPath, |
| 71 | + }); |
| 72 | + |
| 73 | + expect(isLowered(code)).toBe(true); |
| 74 | + }); |
| 75 | + |
| 76 | + test('keeps import/export when experimentalImportSupport is on', () => { |
| 77 | + // Metro lowers ESM itself in this mode. If the preset lowered it first, |
| 78 | + // Metro's plugin would find nothing to do and the option would be a no-op. |
| 79 | + const code = transformToCode({ |
| 80 | + experimentalImportSupport: true, |
| 81 | + extendsBabelConfigPath, |
| 82 | + }); |
| 83 | + |
| 84 | + expect(isLowered(code)).toBe(false); |
| 85 | + expect(code).toContain("import foo from './foo'"); |
| 86 | + }); |
| 87 | +}); |
0 commit comments