unjs / unbuild

📦 A unified JavaScript build system
MIT License
2.31k stars 90 forks source link

Failed to resolve import #248

Open gaetansenn opened 1 year ago

gaetansenn commented 1 year ago

Environment

node: v16.19.0

Reproduction

git clone https://github.com/gaetansenn/vunix git checkout bugs/stub-symbol yarn dev:nuxt-example

Describe the bug

I'm still facing a few problems with the build of my mono-repo project. The first problem is with the dev build when using the --stub option of the https://github.com/unjs/unbuild Here is the error when using the command: yarn dev:nuxt-example The reproduction can be found on the branch: bugs/stub-symbol with yarn dev:nuxt-example command.

nuxt-example:dev:  ERROR  Failed to resolve import "file:///Users/gaetansenn/Development/dewib/librairies/vunix/node_modules/jiti/lib/index.js" from "../../packages/default-preset-tailwind/dist/index.mjs". Does the file exist?
nuxt-example:dev: 
nuxt-example:dev: ✔ Nitro built in 465 ms
nuxt-example:dev: ℹ Vite client warmed up in 2202ms

Additional context

My second problem is that, after updating the code on the branch bugs/stub-symbol, I encountered a problem with the Symbol() export used inside the @vunix/core package. It seems that the Symbol called from https://github.com/gaetansenn/vunix/blob/bugs/stub-symbol/packages/core/src/runtime/utils/config.ts#L175 is not returning the same symbol as the one used in https://github.com/gaetansenn/vunix/blob/bugs/stub-symbol/packages/core/src/runtime/components/elements/button/Button.vue#L66 I have added a comment to the latter version using a fixed string for the provide/inject.

Logs

No response

johannschopplich commented 1 year ago

I'm having the same error in my unlazy pnpm monorepo on macOS.

This is my current solution:

"scripts": {
  "jiti-fix": "find ./dist -type f -exec sed -i '' 's|file://||g' {} \\;",
  "dev": "unbuild --stub && npm run jiti-fix"
},

I can't pin down the issue why the file:/// prefix is added in the Jiti dev build.

sadeghbarati commented 1 year ago

https://github.com/unjs/unbuild/issues/438

I tried your solution on macOS too, the error gone from console but in localhost still got the same error

nekomeowww commented 6 months ago

Weird. I tried "jiti-fix": "find ./dist -type f -exec sed -i '' 's|file://||g' {} \\;",, luckily, the error was resolved for vite, but the error remains in console of localhost:5173 just like what https://github.com/unjs/unbuild/issues/248#issuecomment-1517511553 have mentioned:

Uncaught SyntaxError: The requested module '/@fs/Users/neko/project/node_modules/.pnpm/jiti@1.21.0/node_modules/jiti/lib/index.js?v=d951be74' does not provide an export named 'default'
justin-schroeder commented 6 months ago

I had this issue too and wrote a little unplugin yesterday to fix it in our app by transforming the file:// url to the relative path between the two modules:

https://github.com/justin-schroeder/unplugin-file-url

czhlin commented 3 weeks ago
 hooks: {
    'build:done': async ctx => {
      // https://github.com/unjs/unbuild/issues/248
      if (ctx.options.stub) {
        fglob.sync('./dist/**/*.*js', { onlyFiles: true }).forEach(item => {
          const filePath = path.resolve(__dirname, item);
          fs.readFile(filePath, (err, data) => {
            if (err) {
              return err;
            }
            let str = data.toString();
            str = str.replace(/file:\/\/\/(.+)/g, subStr =>
              path.relative(path.resolve(filePath, '..'), url.fileURLToPath(subStr)).replace(/\\/g, '/')
            );
            fs.writeFile(filePath, str, err => {
              if (err) return err;
            });
          });
        });
      }
    }
  },