swc-project / swc-loader

Moved to https://github.com/swc-project/pkgs
MIT License
394 stars 29 forks source link

Is `npm link` / `yarn link` supported? #65

Closed catamphetamine closed 2 years ago

catamphetamine commented 2 years ago

I'm looking into moving a hobby pet project from Babel to SWC.

My configuration is:

In order to access the utilities repo code as if it was a node_module, I did:

cd c:\dev
cd util
npm link
cd ../main-app
npm link util

It has created a "symlink" c:/dev/main-app/node_modules/utilc:/dev/util.

Next, I run the app and it says something like:

ERROR in ../util/aaa/bbb.js
Module not found: Error: Can't resolve 'regenerator-runtime' in 'c:\dev\util\aaa'
resolve 'regenerator-runtime' in 'c:\dev\util\aaa'
  Parsed request is a module
  using description file: `c:\dev\util\package.json` (relative path: `./aaa`)
    Field 'browser' doesn't contain a valid alias configuration
    resolve as module
      `c:\dev\util\aaa\node_modules` doesn't exist or is not a directory
      looking for modules in `c:\dev\util\node_modules`
        single file module
          using description file: `c:\dev\util\package.json` (relative path: `./node_modules/regenerator-runtime`)
            no extension
              Field 'browser' doesn't contain a valid alias configuration
              `c:\dev\util\node_modules\regenerator-runtime` doesn't exist
            .js
              Field 'browser' doesn't contain a valid alias configuration
              `c:\dev\util\node_modules\regenerator-runtime.js` doesn't exist
            .json
              Field 'browser' doesn't contain a valid alias configuration
              `c:\dev\util\node_modules\regenerator-runtime.json` doesn't exist
            .wasm
              Field 'browser' doesn't contain a valid alias configuration
              `c:\dev\util\node_modules\regenerator-runtime.wasm` doesn't exist
        `c:\dev\util\node_modules\regenerator-runtime` doesn't exist
      `c:\dev\node_modules` doesn't exist or is not a directory
      `c:\node_modules` doesn't exist or is not a directory

At the same time: c:\dev\main-app\node_modules\regenerator-runtime package is installed and exists.

Looks as if SWC ignored the original (main-app) folder when resolving node_modules in a symlinked sibling repo folder.

The question is:

catamphetamine commented 2 years ago

Although having switched from swc-loader to babel-loader, I can see the same error in the console. So maybe the reason is Webpack itself.

catamphetamine commented 2 years ago

Ok, it was a Webpack issue. A fix is adding resolve.symlinks = false flag in Webpack config.

resolve: {
  // Fix Webpack when using symlinked packages with `npm link`/`yarn link`.
  // Prevents Webpack from "expanding" symlinked paths inside `node_modules`
  // to actual filesystem paths, so that `node_modules` from the main application
  // directory are used instead of searching `node_modules` in the symlinked folders.
  symlinks: false
}