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

Support conditional exports without export package.json #227

Closed
xiaoxiangmoe opened this issue May 22, 2020 · 0 comments
Closed

Support conditional exports without export package.json #227

xiaoxiangmoe opened this issue May 22, 2020 · 0 comments

Comments

@xiaoxiangmoe
Copy link
Contributor

xiaoxiangmoe commented May 22, 2020

import * as utils from 'utils-ts'
console.log(utils)

it will throw an error:

Error: ENOENT: no such file or directory, stat '/Users/foobar/my-react-project/node_modules/utils-ts/dist/utils-ts.js'

The package.json of utils-ts is:

{
  "name": "utils-ts",
  "version": "0.1.0-alpha.3",
  "types": "./dist/utils-ts.d.ts",
  "main": "./dist/utils-ts.cjs",
  "module": "./dist/utils-ts.mjs",
  "exports": {
    ".": {
      "import": "./dist/utils-ts.mjs",
      "require": "./dist/utils-ts.cjs",
      "default": "./dist/utils-ts.cjs"
    }
  }
}

export const resolveFrom = (root: string, id: string) =>
require.resolve(id, { paths: [root] })

vite/src/node/resolver.ts

Lines 179 to 183 in 4637556

let pkgPath
try {
// see if the id is a valid package name
pkgPath = resolveFrom(root, `${id}/package.json`)
} catch (e) {}

It will throw an Error for library utils-ts:

Uncaught:
Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './package.json' is not defined by "exports" in /Users/foobar/my-react-project/node_modules/utils-ts/package.json

Maybe we could build vite into mjs and use --experimental-import-meta-resolve.

Also, we should support paths rather than root, because ./src has no node_modules dir.

@xiaoxiangmoe xiaoxiangmoe changed the title Support mjs by default Support conditional exports without export package.json May 22, 2020
@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.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant