Skip to content

Commit

Permalink
Add experimental transformer settings: compactOutput, disableNormaliz…
Browse files Browse the repository at this point in the history
…ePseudoGlobals

Summary:
Adds two experimental flags to Metro's built-in JS transformer. These are:

* `unstable_compactOutput`: generate compact output from Babel. This is a little faster than the default behaviour of generating pretty-printed JS. The change in output is only noticeable when not subsequently running a minifier (per the normal flags).
* `unstable_disableNormalizePseudoGlobals`: disables the normalizePseudoGlobals pass, a pre-processing step Metro runs before the minifier to make the resulting module headers more consistent and therefore compressible.

NOTE: These flags may change or stop working at any point, even in a patch version of Metro.

Reviewed By: MichaReiser, feedthejim

Differential Revision: D28442418

fbshipit-source-id: c692af6ebda1cfd1284d59701a168bb56689da60
  • Loading branch information
motiz88 authored and facebook-github-bot committed May 25, 2021
1 parent c6aef25 commit 5b913fa
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ Object {
"default": Object {},
},
"unstable_collectDependenciesPath": "metro/src/ModuleGraph/worker/collectDependencies.js",
"unstable_compactOutput": false,
"unstable_dependencyMapReservedName": null,
"unstable_disableNormalizePseudoGlobals": false,
"workerPath": "metro/src/DeltaBundler/Worker",
},
"transformerPath": "",
Expand Down Expand Up @@ -268,7 +270,9 @@ Object {
"default": Object {},
},
"unstable_collectDependenciesPath": "metro/src/ModuleGraph/worker/collectDependencies.js",
"unstable_compactOutput": false,
"unstable_dependencyMapReservedName": null,
"unstable_disableNormalizePseudoGlobals": false,
"workerPath": "metro/src/DeltaBundler/Worker",
},
"transformerPath": "",
Expand Down Expand Up @@ -407,7 +411,9 @@ Object {
"default": Object {},
},
"unstable_collectDependenciesPath": "metro/src/ModuleGraph/worker/collectDependencies.js",
"unstable_compactOutput": false,
"unstable_dependencyMapReservedName": null,
"unstable_disableNormalizePseudoGlobals": false,
"workerPath": "metro/src/DeltaBundler/Worker",
},
"transformerPath": "",
Expand Down Expand Up @@ -546,7 +552,9 @@ Object {
"default": Object {},
},
"unstable_collectDependenciesPath": "metro/src/ModuleGraph/worker/collectDependencies.js",
"unstable_compactOutput": false,
"unstable_dependencyMapReservedName": null,
"unstable_disableNormalizePseudoGlobals": false,
"workerPath": "metro/src/DeltaBundler/Worker",
},
"transformerPath": "",
Expand Down
2 changes: 2 additions & 0 deletions packages/metro-config/src/defaults/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ const getDefaultValues = (projectRoot: ?string): ConfigT => ({
unstable_collectDependenciesPath:
'metro/src/ModuleGraph/worker/collectDependencies.js',
unstable_dependencyMapReservedName: null,
unstable_disableNormalizePseudoGlobals: false,
unstable_compactOutput: false,
},
cacheStores: [
new FileStore({
Expand Down
38 changes: 38 additions & 0 deletions packages/metro-transform-worker/src/__tests__/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ const baseConfig: JsTransformerConfig = {
unstable_collectDependenciesPath:
'metro/src/ModuleGraph/worker/collectDependencies',
unstable_dependencyMapReservedName: null,
unstable_compactOutput: false,
unstable_disableNormalizePseudoGlobals: false,
};

beforeEach(() => {
Expand Down Expand Up @@ -480,3 +482,39 @@ it('throws if the reserved dependency map name appears in the input', async () =
`"Source code contains the reserved string \`THE_DEP_MAP\` at character offset 55"`,
);
});

it('allows disabling the normalizePseudoGlobals pass when minifying', async () => {
const result = await Transformer.transform(
{...baseConfig, unstable_disableNormalizePseudoGlobals: true},
'/root',
'local/file.js',
'arbitrary(code);',
{
dev: false,
minify: true,
type: 'module',
},
);
expect(result.output[0].data.code).toMatchInlineSnapshot(`
"__d(function (global, _$$_REQUIRE, _$$_IMPORT_DEFAULT, _$$_IMPORT_ALL, module, exports, _dependencyMap) {
minified(code);
});"
`);
});

it('allows emitting compact code when not minifying', async () => {
const result = await Transformer.transform(
{...baseConfig, unstable_compactOutput: true},
'/root',
'local/file.js',
'arbitrary(code);',
{
dev: false,
minify: false,
type: 'module',
},
);
expect(result.output[0].data.code).toMatchInlineSnapshot(
`"__d(function(global,_$$_REQUIRE,_$$_IMPORT_DEFAULT,_$$_IMPORT_ALL,module,exports,_dependencyMap){arbitrary(code);});"`,
);
});
11 changes: 8 additions & 3 deletions packages/metro-transform-worker/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ export type JsTransformerConfig = $ReadOnly<{|
allowOptionalDependencies: AllowOptionalDependencies,
unstable_collectDependenciesPath: string,
unstable_dependencyMapReservedName: ?string,
unstable_disableNormalizePseudoGlobals: boolean,
unstable_compactOutput: boolean,
|}>;

export type {CustomTransformOptions} from 'metro-babel-transformer';
Expand Down Expand Up @@ -329,7 +331,6 @@ async function transformJS(
code: false,
configFile: false,
comments: false,
compact: false,
filename: file.filename,
plugins,
sourceMaps: false,
Expand Down Expand Up @@ -389,7 +390,11 @@ async function transformJS(
if (config.unstable_dependencyMapReservedName != null) {
reserved.push(config.unstable_dependencyMapReservedName);
}
if (options.minify && file.inputFileSize <= config.optimizationSizeLimit) {
if (
options.minify &&
file.inputFileSize <= config.optimizationSizeLimit &&
!config.unstable_disableNormalizePseudoGlobals
) {
reserved.push(
...metroTransformPlugins.normalizePseudoGlobals(wrappedAst, {
reservedNames: reserved,
Expand All @@ -401,7 +406,7 @@ async function transformJS(
wrappedAst,
{
comments: false,
compact: false,
compact: config.unstable_compactOutput,
filename: file.filename,
retainLines: false,
sourceFileName: file.filename,
Expand Down

0 comments on commit 5b913fa

Please sign in to comment.