question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

perf problems with `require.context`

See original GitHub issue

@sokra this issue is very much alive in webpack5 and also affects dynamic require() and import() calls. I created another repro, although probably @ericpitcher’s is just as good: https://github.com/tmeasday-shopify/webpack-require-context-repro

We are very interested about this for Storybook in large code bases because we rely on require.context() or some other globbing mechanism to import story files.

Would you like me to open a new ticket or can this ticket be reopened?

_Originally posted by @tmeasday in https://github.com/webpack/webpack/issues/6093#issuecomment-868293507_

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:32
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

4reactions
vidal7commented, Oct 13, 2021

@alexander-akait, sure instead of doing

module.exports = {
    stories: [
        './src/**/*.stories.ts',
    ]
};

I am doing something like this. I oversimplified my code just for this example.

const { sync } = require('glob');

module.exports = {
    stories: sync('./src/**/*.stories.ts')
};
2reactions
alexander-akaitcommented, Oct 14, 2021

In theory you can create entry.js file and apply loader, inside loader run glob and create imports like:

import oneStory from "./one-story/index.js";
import twoStory from "./two-story/index.js";

inside loader, use glob-parent (i.e. extract src directory) and use this.addContextDependency https://webpack.js.org/api/loaders/#thisaddcontextdependency, it can be some slow on initial run, but will be fast for incremental builds, also it will tract all changes inside src directory, i.e. we just generate entry file based on glob.

But why do not use https://webpack.js.org/configuration/experiments/#experimentslazycompilation with import()? All stories can be loaded using import(). If you need to add new story just add new lines to your source file with stories. This is actually very close to how it works in browser, even more it can work in browser without bundlers (in theory). WE don’t have glob/require.context in browser and we should migrate from these hacky workarounds.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Webpack loaders in require.context on a Typescript create ...
It turns out the culprit is react-scripts@5.0.0. Downgrading to @4.0.3 fixed the problem. Incidentally, @5.0.0 had other problems with svg ...
Read more >
Dependency Management - webpack
A context module is generated. It contains references to all modules in that directory that can be required with a request matching the...
Read more >
An in-depth guide to performance optimization with webpack
A good understanding of webpack's underlying principles can vastly improve your bundle size, application performance, and user experience.
Read more >
The Tableau Performance Checklist: Filtering - Avoid Quick ...
These require reloading the context table and should be avoided ... set in the new context table, it can create big performance problems....
Read more >
Optimizing Performance - React
Use the Production Build. If you're benchmarking or experiencing performance problems in your React apps, make sure you're testing with the minified production ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found