Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot read property 'pop' of undefined #341

Closed
a-aslan opened this issue Jan 20, 2019 · 12 comments
Closed

Cannot read property 'pop' of undefined #341

a-aslan opened this issue Jan 20, 2019 · 12 comments

Comments

@a-aslan
Copy link

a-aslan commented Jan 20, 2019

I'm running Node v8.11.4 on Windows 10 version 1803.

I have a minimal project showcasing the issue on: https://github.com/a-aslan/webpack-dynamic-imports

The issue is that when I'm using mini-css-extract-plugin with dynamic imports, I'm getting the following error on a webpack build:

ERROR in chunk app
app.app.css
Cannot read property 'pop' of undefined

I've also recorded the issue on: https://youtu.be/b0Q312y2LSU

How to reproduce the issue:

  • clone the minimal project git repository on: https://github.com/a-aslan/webpack-dynamic-imports
  • git checkout master to see a simple hello world project with style-loader and bootstrap
  • git checkout css-extract to see the project still working now with the bootstrap css extracted
  • git checkout dynamic-imports to see a project where I added react-mapbox-gl with the style-loader working (so mini-css-extract-plugin is not applied in this branch) and the library is being split into vendors~mapbox.js
  • git checkout merge-dynamic-imports-with-css-extract to see the previous two branches merged on how to reproduce the issue

As you can see both extracting css and dynamic imports works when they are excluded from eachother. But when I want to extract css in combination with code splitting (by using dynamic imports), I'm getting the issue.

Am I doing something wrong?

@alexander-akait
Copy link
Member

@a-aslan it is strange, maybe problem in babel, anyway feel free to investigate and send a PR

@tmeme
Copy link

tmeme commented Apr 9, 2019

#220

@khades
Copy link

khades commented Mar 4, 2020

I did some research.

THIS IS EDITED, my previous observation was wrong.

In case of dynamic imports while calling that function

renderContentAsset(compilation, chunk, modules, requestShortener) {

we are getting more modules than chunk needs to render.

Loader compares size usedModules with size of modules.

while (usedModules.size < modules.length) {

BUT when walking through all modulesByChunkGroups

for (const list of modulesByChunkGroup) {

it find that all list elements are used (listed in userModules set)

while (list.length > 0 && usedModules.has(list[list.length - 1])) {

In case when ALL modules in modulesByChunkGroup are in usedModules, even if not all modules are used, it neither sets value for variable "bestMatch", nor setting "success" to true. In that case it can not .pop() from undefined value

const fallbackModule = bestMatch.pop();

My proposal is to check existence of value in "bestMatch", if there not any - just break the cycle before trying to pop the bestMatch, since all modules, that needs rendering got their dependencies resolved.

@tusharshuklaa
Copy link

Is there any workaround for this, I'm also facing this issue.

@fivethreeo
Copy link

Just dont use this with HMR, just use in a production build. So skip this loader and plugin in dev and use style-loader in dev.

@fivethreeo
Copy link

Feel free to contact me for support 😀

@khades
Copy link

khades commented Mar 23, 2020

Is there any workaround for this, I'm also facing this issue.

only by modifying mini-css-extract-plugin itself. Before line

const fallbackModule = bestMatch.pop();

add code

if (!bestMatch) {
    break;
}

I think that's the right way, cause bestMatch is not filled when all chunks have all modules resolved.

@kavience
Copy link

kavience commented Jun 4, 2020

@khades Thx, dude, you are greate

@ghost
Copy link

ghost commented Aug 17, 2020

any news about this? how to fix this issue?

@alexander-akait
Copy link
Member

Found a problem

@alexander-akait
Copy link
Member

Original:

optimization: {
    splitChunks: {
      cacheGroups: {
        styles: {
          name: 'app',
          test: /\.css$/,
          chunks: 'all',
          enforce: true,
        },
      },
    },
  },

Need to changed to:

optimization: {
    splitChunks: {
      cacheGroups: {
        styles: {
          // Just get any other name differing from entrypoints names
          name: 'app-other',
          test: /\.css$/,
          chunks: 'all',
          enforce: true,
        },
      },
    },
  },

Why? Because app.js from splitChunks conflicts with app.js endpoint

entry: {
  app: './src/index.js',
},

Anyway you will have empty chunk due #85 and webpack/webpack#11088, we will fix it on webpack@5 side, maybe backport on plugin side in near future

I think we can also improve error reporting here with some advice if we can't solve it.

@alexander-akait
Copy link
Member

Let's close in favor #257, anyway if somebody still have the problem and solution from #341 (comment) not help you please open a new issue with reproducible test repo, I am very sorry for the long delay in the reply and please forgive us. Also I will try to improve error reporting in near future, it is in my TODO

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants