Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with resolving SASS files when the build files are in a subfolder #18134

Closed
7 tasks done
HolyNoodle opened this issue Sep 18, 2024 · 2 comments
Closed
7 tasks done

Comments

@HolyNoodle
Copy link

HolyNoodle commented Sep 18, 2024

Describe the bug

We are working in a monorepo and trying to migrate our apps to vite.

We have a React component package which is built using roolup. The rollup config builds two entrypoint:

  • index.js (CJS), this also builds the SASS and CSS files (index.scss & index.css at the same place)
  • index.esm.js (ES)

All build files are in a build folder. An abstract of the folder structure:

  • Lib
    • src (source files)
    • build
    • package.json
    • rollup.config.mjs
  • App
    • src
    • index.html
    • package.json
    • vite.config.ts

When trying to build the app we are blocked by this error:
Failed to resolve entry for package "lib". The package may have incorrect main/module/exports specified in its package.json.

In my investigations I found this code block: https://github.com/vitejs/vite/blob/main/packages/vite/src/node/plugins/resolve.ts#L1070-L1075

This code block reset the value of the resolved entrypoint (before the if the value is ./build/index.esm.js). Following up the code, we can see later that this leads the resolver to scan for the presence of index files at the root of the lib and not in the build folder. Which makes the resolve mechanism fail.

I was thinking of opening a PR to change the entry = '' to something like entry = path.dirname(entry). A quick local fix in the built code of vite changing the code to entry = "./build/" fixes the import issue.

WDYT about such a solution?

Reproduction

https://github.com/HolyNoodle/vite-sass-reproduction

Steps to reproduce

  • cd app
  • npm run build

Moving the file ./lib/build/index.scss to ./lib/index.scss will also fix the problem.

System Info

System:
    OS: Linux 6.8 Ubuntu 24.04.1 LTS 24.04.1 LTS (Noble Numbat)
    CPU: (16) x64 12th Gen Intel(R) Core(TM) i7-1260P
    Memory: 4.31 GB / 15.33 GB
    Container: Yes
    Shell: 5.2.21 - /bin/bash
  Binaries:
    Node: 18.19.1 - /usr/bin/node
    npm: 9.2.0 - /usr/bin/npm
  Browsers:
    Chrome: 128.0.6613.119

Used Package Manager

npm

Logs

app@0.0.0 build
vite build

vite v5.4.6 building for production...
transforming (1) index.htmlDeprecation [legacy-js-api]: The legacy JS API is deprecated and will be removed in Dart Sass 2.0.0.
transforming (19) src/App.cssfile:///home/holynoodle/dev/repro_vite/node_modules/vite/dist/node/chunks/dep-DyBnyoVI.js:46574
const err = new Error(
^

Error: Failed to resolve entry for package "lib". The package may have incorrect main/module/exports specified in its package.json.
at packageEntryFailure (file:///home/holynoodle/dev/repro_vite/node_modules/vite/dist/node/chunks/dep-DyBnyoVI.js:46574:15)
at resolvePackageEntry (file:///home/holynoodle/dev/repro_vite/node_modules/vite/dist/node/chunks/dep-DyBnyoVI.js:46571:3)
at tryNodeResolve (file:///home/holynoodle/dev/repro_vite/node_modules/vite/dist/node/chunks/dep-DyBnyoVI.js:46387:16)
at ResolveIdContext.resolveId (file:///home/holynoodle/dev/repro_vite/node_modules/vite/dist/node/chunks/dep-DyBnyoVI.js:46137:19)
at PluginContainer.resolveId (file:///home/holynoodle/dev/repro_vite/node_modules/vite/dist/node/chunks/dep-DyBnyoVI.js:48952:17)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async file:///home/holynoodle/dev/repro_vite/node_modules/vite/dist/node/chunks/dep-DyBnyoVI.js:66208:15
at async internalImporter (file:///home/holynoodle/dev/repro_vite/node_modules/vite/dist/node/chunks/dep-DyBnyoVI.js:37307:22) {
code: 'ERR_RESOLVE_PACKAGE_ENTRY_FAIL'
}

Node.js v18.19.1
npm ERR! Lifecycle script build failed with error:
npm ERR! Error: command failed
npm ERR! in workspace: app@0.0.0
npm ERR! at location: /home/holynoodle/dev/repro_vite/app

Validations

@HolyNoodle
Copy link
Author

HolyNoodle commented Sep 18, 2024

I prepared a PR about this for the discussion to be easier: #18135

@HolyNoodle
Copy link
Author

HolyNoodle commented Sep 18, 2024

For those coming here with the same issue, here how I fixed it (thanks to @hi-ogawa). See the PR for details.

Solution:
Specifying the sass property in the package.json of the lib > exports > "." > sass.
This property needs to be present before the import and require. Putting it after result in the same error as in this issue.

Example:

{
   "main": "build/index",
   "module": "build/index.esm.js",
   "exports": {
      ".": {
         "sass": "./build/index.scss",
         ... Other properties like require or import
      }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant