【问题标题】:You may need an additional loader to handle the result of these loaders (upgrading Monaco Editor)您可能需要额外的加载器来处理这些加载器的结果(升级 Monaco Editor)
【发布时间】:2021-12-31 02:34:46
【问题描述】:

我想将 Monaco Editor 的版本升级到 v0.31.1。我做了npm install monaco-editor@0.31.1sudo PORT=8000 HTTPS=true ./node_modules/.bin/react-app-rewired start 重新编译给了我以下错误:

Failed to compile.

./node_modules/monaco-editor/esm/vs/language/html/monaco.contribution.js 154:11
Module parse failed: Unexpected token (154:11)
File was processed with these loaders:
 * ./node_modules/babel-loader/lib/index.js
You may need an additional loader to handle the result of these loaders.
|     dispose() {
|       onLanguageListener.dispose();
>       mode?.dispose();
|       mode = void 0;
|     }

有人可以帮忙吗?

编辑 1: package.json升级前:

{
  "name": "frontend",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@fluentui/react": "^8.33.0",
    "@handsontable/react": "^9.0.2",
    "@material-ui/core": "^4.9.8",
    "@microsoft/office-js-helpers": "^1.0.2",
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.3.2",
    "@testing-library/user-event": "^7.1.2",
    "@types/react-stripe-elements": "^6.0.4",
    "@uifabric/react-cards": "^0.109.49",
    "axios": "^0.19.2",
    "color": "^3.1.2",
    "copy-to-clipboard": "^3.3.1",
    "cross-storage": "^1.0.0",
    "dva": "^2.4.1",
    "dva-model-creator": "^0.4.3",
    "formik": "^2.1.4",
    "handsontable": "^9.0.2",
    "highcharts": "^7.0.3",
    "highcharts-react-official": "^3.0.0",
    "lodash": "^4.17.15",
    "moment": "^2.24.0",
    "monaco-editor": "^0.20.0",
    "monaco-languageclient": "^0.13.0",
    "node-sass": "^4.14.1",
    "office-ui-fabric-core": "^11.0.0",
    "office-ui-fabric-react": "^7.105.4",
    "query-string": "^6.11.1",
    "react": "^16.13.1",
    "react-app-polyfill": "^1.0.6",
    "react-bootstrap": "^1.0.1",
    "react-dom": "^16.13.1",
    "react-meta-tags": "^1.0.1",
    "react-monaco-editor": "^0.35.0",
    "react-scripts": "3.4.1",
    "react-stripe-elements": "^6.1.1",
    "redux-devtools-extension": "^2.13.8",
    "styled-components": "^5.0.1",
    "typescript": "^3.8.3",
    "yup": "^0.28.3"
  },
  "scripts": {
    "start": "PORT=3000 react-app-rewired start",
    "start:https": "HTTPS=true  react-app-rewired start",
    "build": "react-scripts --max_old_space_size=8096 build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "@types/color": "^3.0.1",
    "@types/cross-storage": "^0.8.29",
    "@types/jest": "^25.1.4",
    "@types/lodash": "^4.14.149",
    "@types/node": "^13.9.5",
    "@types/office-js": "^1.0.92",
    "@types/query-string": "^6.3.0",
    "@types/react": "^16.9.27",
    "@types/react-dom": "^16.9.5",
    "@types/react-redux": "^7.1.7",
    "@types/styled-components": "^5.0.1",
    "@types/yup": "^0.26.33",
    "css-loader": "^3.5.3",
    "react-app-rewired": "^2.1.8",
    "style-loader": "^1.2.1"
  }
}

"monaco-editor": "^0.20.0"npm list monaco-editor 返回时

$ npm list monaco-editor
npm list monaco-editor
frontend@0.1.0 /frontend
├── UNMET DEPENDENCY monaco-editor@0.20.0 
└─┬ UNMET DEPENDENCY react-monaco-editor@0.35.0
  └── UNMET DEPENDENCY monaco-editor@0.20.0 

