我正在构建一个 CodeMirror-Yjs 编辑器。我按照 y-codemirror.next 中的示例进行操作。但是当我尝试汇总 JS 时,我得到了标题中的错误。
我尝试更改依赖版本,但仍然无法使其工作。请帮忙。
如有任何帮助,我们将不胜感激。
汇总命令:
npm run dist
错误:
[!] RollupError: "default" is not exported by "node_modules/simple-peer/simplepeer.min.js", imported by "node_modules/y-webrtc/src/y-webrtc.js".
https://rollupjs.org/troubleshooting/#error-name-is-not-exported-by-module
node_modules/y-webrtc/src/y-webrtc.js (16:7)
14:
15: import * as Y from 'yjs' // eslint-disable-line
16: import Peer from 'simple-peer/simplepeer.min.js'
^
17:
18: import * as syncProtocol from 'y-protocols/sync'
at error (/var/www/websocket_live/codemirror.next/node_modules/rollup/dist/shared/rollup.js:349:30)
at Module.error (/var/www/websocket_live/codemirror.next/node_modules/rollup/dist/shared/rollup.js:15093:16)
at Module.traceVariable (/var/www/websocket_live/codemirror.next/node_modules/rollup/dist/shared/rollup.js:15518:29)
... ...
我的
package.json
文件:
{
"name": "CodeMirror-Yjs-editor",
"version": "1.0.0",
"description": "",
"main": "codemirror.js",
"scripts": {
"clean": "rm -rf dist",
"dist": "npm run clean && rollup -c --bundleConfigAsCjs && tsc",
"watch": "rollup -wc",
"lint": "standard && tsc",
"start": "concurrently 'http-server -o index.html' 'npm run watch'"
},
"dependencies": {
"@codemirror/lang-javascript": "^6.0.2",
"codemirror": "^6.0.1",
"y-codemirror.next": "^0.3.2",
"y-webrtc": "^10.2.5",
"yjs": "^13.5.41"
},
"devDependencies": {
"@rollup/plugin-babel": "^6.0.3",
"@rollup/plugin-commonjs": "^25.0.3",
"@rollup/plugin-node-resolve": "^15.0.1",
"concurrently": "^5.3.0",
"http-server": "^14.1.0",
"jsdom": "^22.1.0",
"rollup": "^3.27.2",
"standard": "^14.3.4",
"typescript": "^3.9.10"
}
}
更新:
rollup.config.js
import babel from "@rollup/plugin-babel";
import {nodeResolve} from "@rollup/plugin-node-resolve";
export default {
input: "./codemirror.js",
output: {
file: "./cm6.bundle.js",
format: "umd"
},
plugins: [babel(), nodeResolve()]
}
找了很久的解决方案后,我将发布我自己的解决方案。希望这会对某人有所帮助。
短篇故事:更改汇总插件顺序。尝试将它们一一移动,它解决了我的问题。
长篇故事短篇:
自从我发布问题以来,我对项目做了很多更改,并且汇总配置文件看起来有点不同。
我在网上搜索了很长时间的解决方案,发现有人通过移动汇总配置文件中的插件来解决问题,问题就消失了。所以我就尝试了一下。
这是我的汇总配置文件,
import builtins from 'rollup-plugin-node-builtins';
import globals from 'rollup-plugin-node-globals';
import nodeResolve from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'
import json from "@rollup/plugin-json";
import { terser } from 'rollup-plugin-terser'
import 'dotenv/config'
... ...
const external = [
];
export default {
input: "./src/codemirror.js",
output: {
name: "cm6.bundle",
file: "./dist/cm6.bundle.js",
format: "iife",
sourcemap: true,
inlineDynamicImports: true
},
external,
plugins: [
json(),
nodeResolve({
mainFields: ['module', 'browser', 'main']
}),
**commonjs(),
globals(),**
builtins(),
...minificationPlugins
]
}
我所做的是将插件数组中的
commonjs(), globals(),
从顶部移动到底部。当它停在这个位置时它就起作用了。我无法解释为什么,因为我还没有阅读 rollup 及其插件的完整文档(和源代码)。但似乎在处理 JS 源代码时,rollup 是按步骤进行操作的。如果操作顺序正确,那么它就会起作用。我认为多读书肯定会有帮助。我会找时间去做这件事。