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

X6并不支持服务端渲染 #4095

Open
kingyue737 opened this issue Dec 13, 2023 · 9 comments · Fixed by #4101
Open

X6并不支持服务端渲染 #4095

kingyue737 opened this issue Dec 13, 2023 · 9 comments · Fixed by #4101

Comments

@kingyue737
Copy link
Contributor

kingyue737 commented Dec 13, 2023

Describe the bug

初次尝试X6,在SSR项目中引入X6,会报错,如:

Element is not defined
document is not defined

目前我发现的在@antv/x6-commonpolyfill.tsprefix.ts中,使用了很多documentElement,这俩只有在browser端才有,服务端Node.js是没有的,所以就出错了。
比较奇怪的是js的polyfill和css的prefix通常应该由下游用户自主决定,比如根据想要兼容的浏览器版本使用webpack/vite来transpile code, 然后用babel来加垫片,postcss来处理css兼容问题。直接在库里处理浏览器兼容问题不是良好的实践,会引入很多不需要代码增加体积。

另外一个比较大的问题是没有有效的ESM入口,也和服务端渲染有关,Node.js不认package.json中的module,必须要用exports表明ESM的入口。参考这两个网站
https://publint.dev/@antv/[email protected]
https://arethetypeswrong.github.io/?p=%40antv%2Fx6%402.16.1
可以看到X6只有CJS入口,没有能被识别的ESM入口。

正确的写法应当如下:

{
  "name":"@antv/x6",
  "exports": {
    ".": {
      "import": "./es/index.mjs",   // IMPORTANT, use ".mjs" instead of ".js"
      "require": "./lib/index.js"
    }
  },
}

或指定type: "module"

{
  "name":"@antv/x6",
  "type":"module",
  "exports": {
    ".": {
      "import": "./es/index.js",
      "require": "./lib/index.cjs"  // IMPORTANT, use ".cjs" instead of ".js"
    }
  },
}

参考:
https://publint.dev/rules#has_module_but_no_exports
https://nuxt.com/docs/guide/concepts/esm#what-are-valid-imports-in-a-nodejs-context

Your Example Website or App

https://stackblitz.com/edit/github-h7fojp

Steps to Reproduce the Bug or Issue

在SSR项目中引入X6

Expected behavior

根据README,X6应该是支持服务端渲染的,不应该有这样的问题。

X6/README.md

Line 32 in f2cae96

- 支持服务端渲染。

Screenshots or Videos

No response

Platform

  • OS: Windows, Linux
  • Browser: Chrome
  • Version: 2.16.1
  • Node: 20.10.0

Additional context

No response

@x6-bot
Copy link
Contributor

x6-bot bot commented Dec 13, 2023

👋 @kingyue737

Thanks for opening your first issue here! If you're reporting a 🐞 bug, please make sure you include steps to reproduce it.
To help make it easier for us to investigate your issue, please follow the contributing guidelines.
We get a lot of issues on this repo, so please be patient and we will get back to you as soon as we can.

@abelce
Copy link

abelce commented Dec 18, 2023

我也遇到这个问题

@America-first-melon
Copy link

image
我在vscode里渲染 webview 也遇到了这个问题。

@yoke233
Copy link

yoke233 commented Jan 22, 2024

怎么close了 nuxt上 还是报错 ESM

@kingyue737
Copy link
Contributor Author

kingyue737 commented Jan 26, 2024

@yoke233 其实不应该close,因为我的PR只是解决了一部分问题。当然现在已经可以在nuxt上用了,但需要transpile这个库:

export default defineNuxtConfig({
  build: {
    transpile: [
      /^@antv/,
      'gl-matrix',
    ],
  }
}

我会再开一个issue追踪剩余的问题。

@NewByVector
Copy link
Contributor

NewByVector commented Jan 26, 2024

我直接 reopen 吧。

@NewByVector NewByVector reopened this Jan 26, 2024
@kingyue737
Copy link
Contributor Author

其他可视化相关开源库:

@guxuerui
Copy link

在Nuxt3中,加上了这段transpile,还是不能用, g6版本已经是5.0.18了

@hellolsk
Copy link

hellolsk commented Nov 2, 2024

image
帮忙看下, next项目引入x6编译不过,如何解决?

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

Successfully merging a pull request may close this issue.

7 participants