【问题标题】:Integrate monaco editor into ember octane将 monaco 编辑器集成到 ember octane
【发布时间】:2020-05-23 12:13:15
【问题描述】:

我尝试将 monaco code 编辑器集成到我的 ember octane 应用程序中。目前我正在使用 ESM 导入样式并确认手册,我安装了 webpack loader 插件并将其集成到我的 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) 您可能需要适当的加载程序来处理此文件类型。

还有

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

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

【问题讨论】:

  • 我不确定这是否适用于当前的 ember 构建管道。你试过了吗,ember-monaco?

标签: webpack ember.js monaco-editor ember-octane


【解决方案1】:

我强烈建议使用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()],
        },
      },
// ...

【讨论】:

  • 非常感谢,这真是太棒了!我自己在哪里可以找到这么好的信息??
  • 不客气@andreas.teich!这不是你可以通过谷歌搜索自己找到的东西——你很幸运我以前做过。 Monaco 是目前最复杂的前端库之一,而这种信息只能从做过它的其他人那里或通过自己进行几周的调试才能获得。好消息是,您在 Ember 或任何其他框架中使用的几乎所有其他 npm 包都比这容易得多。
猜你喜欢
  • 2019-01-03
  • 1970-01-01
  • 2020-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-12
  • 2019-05-14
  • 2021-12-15
相关资源
最近更新 更多