swc-project / pkgs

node.js packages for SWC
49 stars 15 forks source link

`TypeError: require(...).getOptions is not a function` ==> missing `loader-utils` in peer deps #38

Closed stevebeauge closed 5 months ago

stevebeauge commented 5 months ago

Hello,

When using webpack 4 swc-loader relies on loader-utils to work https://github.com/swc-project/pkgs/blob/f035da2969b45ab9b1810fc2b93be9798ccf8069/packages/swc-loader/src/index.js#L12

However, this package is not listed in the dependencies explicitly. It may work when using npm, because it's a phantom dependency (installed by webpack in the flat node_modules), but when using pnpm, it fails because the dependency is not found with:

TypeError: require(...).getOptions is not a function

I believe you should declare loader-utils as a peer dependency to ensure the package is available.

As a workaround, pnpm allows tweaking the dependencies of dependencies using pnpm.packageExtension field. I was able to get rid of this error using in my package.json file :

{
    "pnpm": {
        "packageExtensions": {
            "swc-loader": {
                "peerDependencies": {
                    "loader-utils": ">=1.2.3"
                }
            }
        }
    }
}
kdy1 commented 5 months ago

It's not a peer dependency for users using webpack 5, so you should install it by yourself.

stevebeauge commented 5 months ago

@kdy1 : installing in my project loader-utils won't work.

The pnpm symlink structure requires to have every dependency known.

If the dependency is optional, you may set peerdependenciesmeta:

{
  "peerDependencies": {
     "loader-utils": ">=1.2.3"
  },
  "peerDependenciesMeta": {
    "loader-utils": {
      "optional": true
    }
  }
}

This way, wp5 users won't be required to install the dependency, but it will be available to wp4 users (at least theorically, I never played with such edge case).

If you need a repro I can take a few minutes to create it.

stevebeauge commented 5 months ago

I'm trying to create a minimal repro but in this repro, I don't get the error.

Let me close the issue while I investigate if there's interference with other tools. I'll reopen the issue only if I get a proper repro.