rennzhang / codemirror-editor-vue3

CodeMirror component for Vue3
https://rennzhang.github.io/codemirror-editor-vue3
MIT License
195 stars 39 forks source link

Does codemirrorr-editor-vue3 support SSR? #9

Closed Xing-He closed 2 years ago

Xing-He commented 2 years ago

Hi there, I just got an problem with SSR

node_modules/codemirror/lib/codemirror.css:3
.CodeMirror {
^

SyntaxError: Unexpected token '.'
    at Object.compileFunction (node:vm:355:18)
    at wrapSafe (node:internal/modules/cjs/loader:1022:15)
    at Module._compile (node:internal/modules/cjs/loader:1056:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1121:10)
    at Module.load (node:internal/modules/cjs/loader:972:32)
    at Function.Module._load (node:internal/modules/cjs/loader:813:14)
    at Module.require (node:internal/modules/cjs/loader:996:19)
    at require (node:internal/modules/cjs/helpers:92:18)
    at ~/node_modules/codemirror-editor-vue3/dist/codemirror-editor-vue3.umd.js:19:9
    at Object.<anonymous> (~/node_modules/codemirror-editor-vue3/dist/codemirror-editor-vue3.umd.js:44:3)
"exports": {
    ".": {
      "import": "./dist/codemirror-editor-vue3.es.js",
      "require": "./dist/codemirror-editor-vue3.umd.js"
    },
    "./dist/style.css": {
      "import": "./dist/style.css",
      "require": "./dist/style.css"
    }
  }

Maybe it's an error of "package conditional exports" because the module invoked "*.umd.js" on the node environment and would not compile "CSS" files

After changing the source file " package.json" exports field, Anther error occurred as below,

ReferenceError: navigator is not defined
    at xxx/entry-server.js:1:141657
    at Module.<anonymous> (xxx/entry-server.js:1:314555)
    at Module._compile (node:internal/modules/cjs/loader:1092:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1121:10)
    at Module.load (node:internal/modules/cjs/loader:972:32)
    at Function.Module._load (node:internal/modules/cjs/loader:813:14)
    at Module.require (node:internal/modules/cjs/loader:996:19)
    at require (node:internal/modules/cjs/helpers:92:18)
    at xxx/ssr-server.js:75:18
    at Layer.handle [as handle_request] (~/node_modules/express/lib/router/layer.js:95:5)

And this navigator error must be accessed for the 'navigator' object on Node.js

BTW, I use the 'vite' for compilation

Xing-He commented 2 years ago

I have dug into the "codemirror", and found that it doesn't support SSR. However, I am still confused with the css compile issue.

Xing-He commented 2 years ago

I am now trying to use the dynamic import import('codemirror-editor-vue3') to solve this problem. but how can i define CodeMirror's language? as i can't get the CodeMirror object !

rennzhang commented 2 years ago

As you said, Codemirror itself does not support SSR, and I have not intended to do this.

As for the dynamic introduction you said, I don't understand it, it is best to provide Demo.

Xing-He commented 2 years ago

As we can see, I couldn't use CodeMirror on SSR. and couldn't import the codemirror with static import as below:

import CodeMirror from 'codemirror';

Instead, dynamic import

import('codemirror').then((CodeMirror)=>{ CodeMirror.doSomething() })

Hope I made it clear.

rennzhang commented 2 years ago

The parent component:

<template>
  <button @click="visible = !visible">show modal</button>
  <codemirrorDemo v-if="visible" />
</template>

<script lang="ts" setup>
import codemirrorDemo from "./codemirrorDemo.vue";
const visible = ref(false);
</script>

Child components(codemirrorDemo.vue):

<template>
  <Codemirror v-model:value="code" :options="cmOptions" border :height="200" />
</template>

<script lang="ts">
import { ref } from "vue";
import "codemirror/mode/javascript/javascript.js";

const res = await import("codemirror-editor-vue3");

export default defineComponent({
  components: {
    Codemirror: res.default,
  },
  setup() {
    const code = ref(`const obj = {
  name: "peter", 
  age: 18
};
`);

    const cmOptions = {
      mode: "javascript", // 语言模式
      lineNumbers: true, // 显示行号
      smartIndent: true, // 智能缩进
      indentUnit: 2, // 智能缩进单位为4个空格长度
      foldGutter: true, // 启用行槽中的代码折叠
      styleActiveLine: true, // 显示选中行的样式
    };

    return {
      code,
      cmOptions,
    };
  },
});
</script>