Skip to content

resolveId doesn’t seem to work in webpack #19

Open

Description

@Codpoe

I use resolveId and load to implement the plugin logic:

export default createUnplugin(() => ({
  name: 'unplugin-starter',
  resolveId(id) {
    return id === 'my-unplugin-virtual-id' ? id : null
  },
  load(id) {
    if (id === 'my-unplugin-virtual-id')
      return 'export default \'hello my-unplugin\''
  },
}))

When I used this plugin in webpack, it reported an error. But it's ok in vite.

import str from 'my-unplugin-virtual-id'
Module not found: Error: Can't resolve 'my-unplugin-virtual-id' in 'D:\Projects\my-unplugin\playground\webpack\src'

This is my minimal demo that can reproduce the problem:https://github.com/Codpoe/unplugin-minimal-demo.
And I had this problem on Windows, I haven’t tested it on Mac...

Activity

  1. tropicalraisel commented on Sep 12, 2021

    @tropicalraisel
    Contributor

    When importing, you need to do something like my-unplugin-virtual-id/{framework}, where framework is either webpack, rollup, or vite.

    Just out of curiosity, why are you using the load statement like that? It seems unnecessary.

  2. Codpoe commented on Sep 13, 2021

    @Codpoe
    Author

    When importing, you need to do something like my-unplugin-virtual-id/{framework}, where framework is either webpack, rollup, or vite.

    Sorry, I don't understand what you mean. I think my plugin has been set up correctly:

    module.exports = {
      entry: './src/index.js',
      plugins: [
        require('my-unplugin/webpack').default(),
      ],
    }

    why are you using the load statement like that? It seems unnecessary.

    I’m just giving a simplest example.😂

  3. tropicalraisel commented on Sep 13, 2021

    @tropicalraisel
    Contributor

    I'm saying that your "import str" statement is strange b/c:

    1. There's no "str" being exported from the plugin
    2. You should be importing "my-unplugin-virtual-id/webpack" in this statement and in webpack.config.js.
  4. Codpoe commented on Sep 13, 2021

    @Codpoe
    Author
    • the name of the plugin is my-plugin
    • my-unplugin-virtual-id is the virtual module which is expected to be resolved in resolveId function.
    • import str from 'my-unplugin-virtual-id' is in the business code , not my configuration.
  5. tropicalraisel commented on Sep 13, 2021

    @tropicalraisel
    Contributor

    Ok I'm understanding now. The variable naming was a bit confusing. The name of the plugin in that configuration is unplugin-starter, so that's likely your issue. You should be doing this in your Webpack config:

    module.exports = {
      entry: './src/index.js',
      plugins: [
        require('unplugin-starter/webpack').default(),
      ],
    }
  6. Codpoe commented on Sep 13, 2021

    @Codpoe
    Author

    Yeah, this is my current configuration, but the resolveId function doesn't work in webpack project as expected, and it throw an error which indicates the virtual module is not found.

  7. tropicalraisel commented on Sep 13, 2021

    @tropicalraisel
    Contributor

    Try deleting your load section and see if it works then. My suspicion is this error is thrown there and not in resolveId.

  8. Codpoe commented on Sep 18, 2021

    @Codpoe
    Author

    I tried to do what you said, but it still didn’t work

  9. danielroe commented on Oct 21, 2022

    @danielroe
    Member
  10. yunsii commented on Jul 24, 2023

    @yunsii

    Unplugin resolveId with virtual modules seem still not work in webpack on Windows OS.

  11. yunsii commented on Jul 25, 2023

    @yunsii

    Unplugin resolveId with virtual modules seem still not work in webpack on Windows OS.

    It works today, but i don't konw what happens 😦

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions