Skip to content

Support import.meta.resolve #16693

Closed

Description

@latin-1

Feature request

What is the expected behavior?

const url = import.meta.resolve("...");

Note: This differs from the Node.js' experimental version, which is asynchronous.12

I'm not sure whether it should act like require.resolve or new URL.

What is motivation or use case for adding/changing the behavior?
https://html.spec.whatwg.org/#hostgetimportmetaproperties
https://chromestatus.com/feature/5086507990777856

How should this be implemented in your opinion?
.

Are you willing to work on this yourself?
no

Footnotes

  1. https://github.com/nodejs/modules/issues/550#issuecomment-685146852 ↩

  2. https://github.com/whatwg/html/pull/5572#issuecomment-1041876685 ↩

Activity

  1. added
    help wanted
    webpack-5
    on Feb 10, 2023
  2. TheLarkInn commented on Apr 16, 2023

    @TheLarkInn
    Member

    @devongovett @lukastaegert lets try to align on this picking which way to go on this so theres not a freaking mess .

  3. lukastaegert commented on Apr 16, 2023

    @lukastaegert

    While Rollup has facilities to let plugins add any implementation that they fancy, a require.resolve based
    implementation will likely never make it into Rollup core. After all, Rollup does not even support require, and it feels less standard than the browser implementation. So I would definitely lean towards the new URL implementation, with the option for plugins to override this.

  4. devongovett commented on Apr 16, 2023

    @devongovett

    As I mentioned in the above parcel issue, I think it is weird that import.meta.resolve would influence the resolution rather than only resolving to what is already there.new URL('...', import.meta.url) creates a dependency, therefore moving the target module or asset to a separate file/bundle, and it would be weird if import.meta.resolve did that IMO. That would mean using import.meta.resolve would change the file structure of the output vs not using it. Maybe providing a URL reference to the resolved module within whatever bundle it ends up in via other dependencies would be more expected?

  5. webpack-bot commented on Jul 17, 2023

    @webpack-bot
    Contributor

    This issue had no activity for at least three months.

    It's subject to automatic issue closing if there is no activity in the next 15 days.

  6. added
    status: stale
    on Jul 17, 2023
  7. webpack-bot commented on Aug 1, 2023

    @webpack-bot
    Contributor

    Issue was closed because of inactivity.

    If you think this is still a valid issue, please file a new issue with additional information.

  8. latin-1 commented on Aug 1, 2023

    @latin-1
    Author

    not stale

  9. removed
    status: stale
    on Aug 7, 2023
  10. moved this to Priority - Medium in webpack 5/6on Aug 7, 2023
  11. 6 remaining items

  12. removed
    status: stale
    on Jul 23, 2024
  13. alexander-akait commented on Aug 1, 2024

    @alexander-akait
    Member

    Also we need to implement import.meta.resolveWeak, ref #18633

  14. added
    status: stale
    on Nov 1, 2024
  15. latin-1 commented on Nov 17, 2024

    @latin-1
    Author
  16. removed
    status: stale
    on Nov 19, 2024
  17. RReverser commented on Dec 22, 2024

    @RReverser
    Contributor

    new URL('...', import.meta.url) creates a dependency, therefore moving the target module or asset to a separate file/bundle, and it would be weird if import.meta.resolve did that IMO.

    I don't see why that would be weird or why they should behave differently. In my view, import.meta.resolve is a direct replacement for new URL('...', import.meta.url), just with a proper "meta" API instead of pattern-matching a somewhat brittle JS expression that is matched across tooling only by convention.

  18. lgarron commented on Jan 4, 2025

    @lgarron

    new URL('...', import.meta.url) creates a dependency, therefore moving the target module or asset to a separate file/bundle, and it would be weird if import.meta.resolve did that IMO.

    I don't see why that would be weird or why they should behave differently. In my view, import.meta.resolve is a direct replacement for new URL('...', import.meta.url), just with a proper "meta" API instead of pattern-matching a somewhat brittle JS expression that is matched across tooling only by convention.

    I strongly agree with this. import.meta.resolve(…) was designed as a standardized way to do what some bundlers have been doing by detecting new URL(…, import.meta.url). It also comes with clear semantics for bare module names (i.e. resolving a package rather than a relative path):
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import.meta/resolve#comparison_with_new_url

    Having a standardized way to do this is absolutely crucial for those of us who maintain packages that include web workers or WASM.

  19. added
    status: stale
    on Apr 9, 2025
  20. jnields commented on Apr 15, 2025

    @jnields

    bump

  21. removed
    status: stale
    on Apr 15, 2025
  22. lgarron commented on Jul 25, 2026

    @lgarron

    I'm able to confirm that v5.109.0 handles import.meta.resolve(…) correctly resolves and includes paths in the import graph for local project code:

    const workerURL = import.meta.resolve("./relative.js"); // Assuming `./relative.js` exists.
    const importSrc = `import "${workerURL}";`;
    const blob = new Blob([importSrc], {
      type: "text/javascript",
    });
    new Worker(URL.createObjectURL(blob), { type: "module" });

    That is very exciting!

    However, I'm unable to confirm whether this works with a library (cubing.js), because:

    • npx create-webpack-app creates a projects that crashes for me.
    • Trying to compile the following simple project crashes:
    # bash
    
    mkdir ./test/ && cd ./test/
    
    mkdir -p ./src && cat <<HEREDOC > ./src/index.js
    import { randomScrambleForEvent } from "cubing/scramble";
    (await randomScrambleForEvent("333")).log();
    HEREDOC
    
    cat <<HEREDOC > package.json
    {
      "type": "module",
      "devDependencies": {
        "webpack": "5.109.0",
        "webpack-cli": "7.2.1"
      },
      "dependencies": {
        "cubing": "0.63.3"
      }
    }
    HEREDOC
    
    npm install
    npx webpack

    Output:

    ERROR in 8b18a8cf57ffbfd052a4.js
    8b18a8cf57ffbfd052a4.js from Terser plugin
    Unexpected token: name (import) [8b18a8cf57ffbfd052a4.js:7,undefined]
        at js_error (/private/tmp/test/test/node_modules/terser/dist/bundle.min.js:577:11)
        at croak (/private/tmp/test/test/node_modules/terser/dist/bundle.min.js:1377:9)
        at token_error (/private/tmp/test/test/node_modules/terser/dist/bundle.min.js:1385:9)
        at unexpected (/private/tmp/test/test/node_modules/terser/dist/bundle.min.js:1391:9)
        at semicolon (/private/tmp/test/test/node_modules/terser/dist/bundle.min.js:1429:56)
        at simple_statement (/private/tmp/test/test/node_modules/terser/dist/bundle.min.js:1707:73)
        at statement (/private/tmp/test/test/node_modules/terser/dist/bundle.min.js:1515:19)
        at _embed_tokens_wrapper (/private/tmp/test/test/node_modules/terser/dist/bundle.min.js:1442:26)
        at block_ (/private/tmp/test/test/node_modules/terser/dist/bundle.min.js:2295:20)
        at statement (/private/tmp/test/test/node_modules/terser/dist/bundle.min.js:1527:29)
    
    webpack 5.109.0 compiled with 1 error and 1 warning in 1989 ms
    
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

    help wantedwebpack-5

    Type

    No type

    Fields

    Priority

    None yet

    Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions