Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/metro-config/src/defaults/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ const getDefaultValues = (projectRoot: ?string): ConfigT => ({
unstable_compactOutput: false,
unstable_memoizeInlineRequires: false,
unstable_workerThreads: false,
unstable_treeShake: false,
},
watcher: {
additionalExts,
Expand Down
2 changes: 2 additions & 0 deletions packages/metro-config/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ type TransformerConfigT = {
transformVariants: {+[name: string]: Partial<ExtraTransformOptions>},
publicPath: string,
unstable_workerThreads: boolean,
/** Enable tree shaking (production only). Default: false. */
unstable_treeShake: boolean,
};

type MetalConfigT = {
Expand Down
1 change: 1 addition & 0 deletions packages/metro-resolver/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export type PackageJson = Readonly<{
main?: string,
exports?: ExportsField,
imports?: ExportsLikeMap,
sideEffects?: boolean | ReadonlyArray<string>,
...
}>;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

const parser = require('@babel/parser');

module.exports.transform = function ({src}) {
return {
ast: parser.parse(src, {
plugins: ['flow', 'dynamicImport'],
sourceType: 'unambiguous',
}),
metadata: {},
};
};

module.exports.getCacheKey = function () {
return 'passthrough-transformer';
};
146 changes: 146 additions & 0 deletions packages/metro-transform-worker/src/__tests__/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -561,3 +561,149 @@ test('allows outputting comments when `minify: true`', async () => {
});"
`);
});

test('preserves ESM for tree-shake analysis and finalizes selected exports', async () => {
const passthroughTransformerPath = require.resolve(
'./fixtures/passthroughTransformer',
);
const result = await Transformer.transform(
{...baseConfig, babelTransformerPath: passthroughTransformerPath},
'/root',
'local/file.js',
Buffer.from(
[
'import {dep} from "./dep";',
'export const foo = dep + 1;',
'export const bar = dep + 2;',
].join('\n'),
'utf8',
),
{
...baseTransformOptions,
dev: false,
minify: false,
unstable_treeShake: true,
},
);

expect(result.output[0].type).toBe('js/module');
expect(result.output[0].data.code).toContain('export const foo');
expect(result.output[0].data.code).toContain('export const bar');
expect(result.output[0].data.code).not.toContain('__d(function');
expect(result.moduleSyntax?.isESModule).toBe(true);
expect(result.dependencies).toEqual(
expect.arrayContaining([
expect.objectContaining({
name: './dep',
data: expect.objectContaining({
importBindings: [{type: 'named', name: 'dep'}],
}),
}),
]),
);

const moduleSyntax = result.moduleSyntax;
expect(moduleSyntax).toBeDefined();
if (moduleSyntax == null) {
throw new Error('Expected moduleSyntax metadata to be present');
}

const finalized = await Transformer.finalizeModule(
result.output[0].data.code,
moduleSyntax,
{
usedExports: {type: 'named', names: new Set(['foo'])},
filename: 'local/file.js',
reexportDemandBySource: {},
dependencyMapName: '_dependencyMap',
globalPrefix: '',
minify: false,
minifierPath: 'minifyModulePath',
minifierConfig: {output: {comments: false}},
dev: false,
eliminatedReexportSources: {},
parserPlugins: moduleSyntax.parserPlugins,
},
);

expect(finalized.code).toContain('__d(function');
expect(finalized.code).toContain('exports.foo=');
expect(finalized.code).not.toContain('exports.bar=');
});

test('does not defer CJS modules when tree-shake mode is enabled', async () => {
const result = await Transformer.transform(
baseConfig,
'/root',
'local/file.js',
Buffer.from('const x = require("./dep"); module.exports = x;', 'utf8'),
{
...baseTransformOptions,
dev: false,
minify: false,
unstable_treeShake: true,
},
);

expect(result.output[0].type).toBe('js/module');
expect(result.output[0].data.code).toContain('__d(function');
expect(result.output[0].data.code).toContain('_$$_REQUIRE');
expect(result.moduleSyntax).toBeUndefined();
});

test('narrows export-star during finalization when demand is proven', async () => {
const finalized = await Transformer.finalizeModule(
'export * from "./dep";',
{
directExportNames: new Set(),
exports: [{source: './dep', type: 'reExportAll'}],
isESModule: true,
parserPlugins: ['flow'],
},
{
usedExports: {type: 'named', names: new Set(['foo'])},
filename: 'local/file.js',
reexportDemandBySource: {'./dep': ['foo']},
dependencyMapName: '_dependencyMap',
globalPrefix: '',
minify: false,
minifierPath: 'minifyModulePath',
minifierConfig: {output: {comments: false}},
dev: false,
eliminatedReexportSources: {},
parserPlugins: ['flow'],
},
);

expect(finalized.code).toContain('./dep');
expect(finalized.code).toContain('exports.foo=');
expect(finalized.code).not.toContain('for(var _key in');
});

test('keeps export-star during finalization when demand is not proven', async () => {
const finalized = await Transformer.finalizeModule(
'export * from "./dep";',
{
directExportNames: new Set(),
exports: [{source: './dep', type: 'reExportAll'}],
isESModule: true,
parserPlugins: ['flow'],
},
{
usedExports: {type: 'named', names: new Set(['foo'])},
filename: 'local/file.js',
reexportDemandBySource: {},
dependencyMapName: '_dependencyMap',
globalPrefix: '',
minify: false,
minifierPath: 'minifyModulePath',
minifierConfig: {output: {comments: false}},
dev: false,
eliminatedReexportSources: {},
parserPlugins: ['flow'],
},
);

expect(finalized.code).toContain('./dep');
expect(finalized.code).toContain('for(var _key in');
});
Loading