npm ERR! missing: monaco-editor@0.20.0, required by frontend@0.1.0
npm ERR! missing: react-monaco-editor@0.35.0, required by frontend@0.1.0
npm ERR! missing: monaco-editor@0.20.0, required by react-monaco-editor@0.35.0

"monaco-editor": "^0.31.1"npm list monaco-editor 返回时

$ npm list monaco-editor
frontend@0.1.0 /frontend
├── UNMET DEPENDENCY monaco-editor@0.31.1 
└─┬ UNMET DEPENDENCY react-monaco-editor@0.35.0
  └── UNMET DEPENDENCY monaco-editor@0.31.1 

npm ERR! missing: monaco-editor@0.31.1, required by frontend@0.1.0
npm ERR! missing: react-monaco-editor@0.35.0, required by frontend@0.1.0
npm ERR! missing: monaco-editor@0.31.1, required by react-monaco-editor@0.35.0

编辑 2:

按照@Bart 的回答,我通过yarn add @babel/preset-env --dev 安装了@babel/preset-env,并使用{"presets": ["@babel/preset-env"]} 创建了babel.config.json,但react-app-rewired start 给出了同样的错误。

然后,我意识到我确实有一个文件config-overrides.js,内容如下:

module.exports = function override (config, env) { 
    // disable chunks so the index.html won't change during development 
    config.optimization.splitChunks = {
        cacheGroups: {
            default: false,
        },
    };
    config.optimization.runtimeChunk = false;
    return config;
}

然后我添加了config.module.rules 的部分,但编译返回了相同的错误。

另外,貌似可以用yarn build构建项目,不会出现这个错误。但是构建项目需要时间。

module.exports = function override (config, env) {
    config.module.rules = 
        [
          {
            test: /\.m?js$/,
            exclude: /(node_modules|bower_components)/,
            use: {
              loader: 'babel-loader',
              options: {
                presets: ['@babel/preset-env']
              }
            }
          }
        ]
    
    // disable chunks so the index.html won't change during development 
    config.optimization.splitChunks = {
        cacheGroups: {
            default: false,
        },
    };
    config.optimization.runtimeChunk = false;
    return config;
}

【问题讨论】:

  • 可能也会更新 Babel。你的版本好像不支持github.com/tc39/proposal-optional-chaining>。
  • 您从哪个版本更新?我很好奇,因为我有类似的问题,并希望得到一个工作版本。
  • @Safiyya 如何在我的 mac OS 中查看 babel 的版本?
  • 你可以使用`npm list monaco-editor`或者`npm list babel`,哪个版本的编辑器适合你?
  • @Safiyya "monaco-editor": "^0.20.0" 为我工作。

标签: webpack babeljs monaco-editor babel-loader react-app-rewired


【解决方案1】:

问题似乎在于可选的链接语法(mode?.dispose(); 中的?.)。这也被列为摩纳哥changelog 中的一项重大更改:

生成的代码现在与 ESBuild 捆绑在一起,并且生成的代码利用了较新的浏览器功能,例如可选链。这些功能应该在所有浏览器中都可用,但它们可能无法被旧的 JS 解析器正确解析,特别是 parcel v1 可能在解析 JS 时出现问题。

correct Babel plugin 可以很好地处理此语法。一般来说,最好使用@babel/preset-env 而不是单独的插件。此预设同时处理多个插件。你有一个可以包含这个预设的 babel 配置文件吗?这可以像安装 @babel/preset-env 作为开发依赖项一样简单,然后在项目根目录中创建一个 babel.config.json 文件,内容如下:

{
  "presets": ["@babel/preset-env"]
}

其次,这可能意味着您的 Babel 配置已经正常工作,但是 Webpack 忽略了 node_modules 中的任何内容(由 Create React App 在后台使用),这通常是为了加快编译时间。然后你必须找到一种方法来否决 Webpack 配置,这样你的 monaco 依赖项就不会被跳过进行编译。

我看到你已经在使用 react-app-rewired,所以更新 Webpack 配置是按照 their documentation 中的说明完成的:

