Skip to content
This repository was archived by the owner on Dec 3, 2025. It is now read-only.

Commit b87d067

Browse files
author
Eimantas Dumse
committed
Updated Terser default options.
1 parent 90d60d5 commit b87d067

2 files changed

Lines changed: 113 additions & 27 deletions

File tree

packages/webpack-builder-plugin-typescript/src/__tests__/__snapshots__/plugin.test.ts.snap

Lines changed: 66 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/webpack-builder-plugin-typescript/src/plugin.ts

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ export interface TypeScriptPluginOptions {
2828
tsconfigPathsPluginOptions?: Partial<TsconfigPathsPluginOptions>;
2929
terserPluginOptions?: TerserPluginOptions;
3030
linter?: Linter;
31+
isEnvProduction?: boolean;
32+
shouldUseSourceMap?: boolean;
3133
}
3234

3335
export const TypeScriptPlugin: Plugin<TypeScriptPluginOptions> = (
@@ -182,39 +184,57 @@ export const TypeScriptPlugin: Plugin<TypeScriptPluginOptions> = (
182184
webpack.resolve.extensions.push(JS_EXTENSION);
183185
}
184186

185-
if (webpack.mode === "production") {
186-
if (webpack.optimization == null) {
187-
webpack.optimization = {};
188-
}
187+
if (webpack.optimization == null) {
188+
webpack.optimization = {};
189+
}
189190

190-
if (webpack.optimization.minimizer == null) {
191-
webpack.optimization.minimizer = [];
192-
}
191+
webpack.optimization.minimize = config?.isEnvProduction ?? webpack.mode === "production";
193192

194-
let terserOptions: TerserPluginOptions = {
195-
cache: true,
196-
parallel: true,
197-
terserOptions: {
198-
compress: {
199-
dead_code: true,
200-
conditionals: true,
201-
booleans: true
202-
},
203-
module: false,
204-
output: {
205-
comments: false,
206-
beautify: false
207-
}
208-
}
209-
};
193+
if (webpack.optimization.minimizer == null) {
194+
webpack.optimization.minimizer = [];
195+
}
210196

211-
if (config != null && config.terserPluginOptions != null) {
212-
terserOptions = config.terserPluginOptions;
213-
}
197+
let terserOptions: TerserPluginOptions = {
198+
cache: true,
199+
parallel: true,
200+
terserOptions: {
201+
parse: {
202+
// We want terser to parse ecma 8 code. However, we don't want it
203+
// to apply any minification steps that turns valid ecma 5 code
204+
// into invalid ecma 5 code. This is why the 'compress' and 'output'
205+
// sections only apply transformations that are ecma 5 safe
206+
// https://github.com/facebook/create-react-app/pull/4234
207+
ecma: 8
208+
},
209+
compress: {
210+
ecma: 5,
211+
warnings: false,
212+
dead_code: true,
213+
conditionals: true,
214+
booleans: true
215+
},
216+
mangle: {
217+
safari10: true
218+
},
219+
module: false,
220+
output: {
221+
ecma: 5,
222+
comments: false,
223+
// Turned on because emoji and regex is not minified properly using default
224+
// https://github.com/facebook/create-react-app/issues/2488
225+
ascii_only: true,
226+
beautify: false
227+
}
228+
},
229+
sourceMap: config?.shouldUseSourceMap
230+
};
214231

215-
webpack.optimization.minimizer.push(new TerserPlugin(terserOptions) as Plugin);
232+
if (config != null && config.terserPluginOptions != null) {
233+
terserOptions = config.terserPluginOptions;
216234
}
217235

236+
webpack.optimization.minimizer.push(new TerserPlugin(terserOptions) as Plugin);
237+
218238
return webpack;
219239
};
220240
};

0 commit comments

Comments
 (0)