callstack / repack

A Webpack-based toolkit to build your React Native application with full support of Webpack ecosystem.
https://re-pack.dev
MIT License
1.46k stars 104 forks source link

ChunkLoadError on Android #55

Closed gedeagas closed 3 years ago

gedeagas commented 3 years ago

Environment

Android 10. React Natvie 64.2

Description

So my build is finished successfully, the main bundle is generated but somehow the chunk is not getting copied to the APK resulting in ChunkLoadError.

Screen Shot 2021-07-15 at 8 07 25 PM

When I run manually with this command, the chuck is in the correct assets folder. npx react-native webpack-bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle

Screen Shot 2021-07-15 at 7 54 03 PM

So I suppose the problem is that the bundle is not getting copied by react-native Gradle because the APK doesn't have any of the chunks that they are supposed to have.

Screen Shot 2021-07-15 at 8 05 28 PM
zamotany commented 3 years ago

Can you prepare a repro project? Something is wrong with the way your project is being built, because it should never load chunk with such URI - noop://.

zamotany commented 3 years ago

Also make sure you're building with dev=false from Gradle. npx react-native webpack-bundle ... works because you're passing --dev false manually, but it looks like the build from Gradle is passing dev as true.

gedeagas commented 3 years ago

Also make sure you're building with dev=false from Gradle. npx react-native webpack-bundle ... works because you're passing --dev false manually, but it looks like the build from Gradle is passing dev as true.

let me double check again. And i will try to create a repro project.

Thanks for the swift response @zamotany

gedeagas commented 3 years ago

@zamotany i can't make the repro yet but i just added a logger in the webpack config.

The build from Gradle is indeed on production mode.

Screen Shot 2021-07-17 at 12 47 22 AM

I even manually disabled the hmr.

entry: [
    ...ReactNative.getInitializationEntries(reactNativePath, {
      hmr: false,
    }),
    entry,
  ],
DanijelBojcic commented 2 years ago

I'm getting the same error in release builds. ( both android and iOS )

Description

It seems like the local chunks are not included in the apk. When I inspect the iOS .app file there is only a main.jsbundle.

I have checked the dev variable in the config and its false.

Crashlytics stack trace:

Fatal Exception: com.facebook.react.common.c: ChunkLoadError: Loading chunk 9557 failed.
(resolution: 9557)

Versions:

repack: 2.5.1,
react-native: 0.65.1,
webpack: 5.65.0,
zamotany commented 2 years ago

It seems like the local chunks are not included in the apk.

Have you specified localChunks property in OutputPlugin in your Webpack config?

DanijelBojcic commented 2 years ago

I think so...

  new ReactNative.OutputPlugin({
      platform,
      devServerEnabled: devServer.enabled,
      remoteChunksOutput: path.join(__dirname, "build", platform, "remote"),
      localChunks: /\.chunk\.(js)?bundle$/,
    }),
DanijelBojcic commented 2 years ago

Maybe I should have mentioned that we use monorepo and we have multiple targets/flavors.

zamotany commented 2 years ago

@DanijelBojcic localChunks matches only chunk name (or id if can cannot be obtained), so /\.chunk\.(js)?bundle$/ won't match anything. If you want to match everything you should provide /.*/ instead. If you want only specific chunks I suggest to:

  1. Use /* webpackChunkName: "myChunk" */ in import()
  2. Set localChunks: ['myChunk'] or localChunks: [/myChunk/]
andreszuluaga-wolox commented 2 years ago

@zamotany how to generate the apk and the ipa to include the local chunks

zamotany commented 2 years ago

Local chunks should be included in the apk/ipa by default. Are you getting any errors?

jd1378 commented 2 years ago

I have the same problem after setting localChunks to [/.*/] it seems the chunks make their way into the apk, but it seems to me that despite using webpackChunkName in import(), and using chunkFilename: '[name].chunk.bundle', in webpack config, chunks still get named randomly (like 462.chunk.bundle) So it fails

I get a different error in logs though:

'ChunkManager.resolveChunk error:', 'No remote chunk resolver was provided. Did you forget to add `ChunkManager
.configure({ resolveRemoteChunk: ... })`?'