sindresorhus/refined-github

Webpack issue: some exports not tree-shaken #2996

fregante posted onGitHub

Since #2512, all page-detect’s tests have been included in the final bundle and have not been tree-shaken as I was expecting.

Here's the what the output looks like:


  const isOwnOrganizationProfile = () => isOrganizationProfile() && !select_dom__WEBPACK_IMPORTED_MODULE_0__["a"].exists('[href*="contact/report-abuse?report="]');
  const _isOwnOrganizationProfile = domBased;
  const isProject = () => /^projects\/\d+/.test(Object(_utils__WEBPACK_IMPORTED_MODULE_2__["k"])());
  const _isProject = [ "https://github.com/sindresorhus/refined-github/projects/3" ];

Exports starting with _ should not be included in the final bundle.


Found the culprits:

  1. Using the rest parameter here creates a new object that Webpack+Terser lose track of: https://github.com/sindresorhus/refined-github/blob/66544c0fa1ce0c081c81f92be460ff8806f647ad/source/libs/features.tsx#L218-L220

It should be replaced with:

// features.tsx
export * as pageDetect from './page-detect';

// every feature
import features, {pageDetect} from './features';

Or simply:

// every feature
import features from './features';
import * as pageDetect from './page-detect';
  1. Compression was disabled in Webpack; it also disables tree-shaking

https://github.com/sindresorhus/refined-github/blob/66544c0fa1ce0c081c81f92be460ff8806f647ad/webpack.config.ts#L159-L163

It should probably be:

compress: {
    defaults: false,
    dead_code: true,
    unused: true
},
  1. .concat() throws off three-shaking, it should be replaced with the rest operator

https://github.com/sindresorhus/refined-github/blob/66544c0fa1ce0c081c81f92be460ff8806f647ad/source/libs/page-detect.ts#L326-L330

This should follow #2987

posted by fregante almost 5 years ago

Fund this Issue

$0.00
Funded

Pull requests