Skip to content

require.context not parsed in ESM packages #15372

Closed

Description

@tavoyne

Bug report

What is the current behavior?

In packages with type: "module", webpack fails to parse and replace require.context() expressions.

If the current behavior is a bug, please provide the steps to reproduce.

  1. Clone https://github.com/tavoyne/webpack-2ece8c.
  2. Run yarn webpack --mode=production.
  3. Inspect the dist/main.js file and notice that the require.context() expression is still present.
  4. (Remove type: "module" from the package.json file.)
  5. (Inspect the dist/main.js file again and notice how the require.context() expression was successfully replaced.)

What is the expected behavior?

The replacement to be successful at step (3).

As require is not available in ESM environments, it could be called import.context in order to avoid confusion.

Other relevant information:
webpack version: 5.68.0
Node.js version: 17.4.0
Operating System: MacOS
Additional tools: N/A

Activity

  1. vankop commented on Feb 11, 2022

    @vankop
    Contributor

    Thanks for your issue. This done intentionally since in esm there is no require, you can mark this js file as type="javascript/auto" in your webpack.config( use https://webpack.js.org/configuration/module/#ruletype )

    Ah.. if you need context you still can use import('/a/a' + name) https://webpack.js.org/api/module-methods/#dynamic-expressions-in-import

  2. added
    webpack-5
    on Feb 11, 2022
  3. tavoyne commented on Feb 11, 2022

    @tavoyne
    Author

    I understand, but the fact that it's called require.context is anecdotical, isn't it? I mean Webpack doesn't use the require function under the hood, it just parses the expression. In this case it's a shame to restrict its usage to CJS environments only, don't you think?

    Ah.. if you need context you still can use import('/a/a' + name)

    I agree this is handy, but I need the filenames.

  4. vankop commented on Feb 11, 2022

    @vankop
    Contributor

    Webpack tries to follow specs. Since esm kind of strict environment there is no cjs stuff in it.

  5. tavoyne commented on Feb 11, 2022

    @tavoyne
    Author

    But require.context is not a cjs stuff, it's webpack's.

  6. alexander-akait commented on Feb 11, 2022

    @alexander-akait
    Member

    require.context exists only because webpack starts when ESM was not ready, so require.context is just hack for import(...), because currently ESM is preferable and we should reduce usage of commonjs, we stop to add new features for require (i.e. CommonJS) and also block them to ESM

  7. tavoyne commented on Feb 11, 2022

    @tavoyne
    Author

    Unless I'm missing something here, it could have been called webpack.context or blabla and work the same way. The fact that it has require in it is arbitrary.

  8. vankop commented on Feb 11, 2022

    @vankop
    Contributor

    @sokra should we add import.meta.context ?

  9. tavoyne commented on Feb 11, 2022

    @tavoyne
    Author

    I agree with you @alexander-akait. But require.context() has an extra functionality compared to import(): it's the fact that you can access filenames.

  10. tavoyne commented on Feb 11, 2022

    @tavoyne
    Author

    Here's an example of what you can do with require.context but not with import().

    const pages = require.context("./pages", true, /\.js$/u, "lazy");
    
    const render = async () => {
      const { pathname } = window.location;
    
      if (pages.keys().includes(`.${pathname}`)) {
        renderHtml(await pages(`.${pathname}`));
      } else {
        renderHtml("Not found");
      }
    };

    It's so powerful.

    @sokra should we add import.meta.context ?

    Could it be even more agnostic, e.g. webpack.context?

  11. alexander-akait commented on Feb 11, 2022

    @alexander-akait
    Member

    @vankop @tavoyne To be honest, I have quite different feelings, on the one hand, I understand well how it is used and why it is needed, on the other hand, we already had the experience of adding new stuff to module system and it always ended badly.

  12. tavoyne commented on Feb 11, 2022

    @tavoyne
    Author

    I understand.

    And what about adding the functionality to import() directly?

    const pages = import(
      /* webpackIsDirectory: true */
      "./pages"
    );

    This would work well with existing options, e.g. webpackMode, webpackPrefetch, webpackInclude, etc.

  13. alexander-akait commented on Feb 11, 2022

    @alexander-akait
    Member

    It changes import logic, it is very bad idea, we should not do it, in this case better to use import.meta.context(), because import.meta.* is free to use, maybe even import.meta.webpackContent to prevent overlapping with other tools

  14. tavoyne commented on Feb 11, 2022

    @tavoyne
    Author

    Makes complete sense. Could work on a PR if it's something that you'd consider integrating.

  15. alexander-akait commented on Feb 11, 2022

    @alexander-akait
    Member

    @sokra What do you think?

  16. sokra commented on Feb 15, 2022

    @sokra
    Member

    import.meta.webpackContext would be the name to choose here.

    But we should use the chance to improve the API a bit and let it take an object instead of many arguments. This would also allow to add the other options a context takes, like include or exclude or mode

  17. maoxiaoke commented on Mar 17, 2022

    @maoxiaoke

    similar phenomenon was found when using require.resolveWeak in a pure ESM package @sokra @alexander-akait

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    webpack-5

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions