4

Hi all I am using webpack 4 and having issues in code-splitting, I have a file main.js that ned to import 5 components that also need to be code-splitted and lazy-loaded with react-router. Now each of the components has their own directory in the source code with an index.js file(it export the component as default export) being the main export file and Is also the file I am importing in main.js for each of the 5 components. I am using lazy loading for these 5 components with React.lazy, but for some reason, they all get bundled into the same chunk for example - packs/js/index.chunk.js. But when I change the name of the main export file(index.js) to something else for each of the 5 components and the file name also is unique for each component then lazy-loading works. What I want is to keep the file name the same as the index.js for each of the components and attain lazy-loading. I tried magic comments like below but had no success with it.

const x1 = lazy(() => import(/* webpackChunkName: "x1" */ './x1'));

Below is the webpack config

    optimization: {
      concatenateModules: false,
      splitChunks: {
        chunks(chunk) {
          return true;
        },
        minSize: 30000,
        minChunks: 1,
        maxAsyncRequests: Infinity,
        maxInitialRequests: Infinity,
        automaticNameMaxLength: 30,
      },
  }                                                                                                                                                                                            
  output: {
    filename: 'js/[name].[chunkHash:6].js',
    chunkFilename: 'js/[name].[chunkHash:6].chunk.js',
    publicPath: `${process.env.CDN_HOST}/packs/`,
  }

1 Answer 1

0

see this doc: https://v4.webpack.js.org/guides/code-splitting/#prevent-duplication

https://v4.webpack.js.org/plugins/split-chunks-plugin/

optimization: {
  splitChunks: {
    chunks: 'all',
  },
},

==========

optimization: {
  splitChunks: {
    chunks: 'all',
    minSize: 30000,
    minChunks: 1,
    maxAsyncRequests: Infinity,
    maxInitialRequests: Infinity,
    automaticNameMaxLength: 30,
  },
},
Sign up to request clarification or add additional context in comments.

1 Comment

hi, Mahdi thanks for taking time for answering this question, but I have tried the above solution and it doesn't work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.