core: SSR not working with ESM modules in vite
Bug report
After updating deps of btp.udina.de live with 2.0.0.beta27 i run into some issues.
Description
Fomerly, i did no need ClientOnly tags around my custom components loaded by plugin register-components, but i was able to fix this using
<ClientOnly>MyCmpCMP</ClientOnly>
Somehow, using vuepress dev docs the site is not working in browser.
If i open chrome dev tools, i see errors, but on reload, preview is working?!?
Some of the used Components (referencing UI5 Web Component) initially throw an error
[vite] Something unexpected happened while optimizing "/service/udina/pricing.html"
The current page should have reloaded by now
Then i manually have to reload the page 1/2 times, to get the component working in preview?
After serving the site multiple times, i am able to preview the whole site and i wantz to compile the result. Problem: Event not used artefacts are throwing errors (wrong images, etc.) but they are not linked/used in config. Anyhow, this worked with older version.
Now trying to build the docs using vuepress build docs --clean-cache
✖ Rendering pages - failed
Error: Cannot find module 'C:\VSC\@udina\udina.docng\node_modules\@ui5\webcomponents\dist\Label'
at createEsmNotFoundErr (internal/modules/cjs/loader.js:916:15)
at finalizeEsmResolution (internal/modules/cjs/loader.js:909:15)
at resolveExports (internal/modules/cjs/loader.js:449:14)
at Function.Module._findPath (internal/modules/cjs/loader.js:489:31)
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:875:27)
at Function.Module._load (internal/modules/cjs/loader.js:745:27)
at Module.require (internal/modules/cjs/loader.js:961:19)
at require (internal/modules/cjs/helpers.js:92:18)
at Module.<anonymous> (C:\VSC\@udina\udina.docng\site\.server\app.js:30:1)
at Module._compile (internal/modules/cjs/loader.js:1072:14)
There seems to be some issues with vite and custom components.
Maybe this is a problem with plugin-register-components. All Component are inside -> /docs/.vuepress/components/…
The import import “@ui5/webcomponents/dist/Label”; is referencing the devDependencies
"devDependencies": {
"@ui5/webcomponents": "^1.0.2"
}
and is also available.
Additionally using config option
bundlerConfig: {
vue: {
compilerOptions: {
isCustomElement: (tag) => /^ui5-/.test(tag)
}
}
}
Environment info
- Browser: latest chrome on win11
Just figured out, that i can fallback to webpack and everthing is still working as expected. But anyhow, since vite is the new default, maybe this issues are solveable also on vite.
Best Regards Holger
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 20 (13 by maintainers)
Commits related to this issue
- build: 解决mermaid打包问题 相关资料: https://github.com/vuepress/vuepress-next/issues/617 https://github.com/vuepress/vuepress-next/issues/585 — committed to SE2022-Qiaqia/docs by SalHe 2 years ago
- build: 解决mermaid打包问题 相关资料: https://github.com/vuepress/vuepress-next/issues/585 — committed to SE2022-Qiaqia/docs by SalHe 2 years ago
- feat(bundler-vite): dynamically handle plugins and themes for vite (close #585) — committed to vuepress/core by Mister-Hope 2 years ago
Any plan to fix it? @meteorlxy
I prefer to regard it as a critical bug. Since vite is the default bundler, as long as there are any pure esm packages imported on the client side, vite will fail on built.
@Mister-Hope Great find!
I gave it a look with my project, and found that adding the offending packages to
ssr.noExternalin the project’s ownvite.config.tsalso fixed the issue.So it seems no changes to Vuepress or the file mentioned above are required.
The only issue I’m facing with this solution, is that the types are not correct as the
ssrproperty is supposedly missing inUserConfigExport.@meteorlxy I finally figure out the problem location.
A list of package is hard code here.
https://github.com/vuepress/vuepress-next/blob/73d297f321750de098c22c8c774dbe934475ddcb/packages/%40vuepress/bundler-vite/src/plugins/createMainPlugin.ts#L9-L33
But actually packages having client esm and node cjs should also be posted above so that they can be resolved here.
https://github.com/vuepress/vuepress-next/blob/73d297f321750de098c22c8c774dbe934475ddcb/packages/%40vuepress/bundler-vite/src/plugins/createMainPlugin.ts#L107-L109
When I add my package here in the list, I sucessfully built with vite, but without my package in the list, I am getting:
And if my package contain
"type": "module", it will beI’m having the exact same issue with d3 (and its packages like d3-selection). Preview works fine, but the build fails with
ERR_REQUIRE_ESM.Logs of the issue are available at https://github.com/DerYeger/d3-graph-controller/runs/4744365874?check_suite_focus=true.
While Webpack does work, it has other issues like broken CSS. I’d much prefer to stay with Vite.