Skip to content

Does tree shaking of barrel files with babel-swc-loader even work? #1314

@mefjuu

Description

@mefjuu

Describe the bug

Has anybody got tree shaking to work with the new @callstack/repack/babel-swc-loader?

As rspack docs says, modules cannot be converted to CJS to utilize tree shaking. However when I configure the @react-native/babel-preset in babel.config.js to do so:

  presets: [
    [
      'module:@react-native/babel-preset', { disableImportExportTransform: true },
    ],
  ],
  ...

bundler throws error:

  error: [Error [TypeError]: Cannot read properties of undefined (reading 'type')
      at Proxy.babelSwcLoader (/Users/m/project/node_modules/@callstack/repack/dist/loaders/babelSwcLoader/babelSwcLoader.js:111:32)]

I've also tried to use babelOverrides property instead and the error is gone, but there is still no tree shaking result.

      {
        test: /\.[cm]?[jt]sx?$/,
        type: 'javascript/auto',
        use: {
          loader: '@callstack/repack/babel-swc-loader',
          parallel: true,
          options: {
            lazyImports: true,
            babelOverrides: {
              presets: [
                ['module:@react-native/babel-preset', { disableImportExportTransform: true }],
              ],
            }
          },
        },
      },

System Info

"@callstack/repack": "5.2.2",
"react-native": "0.81.4",
"@react-native/babel-preset"

Re.Pack Version

5.2.2

Reproduction

https://github.com/mefjuu/repack-optimizations-test

Steps to reproduce

  1. Clone the repo https://github.com/mefjuu/repack-optimizations-test
  2. Run
      npm run bundle -- --entry-file ./index.js && cat build/generated/ios/index.bundle | grep "sernik"
    
  3. The phrase "sernik" has been found in bundled file even though the Sernique.tsx file is not imported in App.tsx (however it's located in components/index.ts barrel file).

Ok, let's get aligned with that rspack docs says and:

  1. Put { disableImportExportTransform: true } at module:@react-native/babel-preset config in babel.config.js
  2. Run npm run bundle -- --entry-file ./index.js
  3. Error: ... [Error [TypeError]: Cannot read properties of undefined (reading 'type') ...

Metadata

Metadata

Assignees

Labels

status:confirmedThe issue has been reviewed and all necessary information is provided.type:bugA bug report.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions