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

feat: add comments abilities to require.context #13306

Closed
wants to merge 3 commits into from
Closed

feat: add comments abilities to require.context #13306

wants to merge 3 commits into from

Conversation

Tofandel
Copy link

@Tofandel Tofandel commented May 4, 2021

Fixes #9184 by adding the special comments feature from import() to require.context()

  • Tests

@webpack-bot
Copy link
Contributor

For maintainers only:

  • This needs to be documented (issue in webpack/webpack.js.org will be filed when merged)
  • This needs to be backported to webpack 4 (issue will be created when merged)

@Tofandel Tofandel marked this pull request as ready for review May 5, 2021 02:56
@Tofandel Tofandel changed the title Add chunkName to require.context #9184 Add comments abilities to require.context #9184 May 5, 2021
@Tofandel Tofandel changed the title Add comments abilities to require.context #9184 Add comments abilities to require.context May 5, 2021
@Tofandel Tofandel changed the title Add comments abilities to require.context feat: add comments abilities to require.context May 5, 2021
@Tofandel
Copy link
Author

Tofandel commented May 6, 2021

Failure is a random fail in all pipelines on macOs

https://dev.azure.com/webpack/webpack/_pipeline/analytics/stageawareoutcome?definitionId=3

@asos-tomp
Copy link

Would love if this could be backported to Webpack 4...

# Conflicts:
#	lib/dependencies/ImportParserPlugin.js
#	package.json
# Conflicts:
#	lib/dependencies/ImportParserPlugin.js
#	package.json
@webpack-bot
Copy link
Contributor

Thank you for your pull request! The most important CI builds succeeded, we’ll review the pull request soon.

@Tofandel
Copy link
Author

First somebody needs to review this PR for webpack 5 😅

@alexander-akait
Copy link
Member

What is use case?

@Tofandel
Copy link
Author

Tofandel commented Jul 23, 2021

@alexander-akait Described in #9184 To be able to specify chunk names (and all other comment features) when using require.context() in the same way you use the async import(), it's currently not possible to give a chunk name to files required with require.context and because they both have the same inner options, it makes sense to give them the same way to pass them

@alexander-akait
Copy link
Member

Why do not use import()? We are trying to reduce non JS abilities due incompatibility with ECMA modules

@Tofandel
Copy link
Author

Tofandel commented Jul 23, 2021

Well, you can't match by regex with import, and you can't get a list of files, so localization is the best use case for require.context, for instance:

  const locales = require.context('@/locales', false, /[A-Za-z0-9-_,\s]+\.json$/i);
  const vuetify = require.context('vuetify/lib/locale', false, /[A-Za-z0-9-_,\s]+\.js$/i);

Because with this you can enumerate/iterate the available locales which import cannot do

@zaqqaz
Copy link

zaqqaz commented Feb 23, 2022

@alexander-akait , @Tofandel are there any updates on it ?

There are quite a few use cases when you may need to dynamically load files by specific regex or with the dynamic value, like:

const importTheme = require.context(
    'codemirror/theme/',
    false,
    /\.css$/,
    'lazy',
);

// ... some-where later in you code
await importTheme(/* webpackChunkName: 'CodeEditor-theme' */ `./${theme}.css`);
// theme is ready;

@vankop
Copy link
Member

vankop commented Mar 8, 2022

please use import.meta.webpackContext #15446

@vankop vankop closed this Mar 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow webpackChunkName to be specified when using require.context
6 participants