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

import.meta.webpackContext #15446

Merged
merged 3 commits into from Mar 3, 2022
Merged

import.meta.webpackContext #15446

merged 3 commits into from Mar 3, 2022

Conversation

vankop
Copy link
Member

@vankop vankop commented Feb 26, 2022

What kind of change does this PR introduce?

feature
closes #15372

Did you add tests for your changes?
yes

Does this PR introduce a breaking change?
no

What needs to be documented once your changes are merged?
import.meta.webpackContext("directory", { recursive: boolean, regExp: RegExp, mode }) returns same as require.context but only for javascript/auto, javascript/esm

@webpack-bot
Copy link
Contributor

webpack-bot commented Feb 26, 2022

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)

@vankop vankop force-pushed the feature/import-meta-webpack-context branch from 0fd43e0 to 2764e91 Compare February 26, 2022 20:26
Copy link
Member

@sokra sokra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Best see lib/dependencies/ImportParserPlugin.js how all these options need to be handled.

@webpack-bot
Copy link
Contributor

@vankop Thanks for your update.

I labeled the Pull Request so reviewers will review it again.

@sokra Please review the new changes.

@vankop vankop force-pushed the feature/import-meta-webpack-context branch 2 times, most recently from 705967f to e6f9968 Compare March 1, 2022 13:52
@vankop vankop force-pushed the feature/import-meta-webpack-context branch from e6f9968 to b89f397 Compare March 1, 2022 14:17
lib/dependencies/RequireContextPlugin.js Outdated Show resolved Hide resolved
lib/dependencies/RequireContextPlugin.js Outdated Show resolved Hide resolved
test/cases/chunks/context-weak/index.js Outdated Show resolved Hide resolved
"webpack/lib/dependencies/ImportMetaContextDependency"
);

ImportMetaContextDependency.Template = ModuleDependencyTemplateAsRequireId;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also is this correct one?


const handler = (parser, parserOptions) => {
if (
parserOptions.importMetaContext !== undefined &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This option need to be added to the schema

@webpack-bot
Copy link
Contributor

I've created an issue to document this in webpack/webpack.js.org.

@Tofandel
Copy link

Tofandel commented Mar 24, 2022

There seems to be a bug when using it with a package

  const locales = import.meta.webpackContext('@/locales', {
    recursive: false,
    regExp: /(en|hu)\.json$/i,
  });
  const vuetify = import.meta.webpackContext('vuetify/lib/locale', {
    recursive: false,
    regExp: /(en|hu)\.js$/i,
  });

First one works perfect, second one throws warning during compilation

 WARNING  Compiled with 2 warnings                                                                                                                                                                                             6:09:41 PM

 warning  in ./node_modules/vuetify/lib/locale/ ./node_modules/vuetify/lib/locale/ sync nonrecursive (en%7Chu)\.js$/

Module not found: Error: Can't resolve './node_modules/vuetify/lib/locale/vuetify/lib/locale/en.js' in '***'

 warning  in ./node_modules/vuetify/lib/locale/ ./node_modules/vuetify/lib/locale/ sync nonrecursive (en%7Chu)\.js$/

Module not found: Error: Can't resolve './node_modules/vuetify/lib/locale/vuetify/lib/locale/hu.js' in '***'

It seems to be using the context twice vuetify/lib/locale/vuetify/lib/locale/en.js instead of vuetify/lib/locale/en.js or am I missing something?

@alexander-akait
Copy link
Member

@Tofandel Looks like yes, please open an issue, thanks

@Tofandel
Copy link

Tofandel commented Mar 24, 2022

Managed to get a minimal repro (funny enough only seems to happen with vue-cli, didn't happen with webpack-cli only) so something funny with the config is going on, so will open one

@Tofandel
Copy link

Tofandel commented Mar 24, 2022

Got it to reproduce without vue-cli

Just adding this in the webpack config

const path = require('path')
module.exports = {
  resolve: {
    modules: [
      'node_modules',
      path.resolve(__dirname, './node_modules'),
    ]
  },
}

@Tofandel
Copy link

Tofandel commented Mar 25, 2022

Also, not really a bug, but just exploring this without a doc

I found that the default mode is sync and so if using chunkName without a mode it will have no effect, maybe we can change the default to 'lazy-once' if a chunkName is given? Otherwise I'd add a compilation warning if chunkName is given with a mode that doesn't use the chunkName

const vuetify = import.meta.webpackContext('vuetify/lib/locale', {
  recursive: false,
  chunkName: 'locales', // This does nothing until we add mode: 'lazy' or mode: 'lazy-once'
  regExp: /(en|hu)\.js$/i,
});

@vankop
Copy link
Member Author

vankop commented Mar 25, 2022

Also, not really a bug, but just exploring this without a doc

I found that the default mode is sync and so if using chunkName without a mode it will have no effect, maybe we can change the default to 'lazy-once' if a chunkName is given? Otherwise I'd add a compilation warning if chunkName is given with a mode that doesn't use the chunkName

const vuetify = import.meta.webpackContext('vuetify/lib/locale', {
  recursive: false,
  chunkName: 'locales', // This does nothing until we add mode: 'lazy' or mode: 'lazy-once'
  regExp: /(en|hu)\.js$/i,
});

yeah, need to add a warning if not lazy* mode

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.

require.context not parsed in ESM packages
5 participants