reactjs 摩纳哥编辑与nextjs

h79rfbju  于 2023-08-04  发布在  React
关注(0)|答案(3)|浏览(135)

使用monaco editor with next js时出现此错误。有人解决这个问题了吗?

Failed to compile
./node_modules/monaco-editor/esm/vs/base/browser/ui/actionbar/actionbar.css
Global CSS cannot be imported from within node_modules.
Read more: https://err.sh/next.js/css-npm
Location: node_modules/monaco-editor/esm/vs/base/browser/ui/actionbar/actionbar.js```

字符串

rhfm7lfc

rhfm7lfc1#

添加此webpack配置为我修复了它

const withCSS = require('@zeit/next-css');
const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin');

module.exports = withCSS({
  webpack(config, options) {
    config.plugins.push(new MonacoWebpackPlugin());
    return config;
  },
  cssLoaderOptions: { url: false }
})

字符串

brccelvz

brccelvz2#

@monaco-editor/react第三方助手

我不知道为什么它工作,我宁愿避免第三方的东西,但这是他的第一件事,我得到了工作,他们只是 Package 得很好,所以它是:

你可以去掉MonacoWebpackPlugin
我测试的关键依赖项:

"dependencies": {
    "@monaco-editor/react": "4.2.1",
    "next": "11.0.1",
    "monaco-editor": "0.26.1",

字符串
然后使用方法如README中所述。它们公开了一个Editor组件帮助器,但如果需要,您也可以获得一个monaco示例。
相关内容:

mgdq6dx1

mgdq6dx13#

以下是我在2023年使用Next.js 13.4.11和monaco编辑器0.40.0的工作。

安装

npm install monaco-editor-webpack-plugin
npm install style-loader
npm install css-loader

字符串

next.config.js

/** @type {import('next').NextConfig} */

const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin');
const monacoRules = [
  {
    test: /\.ttf$/,
    type: 'asset/resource'
  }
]

module.exports = {
  webpack: (
    config,
    { isServer }
  ) => {

    // Modify config and then return it
    if(!isServer) {
      console.log(config)
      config.plugins.push(new MonacoWebpackPlugin({
        languages: ["javascript", "markdown", "yaml"],
        filename: "static/[name].worker.js",
      }))
      config.module.rules.push(...monacoRules)
    }

    return config
  },
}

MyClientComponent.js

"use client"

import { useEffect, useRef, useState } from "react"
import * as monaco from 'monaco-editor/esm/vs/editor/editor.api';

export default function MyEditor() {
  const [editor, setEditor] = useState(null)
  const editorRef = useRef(null)

  // Instantiate Monaco Editor
  useEffect(() => {
    const options = {
      value: "# Hello World",
      language: 'markdown'
    }
    const myEditor = monaco.editor.create(editorRef.current, options);
    setEditor(myEditor)
    return () => { editor.dispose() }
  }, [editorRef])

  return (
    <div ref={ editorRef }></div>
  )
}

注意import语句import * as monaco from 'monaco-editor/esm/vs/editor/editor.api'

import * as monaco from 'monaco-editor'给了我错误
错误:EditorSimpleWorker.loadForeignModule(webpack-internal:/(app-pages-browser)/./node_modules/monaco-editor/esm/vs/editor/common/services/editorSimpleWorker.js:523:31)at eval(webpack-internal:/(app-pages-browser)/./node_modules/monaco-editor/esm/vs/editor/editor/editor/browser/services/webWorker.js:44:30)at async eval(webpack-internal:/./node_modules/monaco-editor/esm/vs/language/typescript/tsMode.js:116:16)意外使用
当使用language: 'javascript'时。

相关问题