vite icon indicating copy to clipboard operation
vite copied to clipboard

Vite + Linaria + `exports` field in `package.json` = crash

Open nstepien opened this issue 2 years ago • 10 comments

Describe the bug

I tried setting up Playwright for component testing, which uses vite, in our existing components library repo, but I ended up hitting a roadblock with Vite using the exports field for linaria's local/relative imports.

Vite should ignore the exports field in the local package.json file.

What is actually happening is this error:

vite v3.2.4 building for production...
✓ 3 modules transformed.
[commonjs--resolver] Missing "./main_lquf2n.css" export in "undefined" package
error during build:
Error: Missing "./main_lquf2n.css" export in "undefined" package
    at bail (file:///D:/repos/vite-bug/node_modules/vite/dist/node/chunks/dep-67e7f8ab.js:32675:8)
    at resolve (file:///D:/repos/vite-bug/node_modules/vite/dist/node/chunks/dep-67e7f8ab.js:32752:10)
    at resolveExports (file:///D:/repos/vite-bug/node_modules/vite/dist/node/chunks/dep-67e7f8ab.js:34128:12)
    at resolveDeepImport (file:///D:/repos/vite-bug/node_modules/vite/dist/node/chunks/dep-67e7f8ab.js:34146:31)       
    at tryNodeResolve (file:///D:/repos/vite-bug/node_modules/vite/dist/node/chunks/dep-67e7f8ab.js:33838:20)
    at Object.resolveId (file:///D:/repos/vite-bug/node_modules/vite/dist/node/chunks/dep-67e7f8ab.js:33598:28)        
    at file:///D:/repos/vite-bug/node_modules/rollup/dist/es/shared/rollup.js:22748:40

If I remove the exports field then the error goes away, but I cannot remove it.

Reproduction

https://github.com/nstepien/vite-bug

Steps to reproduce

  1. Clone repo above
  2. npm ci
  3. npm run build

System Info

System:
    OS: Windows 10 10.0.22621
    CPU: (64) x64 AMD Ryzen Threadripper 3970X 32-Core Processor
    Memory: 16.33 GB / 31.86 GB
  Binaries:
    Node: 19.1.0 - C:\Program Files\nodejs\node.EXE
    npm: 9.1.1 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Spartan (44.22621.819.0), Chromium (107.0.1418.52)
    Internet Explorer: 11.0.22621.1
  npmPackages:
    vite: ^3.2.3 => 3.2.4

Used Package Manager

npm

Logs

No response

Validations

nstepien avatar Nov 22 '22 15:11 nstepien

Looks similar to https://github.com/vitejs/vite/issues/11020#issuecomment-1323531406 These things in the local package.json should be ignored by Rollup build.

haoqunjiang avatar Nov 23 '22 07:11 haoqunjiang

@sodatea if I use Rollup with a similar set up I do not get any errors. So I'm thinking it's an issue with Vite rather than Rollup.

nstepien avatar Nov 23 '22 11:11 nstepien

I should add that Vite crashes when running either vite or vite build.

nstepien avatar Nov 23 '22 19:11 nstepien

Same bug with [email protected]

nstepien avatar Dec 09 '22 23:12 nstepien

@Anber would you know anything about this bug with Vite+linaria?

nstepien avatar Jan 23 '23 18:01 nstepien

@nstepien is it a Windows-specific bug? I tried https://github.com/nstepien/vite-bug, and it works well.

Anber avatar Jan 24 '23 14:01 Anber

@Anber Mmh, I don't have another machine to confirm whether this is Windows-only 🤔 Thanks for checking though 🙏

nstepien avatar Jan 24 '23 14:01 nstepien

@nstepien @Anber it works well on MacOS

godon019 avatar Jan 24 '23 17:01 godon019

Same issue with 4.1.0-beta.1

amanmahajan7 avatar Jan 27 '23 02:01 amanmahajan7

I've looked into it more deeply,

Linaria outputs code like this:

import "D:/repos/vite-bug/main_lquf2n.css";

The path uses / instead of \, which Vite doesn't like.

If I patch Vite's resolvePlugin with the following:

const prefix1 = 'D:/repos/vite-bug/';
const prefix2 = 'D:\\repos\\vite-bug\\';
id = id.replace(prefix1, prefix2);

or

const prefix1 = 'D:/repos/vite-bug/';
const prefix2 = './';
id = id.replace(prefix1, prefix2);

then Vite + Linaria works as it should.

@Anber ~Is it an easy fix in Linaria to output windows absolute paths with \\?~ I've opened a PR https://github.com/callstack/linaria/pull/1201

nstepien avatar Feb 03 '23 15:02 nstepien

Fixed in Linaria.

nstepien avatar Feb 06 '23 16:02 nstepien