无法加载通过 ember-auto-import 导入 css 的模块

如何解决无法加载通过 ember-auto-import 导入 css 的模块

我正在尝试构建一个 ember 3.25 应用程序,该应用程序通过 CkEditor

导入 ember-auto-import

通过将以下内容添加到我的 package.json 中,我能够使编辑器正常工作:

"@ckeditor/ckeditor5-build-classic": "^27.0.0",

并将以下内容添加到我的 ember 组件中:

import ClassicEditor from '@ckeditor/ckeditor5-build-classic';

...

didInsertElement() {
  var editor = ClassicEditor
    .create( document.querySelector( '#editor' ),{
      ...
    });
}

但是当我尝试通过以下方式添加 ImageResize 模块时:

"@ckeditor/ckeditor5-image": "^27.0.0",

在我的组件 (as instructed here) 中:

import Image from '@ckeditor/ckeditor5-image/src/image';
import ImageResize from '@ckeditor/ckeditor5-image/src/image-resize';

我最初看到的错误是:'UnhandledPromiseRejectionWarning: Error: webpack returned errors to ember-auto-import 所以我运行了 AUTO_IMPORT_VERBOSE=true ember serve

我现在看到 ckeditor 不能 @import 嵌套 .css 的错误:

ERROR in ./node_modules/@ckeditor/ckeditor5-ui/theme/components/toolbar/toolbar.css 6:0
Module parse failed: Unexpected character '@' (6:0)
You may need an appropriate loader to handle this file type,currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
|  */
|
> @import "../../mixins/_unselectable.css";

或者使用 ES6 语法:

