Skip to content
This repository has been archived by the owner on Jan 18, 2024. It is now read-only.

Commit

Permalink
fix(webpack-config): fix import export treeshaking (#4476)
Browse files Browse the repository at this point in the history
* fix(webpack-config): fix import export treeshaking

* fix tests

* Update webpack.config-test.js.snap

* Update yarn.lock
  • Loading branch information
EvanBacon committed Jul 25, 2022
1 parent 242c32c commit 7f7242b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
Expand Up @@ -98,6 +98,9 @@ Object {
],
"strictExportPresence": false,
},
"optimization": Object {
"usedExports": false,
},
"output": Object {
"chunkFilename": "static/js/[name].chunk.js",
"devtoolModuleFilenameTemplate": [Function],
Expand Down Expand Up @@ -309,6 +312,7 @@ Object {
"chunks": "all",
"name": false,
},
"usedExports": false,
},
"output": Object {
"chunkFilename": "static/js/[name].[contenthash:8].chunk.js",
Expand Down Expand Up @@ -528,6 +532,9 @@ Object {
"net": "empty",
"tls": "empty",
},
"optimization": Object {
"usedExports": false,
},
"output": Object {
"chunkFilename": "static/js/[name].chunk.js",
"devtoolModuleFilenameTemplate": [Function],
Expand Down Expand Up @@ -794,6 +801,7 @@ Object {
"chunks": "all",
"name": false,
},
"usedExports": false,
},
"output": Object {
"chunkFilename": "static/js/[name].[contenthash:8].chunk.js",
Expand Down
Expand Up @@ -13,7 +13,9 @@ it(`only uses optimizations in production`, () => {
withOptimizations({
mode: 'development',
}).optimization
).not.toBeDefined();
).toEqual({
usedExports: false,
});
});

it(`doesn't overwrite existing optimizations`, () => {
Expand Down
9 changes: 9 additions & 0 deletions packages/webpack-config/src/addons/withOptimizations.ts
Expand Up @@ -22,13 +22,22 @@ export function isDebugMode(): boolean {
*/
export default function withOptimizations(webpackConfig: AnyConfiguration): AnyConfiguration {
if (webpackConfig.mode !== 'production') {
webpackConfig.optimization = {
...webpackConfig.optimization,
// Required for React Native packages that use import/export syntax:
// https://webpack.js.org/configuration/optimization/#optimizationusedexports
usedExports: false,
};
return webpackConfig;
}
const shouldUseSourceMap = typeof webpackConfig.devtool === 'string';

const _isDebugMode = isDebugMode();

webpackConfig.optimization = {
// Required for React Native packages that use import/export syntax:
// https://webpack.js.org/configuration/optimization/#optimizationusedexports
usedExports: false,
...(webpackConfig.optimization || {}),
nodeEnv: false,
minimize: true,
Expand Down
5 changes: 2 additions & 3 deletions packages/webpack-config/src/webpack.config.ts
Expand Up @@ -587,9 +587,8 @@ export default async function (
webpackConfig.target = 'webworker';
}

if (isProd) {
webpackConfig = withOptimizations(webpackConfig);
} else {
webpackConfig = withOptimizations(webpackConfig);
if (!isProd) {
webpackConfig = withDevServer(webpackConfig, env, {
allowedHost: argv.allowedHost,
proxy: argv.proxy,
Expand Down

0 comments on commit 7f7242b

Please sign in to comment.