Skip to content

Commit 6d7c110

Browse files
rafecafacebook-github-bot
authored andcommitted
Make minifier options configurable
Reviewed By: davidaurelio Differential Revision: D10147357 fbshipit-source-id: 3bc836e22e9dbb13a9ad4e993b6e36e659f95445
1 parent 681924e commit 6d7c110

File tree

10 files changed

+89
-31
lines changed

10 files changed

+89
-31
lines changed

docs/Configuration.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ These options are used when Metro serves the content.
7171
| `getTransformOptions` | `GetTransformOptions` | Get the transform options. |
7272
| `postMinifyProcess` | `PostMinifyProcess` | What happens after minification. |
7373
| `minifierPath` | `string` | Path to the minifier that minifies the code after transformation. |
74+
| `minifierConfig` | `{[key: string]: mixed}` | Configuration object that will be passed to the minifier (it should be serializable). |
7475
| `optimizationSizeLimit` | `number` | Define a threshold (in bytes) to disable some expensive optimizations for big files. |
7576

7677
#### React Native Only

packages/metro-config/src/__tests__/__snapshots__/loadConfig-test.js.snap

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,23 @@ Object {
8787
"enableBabelRCLookup": true,
8888
"enableBabelRuntime": true,
8989
"getTransformOptions": [Function],
90+
"minifierConfig": Object {
91+
"compress": Object {
92+
"reduce_funcs": false,
93+
},
94+
"mangle": Object {
95+
"toplevel": false,
96+
},
97+
"output": Object {
98+
"ascii_only": true,
99+
"quote_style": 3,
100+
"wrap_iife": true,
101+
},
102+
"sourceMap": Object {
103+
"includeSources": false,
104+
},
105+
"toplevel": false,
106+
},
90107
"minifierPath": "metro-minify-uglify",
91108
"optimizationSizeLimit": 153600,
92109
"postMinifyProcess": [Function],
@@ -189,6 +206,23 @@ Object {
189206
"enableBabelRCLookup": true,
190207
"enableBabelRuntime": true,
191208
"getTransformOptions": [Function],
209+
"minifierConfig": Object {
210+
"compress": Object {
211+
"reduce_funcs": false,
212+
},
213+
"mangle": Object {
214+
"toplevel": false,
215+
},
216+
"output": Object {
217+
"ascii_only": true,
218+
"quote_style": 3,
219+
"wrap_iife": true,
220+
},
221+
"sourceMap": Object {
222+
"includeSources": false,
223+
},
224+
"toplevel": false,
225+
},
192226
"minifierPath": "metro-minify-uglify",
193227
"optimizationSizeLimit": 153600,
194228
"postMinifyProcess": [Function],

packages/metro-config/src/convertConfig.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ async function convertOldToNew({
137137
enableBabelRCLookup: getEnableBabelRCLookup(),
138138
enableBabelRuntime: true,
139139
getTransformOptions,
140+
minifierConfig: defaultConfig.transformer.minifierConfig,
140141
minifierPath: minifierPath || defaultConfig.transformer.minifierPath,
141142
optimizationSizeLimit: 150 * 1024, // 150 KiB enforced for old configs.
142143
postMinifyProcess,

packages/metro-config/src/defaults/index.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,24 @@ const getDefaultValues = (projectRoot: ?string): ConfigT => ({
7575
preloadedModules: false,
7676
ramGroups: [],
7777
}),
78+
minifierConfig: {
79+
mangle: {
80+
toplevel: false,
81+
},
82+
output: {
83+
ascii_only: true,
84+
quote_style: 3,
85+
wrap_iife: true,
86+
},
87+
sourceMap: {
88+
includeSources: false,
89+
},
90+
toplevel: false,
91+
compress: {
92+
// reduce_funcs inlines single-use functions, which cause perf regressions.
93+
reduce_funcs: false,
94+
},
95+
},
7896
minifierPath: DEFAULT_METRO_MINIFIER_PATH,
7997
optimizationSizeLimit: 150 * 1024, // 150 KiB.
8098
postMinifyProcess: x => x,

packages/metro-minify-terser/__tests__/minify-test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const baseOptions = {
3838
map: getFakeMap(),
3939
filename: '',
4040
reserved: [],
41+
config: {},
4142
};
4243

4344
describe('Minification:', () => {
@@ -59,6 +60,7 @@ describe('Minification:', () => {
5960
code,
6061
map,
6162
filename,
63+
config: {sourceMap: {includeSources: false}},
6264
});
6365
expect(terser.minify).toBeCalledWith(
6466
code,

packages/metro-minify-terser/src/minifier.js

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,34 +35,32 @@ function minify({
3535
code,
3636
map,
3737
reserved,
38+
config,
3839
}: MinifierOptions): {code: string, map: ?string} {
39-
const result = terser.minify(code, {
40+
const options = {
41+
...config,
4042
mangle: {
41-
toplevel: false,
43+
...config.mangle,
4244
reserved,
4345
},
44-
output: {
45-
ascii_only: true,
46-
quote_style: 3,
47-
wrap_iife: true,
48-
},
49-
sourceMap: {
50-
content: map,
51-
includeSources: false,
52-
},
53-
toplevel: false,
54-
compress: {
55-
// reduce_funcs inlines single-use function, which cause perf regressions.
56-
reduce_funcs: false,
57-
},
58-
});
46+
sourceMap: map
47+
? {
48+
...config.sourceMap,
49+
content: map,
50+
}
51+
: false,
52+
};
53+
54+
const result = terser.minify(code, options);
5955

6056
if (result.error) {
6157
throw result.error;
6258
}
6359

6460
return {
6561
code: result.code,
62+
// eslint-disable-next-line lint/flow-no-fixme
63+
// $FlowFixMe flow cannot coerce the uglify options after using spread.
6664
map: result.map,
6765
};
6866
}

packages/metro-minify-uglify/__tests__/minify-test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const baseOptions = {
3838
map: getFakeMap(),
3939
filename: '',
4040
reserved: [],
41+
config: {},
4142
};
4243

4344
describe('Minification:', () => {
@@ -59,6 +60,7 @@ describe('Minification:', () => {
5960
code,
6061
map,
6162
filename,
63+
config: {sourceMap: {includeSources: false}},
6264
});
6365
expect(uglify.minify).toBeCalledWith(
6466
code,

packages/metro-minify-uglify/src/minifier.js

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,34 +35,30 @@ function minify({
3535
code,
3636
map,
3737
reserved,
38+
config,
3839
}: MinifierOptions): {code: string, map: ?string} {
39-
const result = uglify.minify(code, {
40+
const options = {
41+
...config,
4042
mangle: {
41-
toplevel: false,
43+
...config.mangle,
4244
reserved,
4345
},
44-
output: {
45-
ascii_only: true,
46-
quote_style: 3,
47-
wrap_iife: true,
48-
},
4946
sourceMap: {
47+
...config.sourceMap,
5048
content: map,
51-
includeSources: false,
52-
},
53-
toplevel: false,
54-
compress: {
55-
// reduce_funcs inlines single-use function, which cause perf regressions.
56-
reduce_funcs: false,
5749
},
58-
});
50+
};
51+
52+
const result = uglify.minify(code, options);
5953

6054
if (result.error) {
6155
throw result.error;
6256
}
6357

6458
return {
6559
code: result.code,
60+
// eslint-disable-next-line lint/flow-no-fixme
61+
// $FlowFixMe flow cannot coerce the uglify options after using spread.
6662
map: result.map,
6763
};
6864
}

packages/metro/src/JSTransformer/worker.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,14 @@ export type BabelTransformer = {|
6969
getCacheKey?: () => string,
7070
|};
7171

72+
type MinifierConfig = $ReadOnly<{[string]: mixed}>;
73+
7274
export type MinifierOptions = {
7375
code: string,
7476
map: ?BabelSourceMap,
7577
filename: string,
7678
reserved: $ReadOnlyArray<string>,
79+
config: MinifierConfig,
7780
};
7881

7982
export type Type = 'script' | 'module' | 'asset';
@@ -86,6 +89,7 @@ export type JsTransformerConfig = $ReadOnly<{|
8689
dynamicDepsInPackages: DynamicRequiresBehavior,
8790
enableBabelRCLookup: boolean,
8891
enableBabelRuntime: boolean,
92+
minifierConfig: MinifierConfig,
8993
minifierPath: string,
9094
optimizationSizeLimit: number,
9195
|}>;
@@ -345,6 +349,7 @@ class JsTransformer {
345349
map: sourceMap,
346350
filename,
347351
reserved,
352+
config: this._config.minifierConfig,
348353
});
349354

350355
return {

packages/metro/src/JSTransformer/worker/__tests__/worker-test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const baseOptions = {
4242
babelTransformerPath,
4343
dynamicDepsInPackages: 'reject',
4444
enableBabelRuntime: true,
45+
minifierConfig: {},
4546
minifierPath: 'minifyModulePath',
4647
optimizationSizeLimit: 100000,
4748
};

0 commit comments

Comments
 (0)