// config-overrides.js
module.exports = function override (config, env) { 
    // disable chunks so the index.html won't change during development 
    config.optimization.splitChunks = {
        cacheGroups: {
            default: false,
        },
    };
    config.optimization.runtimeChunk = false;
    
    config.module.rules = [
      {
        test: /node_modules\/monaco-editor/,
        use: {
          loader: 'babel-loader',
          // if you include your babel config here,
          // you don’t need the `babel.config.json` file
          options: { presets: ['@babel/preset-env'] }
        }
      },
      ...config.module.rules
    ]
    return config;
}

集成 Babel 可以通过 babel-loader 完成,请参阅说明 here

【讨论】:

  • 谢谢...我通过yarn add @babel/preset-env --dev 安装了@babel/preset-env,并使用您的内容创建了babel.config.json,但编译时出现了同样的错误。对于你的第二点,我不知道该怎么办......
  • 为您的帮助添加了更多指针
  • 谢谢。我在 OP 中添加了Edit 2。你能帮忙吗?
  • 谢谢你...但是你能直接在我的config-overrides.js 上工作吗?我对 webpack 和 react-app-rewired 的了解不允许我根据我的代码调整您的答案。
  • 您好,您确实提供了帮助,但我无法使这个答案起作用。目前,我使用了答案中描述的解决方法。我不会选择你的答案,但我会为你提供帮助。
【解决方案2】:

我在 Vue 项目中遇到了和你一样的问题。

也许你可以这样处理:

从GitHub下载monaco-editor的源码

在文件monaco-editor/build/utils.js中修改函数buildESM和函数buildOneAMD

例如:将target 设置为es2015

function buildESM(options) {
    build({
        entryPoints: options.entryPoints,
        bundle: true,
        target: 'es2015',
        format: 'esm',
        define: {
            AMD: 'false'
        },
        banner: {
            js: bundledFileHeader
        },
        external: options.external,
        outbase: `src/${options.base}`,
        outdir: `out/release/${options.base}/esm/`,
        plugins: [
            alias({
                'vscode-nls': path.join(__dirname, 'fillers/vscode-nls.ts')
            })
        ]
    });
}

然后,npm run release 并等待它完成

npm run release

你得到文件夹monaco-editor/release,复制文件夹release的内容并粘贴到你在项目下创建的文件夹modules/monaco-editor中。

在您的项目中,修改package.json,将monaco-editor 设置为file:modules/monaco-editor,以及npm install

{
  "name": "frontend",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "monaco-editor": "file:modules/monaco-editor",
  }
}
npm install

上面是我解决这个问题的方法,如果有人找到更好的解决方案,请在这里回复我。

对不起,我的英语不好。

【讨论】:

  • 谢谢。我了解到您自己重新编译了 Monaco Editor。
【解决方案3】:

我们找到了两个临时解决方案:

第一个解决方案是,通过关注this thread,在package.json中使用关注:

"browserslist":[
    ">0.2%",
    "not dead",
    "not op_mini all"
]

第二种解决方案是,使用import * as monaco from 'monaco-editor/esm/vs/editor/editor.api' 而不是import * as monaco from 'monaco-editor'

这两种解决方案绕过了可选链接语法的错误。我们还没有找到直接处理 babel、babel-loder、react-app-rewired 等的方法。

【讨论】:

  • 谢谢!第二个解决方案(显式导入)有效。
【解决方案4】:

我的项目遇到了同样的问题。有一个处理这些加载程序问题的 monaco-editor-webpack-plugin。但是最新版本存在一些问题。 所以我安装了monaco-editor-webpack-plugin@6.0.0monaco-editor@0.30.1。然后你只需要将插件添加到 webpack。

const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin');

module.exports = {
  // ...
  plugins: [
    new MonacoWebpackPlugin(),
  ],
};

【讨论】:

    猜你喜欢
    • 2020-04-05
    • 2020-12-04
    • 1970-01-01
    • 2020-09-12
    • 1970-01-01
    • 2022-11-06
    • 2021-08-05
    • 1970-01-01
    • 2022-11-10
    相关资源
    最近更新 更多