3

我尝试将monaco 代码编辑器集成到我的 ember octane 应用程序中。目前我正在使用 ESM 导入样式并确认手册,我安装了 webpack 加载器插件并将其集成到我的 ember-cli-build.js

const EmberApp = require('ember-cli/lib/broccoli/ember-app');
const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin');

module.exports = function(defaults) {
  let app = new EmberApp(defaults, {
    autoImport: {
      webpack: {
        plugins: [
          new MonacoWebpackPlugin()
        ]
      }
    }
  });

  // Use `app.import` to add additional libraries to the generated
  // output files.
  //
  // If you need to use different assets in different
  // environments, specify an object as the first parameter. That
  // object's keys should be the environment name and the values
  // should be the asset to use in that environment.
  //
  // If the library that you are including contains AMD or ES6
  // modules that you would like to import into your application
  // please specify an object with the list of modules as keys
  // along with the exports of each module as its value.

  return app.toTree();
};

但是在构建我的应用程序时,我总是收到错误消息:

模块解析失败:意外令牌 (8:0) 您可能需要适当的加载程序来处理此文件类型。

(节点:7993)UnhandledPromiseRejectionWarning:错误:webpack 将错误返回到 ember-auto-import

谁能帮助我并告诉我如何将 monaco 正确集成到我的 ember 应用程序中?非常感谢你!

4

1 回答 1

4

我强烈建议使用ember-monaco而不是 monaco-editor,除非以下所有情况都是正确的:您已经成功使用 Embroider,ember-monaco 缺少一个无法添加到该软件包的关键功能,您可以投入大量精力在 Ember 应用程序中手动设置(数周)。

为了在 Ember 应用程序中使用 Webpack 插件,您还需要安装和使用Embroider。常规的 ember-cli 构建根本不使用 Webpack,因此 Webpack 插件将不起作用。

如果您致力于直接使用 monaco-editor,您必须:

  • 使用绣花机
  • 安装了 monaco-editor
  • 安装了 Webpack 插件monaco-editor-webpack-plugin
  • 安装一个 polyfill ( @cardstack/requirejs-monaco-ember-polyfill),并按照自述文件进行注册
  • 注册 webpack 插件并导入 css 文件

这是一个示例 ember-cli-build.js 文件:

'use strict';

process.env.BROCCOLI_ENABLED_MEMOIZE = 'true';

const EmberApp = require('ember-cli/lib/broccoli/ember-app');
const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin');

module.exports = function(defaults) {
  let app = new EmberApp(defaults, {
    prember: {
      // we're not pre-rendering any URLs yet, but we still need prember because
      // our deployment infrastructure already expects `_empty.html` to exist
      // for handling unknown URLs.
      urls: [],
    },
  });

  app.import('node_modules/monaco-editor/dev/vs/editor/editor.main.css');

  return (function() {
    const Webpack = require('@embroider/webpack').Webpack;
    const { join } = require('path');
    const { writeFileSync } = require('fs');

    return require('@embroider/compat').compatBuild(app, Webpack, {
      staticAddonTestSupportTrees: true,
      staticAddonTrees: true,
      staticHelpers: true,
      staticComponents: true,
      onOutputPath(outputPath) {
        writeFileSync(join(__dirname, '.embroider-app-path'), outputPath, 'utf8');
      },
      packagerOptions: {
        webpackConfig: {
          plugins: [new MonacoWebpackPlugin()],
        },
      },
// ...
于 2020-02-11T21:54:56.223 回答