ERROR in ./node_modules/@ckeditor/ckeditor5-ui/theme/components/dropdown/toolbardropdown.css 6:0
Module parse failed: Unexpected token (6:0)
You may need an appropriate loader to handle this file type,currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
|  */
|
> :root {

以及尝试包含 svg 文件的错误:

ERROR in ./node_modules/@ckeditor/ckeditor5-widget/theme/icons/drag-handle.svg 1:0
Module parse failed: Unexpected token (1:0)
You may need an appropriate loader to handle this file type,currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
> <svg viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg"><path d="M4 0v1H1v3H0V.5A.5.5 0 0 1 .5 0H4zm8 0h3.5a.5.5 0 0 1 .5.5V4h-1V1h-3V0zM4 16H.5a.5.5 0 0 1-.5-.5V12h1v3h3v1zm8 0v-1h3v-3h1v3.5a.5.5 0
 0 1-.5.5H12z"/><path fill-opacity=".256" d="M1 1h14v14H1z"/><g class="ck-icon__selected-indicator"><path d="M7 0h2v1H7V0zM0 7h1v2H0V7zm15 0h1v2h-1V7zm-8 8h2v1H7v-1z"/><path fill-opacity=".254" d="M1 1h14
v14H1z"/></g></svg>

基于此消息:

You may need an appropriate loader to handle this file type,currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders

似乎我需要向 ember 添加一些 webpack 加载器,但我真的不知道该怎么做。有人可以帮忙吗?

解决方法

@ckeditor/ckeditor5-image/src/image imports a CSS file。由于 CKEditor 使用 CSS-in-JS 和其他不属于 ECMAScript 规范的功能,您需要配置 webpack 以支持它。 CKEditor 的文档包括 minimal configuration example:

const CKEditorWebpackPlugin = require("@ckeditor/ckeditor5-dev-webpack-plugin");
const { styles } = require("@ckeditor/ckeditor5-dev-utils");

module.exports = {
  plugins: [
    // ...

    new CKEditorWebpackPlugin({
      // See https://ckeditor.com/docs/ckeditor5/latest/features/ui-language.html
      language: "pl",}),],module: {
    rules: [
      {
        test: /ckeditor5-[^/\\]+[/\\]theme[/\\]icons[/\\][^/\\]+\.svg$/,use: ["raw-loader"],},{
        test: /ckeditor5-[^/\\]+[/\\]theme[/\\].+\.css$/,use: [
          {
            loader: "style-loader",options: {
              injectType: "singletonStyleTag",attributes: {
                "data-cke": true,{
            loader: "postcss-loader",options: styles.getPostCssConfig({
              themeImporter: {
                themePath: require.resolve("@ckeditor/ckeditor5-theme-lark"),minify: true,};

Ember Auto Import 允许您提供自定义 webpack 配置作为 autoImport.webpack 配置键:

// ember-cli-build.js

let app = new EmberApp(defaults,{
  autoImport: {
    webpack: {
      // extra webpack configuration goes here
    },});

请联系Ember Auto Import's documentation了解详情。

将两者放在一起,像这样应该工作:

// ember-cli-build.js

const CKEditorWebpackPlugin = require('@ckeditor/ckeditor5-dev-webpack-plugin');
const { styles } = require('@ckeditor/ckeditor5-dev-utils');

let app = new EmberApp(defaults,{
  autoImport: {
    webpack: {
      plugins: [
        new CKEditorWebpackPlugin({
          // See https://ckeditor.com/docs/ckeditor5/latest/features/ui-language.html
          language: "pl",module: {
        rules: [
          {
            test: /ckeditor5-[^/\\]+[/\\]theme[/\\]icons[/\\][^/\\]+\.svg$/,{
            test: /ckeditor5-[^/\\]+[/\\]theme[/\\].+\.css$/,use: [
              {
                loader: "style-loader",options: {
                  injectType: "singletonStyleTag",attributes: {
                    "data-cke": true,{
                loader: "postcss-loader",options: styles.getPostCssConfig({
                  themeImporter: {
                    themePath: require.resolve(
                      "@ckeditor/ckeditor5-theme-lark"
                    ),});

更新

这个解决方案几乎有效,但有几点需要注意:

从'@ckeditor/ckeditor5-image/src/image-resize'导入ImageResize; '@ckeditor/ckeditor5-build-classic' 中的 ClassicEditor 不起作用!显然这会导致重复模块错误。

因此,在执行此操作之后,按照有关如何 build from source 的说明进行操作,我发现 ckeditor 样式没有在 ember 中加载,直到我按照 extracting CSS 上的说明进行操作,然后一切正常!

>

以下是我的 package.json 中的相关部分:

"@ckeditor/ckeditor5-adapter-ckfinder": "^27.0.0","@ckeditor/ckeditor5-autoformat": "^27.0.0","@ckeditor/ckeditor5-basic-styles": "^27.0.0","@ckeditor/ckeditor5-block-quote": "^27.0.0","@ckeditor/ckeditor5-ckfinder": "^27.0.0","@ckeditor/ckeditor5-cloud-services": "^27.0.0","@ckeditor/ckeditor5-core": "^27.0.0","@ckeditor/ckeditor5-dev-utils": "^24.0.0","@ckeditor/ckeditor5-dev-webpack-plugin": "^24.4.2","@ckeditor/ckeditor5-easy-image": "^27.0.0","@ckeditor/ckeditor5-editor-classic": "^27.0.0","@ckeditor/ckeditor5-essentials": "^27.0.0","@ckeditor/ckeditor5-heading": "^27.0.0","@ckeditor/ckeditor5-image": "^27.0.0","@ckeditor/ckeditor5-indent": "^27.0.0","@ckeditor/ckeditor5-link": "^27.0.0","@ckeditor/ckeditor5-list": "^27.0.0","@ckeditor/ckeditor5-media-embed": "^27.0.0","@ckeditor/ckeditor5-paragraph": "^27.0.0","@ckeditor/ckeditor5-paste-from-office": "^27.0.0","@ckeditor/ckeditor5-table": "^27.0.0","@ckeditor/ckeditor5-theme-lark": "^27.0.0","@ckeditor/ckeditor5-typing": "^27.0.0","css-loader": "^5.2.2","mini-css-extract-plugin": "^1.5.0","postcss-loader": "^3.0.0","raw-loader": "^3.1.0","style-loader": "^2.0.0"

还有我的 ember-cli-build.js 文件:

  const CKEditorWebpackPlugin = require("@ckeditor/ckeditor5-dev-webpack-plugin");
  const { styles } = require("@ckeditor/ckeditor5-dev-utils");
  const MiniCssExtractPlugin = require( 'mini-css-extract-plugin' );


  module.exports = function (defaults) {
    let app = new EmberApp(defaults,{

     cssModules: {
       includeExtensionInModulePath: true,sassOptions: {
       inputFiles: [  
         '/app/styles/app.scss',includePaths: ['app','app/components']
      },// Add options here
      autoImport: {
        webpack: {
          plugins: [
           new CKEditorWebpackPlugin({
              // See https://ckeditor.com/docs/ckeditor5/latest/features/ui-language.html
              // language: "en",new MiniCssExtractPlugin( {
              filename: 'ckeditor.css'
            } )
          ],module: {
            rules: [
              {
                test: /ckeditor5-[^/\\]+[/\\]theme[/\\]icons[/\\][^/\\]+\.svg$/,{
                test: /ckeditor5-[^/\\]+[/\\]theme[/\\].+\.css$/,use: [
                  MiniCssExtractPlugin.loader,'css-loader',{
                    loader: "postcss-loader",options: styles.getPostCssConfig({
                    themeImporter: {
                      themePath: require.resolve(
                        "@ckeditor/ckeditor5-theme-lark"
                      ),});


  return app.toTree();
};

还有我的 ck-editor.js 文件(与我的组件保存在同一目录中):

import ClassicEditorBase from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
import EssentialsPlugin from '@ckeditor/ckeditor5-essentials/src/essentials';
import UploadAdapterPlugin from '@ckeditor/ckeditor5-adapter-ckfinder/src/uploadadapter';
import AutoformatPlugin from '@ckeditor/ckeditor5-autoformat/src/autoformat';
import BoldPlugin from '@ckeditor/ckeditor5-basic-styles/src/bold';
import ItalicPlugin from '@ckeditor/ckeditor5-basic-styles/src/italic';
import BlockQuotePlugin from '@ckeditor/ckeditor5-block-quote/src/blockquote';
// import EasyImagePlugin from '@ckeditor/ckeditor5-easy-image/src/easyimage';
import HeadingPlugin from '@ckeditor/ckeditor5-heading/src/heading';
import ImagePlugin from '@ckeditor/ckeditor5-image/src/image';
import ImageCaptionPlugin from '@ckeditor/ckeditor5-image/src/imagecaption';
import ImageStylePlugin from '@ckeditor/ckeditor5-image/src/imagestyle';
import ImageToolbarPlugin from '@ckeditor/ckeditor5-image/src/imagetoolbar';
import ImageUploadPlugin from '@ckeditor/ckeditor5-image/src/imageupload';
import LinkPlugin from '@ckeditor/ckeditor5-link/src/link';
import ListPlugin from '@ckeditor/ckeditor5-list/src/list';
import ParagraphPlugin from '@ckeditor/ckeditor5-paragraph/src/paragraph';
import ImageResize from '@ckeditor/ckeditor5-image/src/imageresize';

export default class ClassicEditor extends ClassicEditorBase {}

ClassicEditor.builtinPlugins = [
    EssentialsPlugin,UploadAdapterPlugin,AutoformatPlugin,BoldPlugin,ItalicPlugin,BlockQuotePlugin,// EasyImagePlugin,HeadingPlugin,ImagePlugin,ImageCaptionPlugin,ImageStylePlugin,ImageToolbarPlugin,ImageUploadPlugin,ImageResize,LinkPlugin,ListPlugin,ParagraphPlugin
];

ClassicEditor.defaultConfig = {
    toolbar: {
        items: [
            'heading','|','bold','italic','link','bulletedList','numberedList','uploadImage','blockQuote','undo','redo'
        ]
    },image: {
        toolbar: [
            'imageStyle:full','imageStyle:side','imageTextAlternative'
        ]
    },language: 'en'
};

现在,从我的组件中,我可以调用:

import ClassicEditor from './ck-editor';

...

didInsertElement() {
  var editor = ClassicEditor
    .create( document.querySelector( '#editor' ),{
      ...
    });
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-