Skip to content

import.meta.webpackContext - #15446

Merged
sokra merged 3 commits into
mainfrom
feature/import-meta-webpack-context
Mar 3, 2022
Merged

sokra merged 3 commits into
mainfrom
feature/import-meta-webpack-context

Conversation

@vankop

@vankop vankop commented Feb 26, 2022

Copy link
Copy Markdown
Contributor

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

webpack-bot commented Feb 26, 2022 •

Copy link
Copy Markdown
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)

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

@sokra sokra left a comment

Copy link
Copy Markdown
Member

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
Copy Markdown
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
Comment thread test/cases/chunks/context-weak/index.js Resolved
"webpack/lib/dependencies/ImportMetaContextDependency"
);

ImportMetaContextDependency.Template = ModuleDependencyTemplateAsRequireId;

Copy link
Copy Markdown
Contributor 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
Copy Markdown
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
Copy Markdown
Contributor

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

@Tofandel

Tofandel commented Mar 24, 2022 •

Copy link
Copy Markdown

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
Copy Markdown
Member

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

@Tofandel

Tofandel commented Mar 24, 2022 •

Copy link
Copy Markdown

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

Tofandel commented Mar 24, 2022 •

Copy link
Copy Markdown

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

Tofandel commented Mar 25, 2022 •

Copy link
Copy Markdown

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

vankop commented Mar 25, 2022

Copy link
Copy Markdown
Contributor Author

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

require.context not parsed in ESM packages

5 participants