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

Resolving dependencies without entry point #247

Closed
baransu opened this issue May 24, 2020 · 4 comments
Closed

Resolving dependencies without entry point #247

baransu opened this issue May 24, 2020 · 4 comments

Comments

@baransu
Copy link

baransu commented May 24, 2020

Do NOT ignore this template or your issue will have a very high chance to be closed without comment.

Describe the bug

Currently, when resolving dependencies vite looks in package.json for module or entry, otherwise it fallbacks to index.js:

entryPoint = pkg.module || pkg.main || 'index.js'

This is problematic with BuckleScript compiled modules where ReasonML files are transpiled 1:1 into *.bs.js files without a single "entry" module. Later when using such a package in your application, paths point directly to the module you need without unnecessary reexports.

Right now vite throws an unhandled exception when trying to use such package when index.js is not found:

const content = fs.readFileSync(entryFilePath, 'utf-8')

As I understand the solution would be to exclude those packages in vite.config.js but it can be tedious as none of the BuckleScript packages use entry point convention and there is no way to do it using the BuckleScript compiler (but it's outside of the scope of this issue).

Reproduction

System Info

  • required vite version: 0.16.10
  • required Operating System: macOS 10.15.4
  • required Node version: v10.16.3
  • Optional:
    • npm/yarn version
    • Installed vue version (from yarn.lock or package-lock.json)
    • Installed @vue/compiler-sfc version

Logs (Optional if provided reproduction)

  1. Run vite or vite build with the --debug flag.
  2. Provide the error log here.
vite:config config resolved in 169ms +0ms
vite:resolve (node_module entry) commons -> index.js +0ms
(node:87960) UnhandledPromiseRejectionWarning: Error: ENOENT: no such file or directory, open '/Users/baransu/Documents/monorepo/node_modules/commons/index.js'
    at Object.openSync (fs.js:443:3)
    at Object.readFileSync (fs.js:343:35)
    at deps.filter (/Users/baransu/Documents/monorepo/node_modules/vite/dist/depOptimizer.js:95:44)
    at Array.filter (<anonymous>)
    at Object.optimizeDeps (/Users/baransu/Documents/monorepo/node_modules/vite/dist/depOptimizer.js:68:32)
(node:87960) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:87960) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
@yyx990803
Copy link
Member

How do you use such a package then? Only with deep imports? And what does a typical BuckleScript package look like? Could’ve at least provided an example.

@baransu
Copy link
Author

baransu commented May 24, 2020

Sorry for not providing an example - here is example repo https://github.com/baransu/vite-issue-247. It's a vite reason-react template with my small addition to present my issue.

It uses 2 Reason (BuckleScript) packages:

  • re-classnames - it contains "main" in package.json which points to non existing *.re file. With only this library everything works perfect as it's ignored during dependencies optimization
  • bs-json - this library doesn't provide "main" in package.json which leads to following error:
(node:93520) UnhandledPromiseRejectionWarning: Error: ENOENT: no such file or directory, open '/Users/baransu/Documents/Reason/vite-issue-247/node_modules/@glennsl/bs-json/index.js'
    at Object.openSync (fs.js:443:3)
    at Object.readFileSync (fs.js:343:35)
    at deps.filter (/Users/baransu/Documents/Reason/vite-issue-247/node_modules/vite/dist/depOptimizer.js:95:44)
    at Array.filter (<anonymous>)
    at Object.optimizeDeps (/Users/baransu/Documents/Reason/vite-issue-247/node_modules/vite/dist/depOptimizer.js:68:32)
(node:93520) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:93520) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

BuckleScript uses deep imports to modules you want to use. Here is an example of App.bs.js imports: https://github.com/baransu/vite-issue-247/blob/2922a7a7a98627de087290de253d60fe0d19380a/src/App.bs.js#L3-L6

@yyx990803
Copy link
Member

I added more graceful handling of package with no main field (or non-existent main field) in the commit above, but that won't make your repro run because re-classnames ships code that uses CommonJS syntax.

I think overall Reason/BuckleScript ecosystem has some problematic integration practices with the npm ecosystem - e.g. declaring a main file that does not exist is simply wrong, and shipping non-ESM-compatible dist files is something Vite simply does not intend to support.

@baransu
Copy link
Author

baransu commented May 25, 2020

BuckleScript provides support for ESM output for packages (as well as CommonJS). I’m not sure about single entry point for packages vs deep imports we use atm.

JS output is not intended to be included into version control and compiler during clean compilation generates new JS files thats why we cannot rely on JS entrypoints.

@github-actions github-actions bot locked and limited conversation to collaborators Jul 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants