-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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 ESM-style "imports"
field aliases in package.json
#7385
Comments
Subpath imports are supported by Webpack and esbuild. I think it should also be supported by Vite. I'm gonna woking on this. |
Has it been resolved? When building a project which contains chalk@5.0.0, it throws an error:
Here's an online example. Visit and run package.json in chalk@5.0.0: {
"name": "chalk",
"version": "5.0.0",
"description": "Terminal string styling done right",
"license": "MIT",
"repository": "chalk/chalk",
"funding": "https://github.com/chalk/chalk?sponsor=1",
"type": "module",
"exports": "./source/index.js",
"imports": {
"#ansi-styles": "./source/vendor/ansi-styles/index.js",
"#supports-color": {
"node": "./source/vendor/supports-color/index.js",
"default": "./source/vendor/supports-color/browser.js"
}
},
"types": "./source/index.d.ts",
"engines": {
"node": "^12.17.0 || ^14.13 || >=16.0.0"
},
"scripts": {
"test": "xo && c8 ava && tsd",
"bench": "matcha benchmark.js"
},
"files": [
"source",
"!source/index.test-d.ts"
],
"keywords": [
"color",
"colour",
"colors",
"terminal",
"console",
"cli",
"string",
"ansi",
"style",
"styles",
"tty",
"formatting",
"rgb",
"256",
"shell",
"xterm",
"log",
"logging",
"command-line",
"text"
],
"devDependencies": {
"@types/node": "^16.11.10",
"ava": "^3.15.0",
"c8": "^7.10.0",
"color-convert": "^2.0.1",
"execa": "^6.0.0",
"log-update": "^5.0.0",
"matcha": "^0.7.0",
"tsd": "^0.19.0",
"xo": "^0.47.0",
"yoctodelay": "^2.0.0"
},
"xo": {
"rules": {
"unicorn/prefer-string-slice": "off"
}
},
"c8": {
"reporter": [
"text",
"lcov"
],
"exclude": [
"source/vendor"
]
}
}
|
Can we get an update on this? I was surprised to find out that vite was the only one of the main bundlers that didn't support this feature when making our reference toolkit. There is a PR that has gone with no reviews for about 6 months now. |
If the error is only from rollup (while building), for now you can use |
At least for building with rollup this seems to be fixed. When running vitest however - which seems to use whatever vite is using during its own runtime vitest-dev/vitest#1365 - it's still throwing an error. |
Clear and concise description of the problem
As of v14, Node.js now supports defining aliases for ESM imports in the
packages.json
through the"imports"
field. Vite supports rollup-style aliases.It would be nice if Vite was capable out of the box of reading the
"imports"
field and translating it correctly to rollup aliases.Suggested solution
As a proof of concept, I have implemented a version that uses rollup's
customResolver
feature and works as long as no conditional imports are used. It turned out to be surprisingly simple:This works for my code-base with the following imports map:
It shouldn't be too hard to expand this to also support conditional imports.
Alternative
No response
Additional context
No response
Validations
The text was updated successfully, but these errors were encountered: