Error: 在 Shopware 6.5 中找不到模块 ‘./image/name-multicolor.svg’

huangapple go评论69阅读模式
英文:

Uncaught (in promise) Error: Cannot find module './image/name-multicolor.svg' in Shopware 6.5

问题

I'm working on a plugin for Shopware 6.5, and I'm encountering an issue with loading custom SVG icons. The plugin worked fine in Shopware 6.4, but after upgrading to 6.5, I'm getting the following error:

Uncaught (in promise) Error: Cannot find module './image/name-multicolor.svg'

Here's my code for the custom Webpack configuration (webpack.config.js):


function resolve(dir) {
    return path.join(__dirname, '..', dir);
}

module.exports = (baseConf) => {
    // Exclude the plugin's icons from being loaded via a url-loader
    baseConf.config.module.rules.forEach((rule) => {
        if (rule.loader === 'url-loader') {
            if (!rule.exclude) {
                rule.exclude = [];
            }
            rule.exclude.push(resolve('src/app/assets/icons/svg'));
        }
    });

    // Add svg-inline-loader for the plugin icons
    return {
        module: {
            rules: [
                {
                    test: /\.svg$/,
                    include: [
                        resolve('src/app/assets/icons/svg'),
                    ],
                    loader: 'svg-inline-loader',
                    options: {
                        removeSVGTagAttrs: false,
                    },
                },
            ],
        },
    };
};

This is how I import and register my custom SVG icons in icons.js:

    const context = require.context('./svg', false, /svg$/);

    return context.keys().reduce((accumulator, item) => {
        const componentName = item.split('.')[1].split('/')[1];
        const component = {
            name: componentName,
            functional: true,
            render(createElement, elementContext) {
                const data = elementContext.data;

                return createElement('span', {
                    class: [data.staticClass, data.class],
                    style: data.style,
                    attrs: data.attrs,
                    on: data.on,
                    domProps: {
                        innerHTML: context(item),
                    },
                });
            },
        };

        accumulator.push(component);
        return accumulator;
    }, []);
})();

This is the twig where I'm using it:

{% block settings_icon %}
    <sw-icon name="image-name-multicolor" size="30" multicolor></sw-icon>
{% endblock %}

Path to SVG image:

src/Resources/app/administration/src/app/assets/icons/svg/icons-image-name-multicolor.svg

I've already checked the following:

  1. The path to the SVG files is correct and points to the directory containing the SVG files.

  2. I've cleared caches, temporary files, and generated assets, then re-run the build process.

Despite trying these steps, I still get the same error. What could be causing this issue, and how can I resolve it? Any help would be appreciated.

英文:

I'm working on a plugin for Shopware 6.5, and I'm encountering an issue with loading custom SVG icons. The plugin worked fine in Shopware 6.4, but after upgrading to 6.5, I'm getting the following error:

Uncaught (in promise) Error: Cannot find module &#39;./image/name-multicolor.svg&#39;

Here's my code for the custom Webpack configuration (webpack.config.js):


function resolve(dir) {
    return path.join(__dirname, &#39;..&#39;, dir);
}

module.exports = (baseConf) =&gt; {
    // Exclude the plugin&#39;s icons from being loaded via a url-loader
    baseConf.config.module.rules.forEach((rule) =&gt; {
        if (rule.loader === &#39;url-loader&#39;) {
            if (!rule.exclude) {
                rule.exclude = [];
            }
            rule.exclude.push(resolve(&#39;src/app/assets/icons/svg&#39;));
        }
    });

    // Add svg-inline-loader for the plugin icons
    return {
        module: {
            rules: [
                {
                    test: /\.svg$/,
                    include: [
                        resolve(&#39;src/app/assets/icons/svg&#39;),
                    ],
                    loader: &#39;svg-inline-loader&#39;,
                    options: {
                        removeSVGTagAttrs: false,
                    },
                },
            ],
        },
    };
};

This is how I import and register my custom SVG icons in icons.js:

    const context = require.context(&#39;./svg&#39;, false, /svg$/);

    return context.keys().reduce((accumulator, item) =&gt; {
        const componentName = item.split(&#39;.&#39;)[1].split(&#39;/&#39;)[1];
        const component = {
            name: componentName,
            functional: true,
            render(createElement, elementContext) {
                const data = elementContext.data;

                return createElement(&#39;span&#39;, {
                    class: [data.staticClass, data.class],
                    style: data.style,
                    attrs: data.attrs,
                    on: data.on,
                    domProps: {
                        innerHTML: context(item),
                    },
                });
            },
        };

        accumulator.push(component);
        return accumulator;
    }, []);
})();

This is the twig where i'm using it:

{% block settings_icon %}
    &lt;sw-icon name=&quot;image-name-multicolor&quot; size=&quot;30&quot; multicolor&gt;&lt;/sw-icon&gt;
{% endblock %}

Path to svg image:

src/Resources/app/administration/src/app/assets/icons/svg/icons-image-name-multicolor.svg

I've already checked the following:

1.The path to the SVG files is correct and points to the directory containing the SVG files.

2.I've cleared caches, temporary files, and generated assets, then re-run the build process.

Despite trying these steps, I still get the same error. What could be causing this issue, and how can I resolve it? Any help would be appreciated.

答案1

得分: 1

自Shopware 6.5版本开始,使用了meteor图标包。此外,大多数图标都是异步加载的。命名约定意味着破折号之前的单词是图标所在的文件夹。因此,例如regular-circle将被解析为以下异步导入:

import('@shopware-ag/meteor-icon-kit/icons/regular/circle.svg')

有关参考,请查看sw-icon组件。

这应该解释了为什么无法在@shopware-ag/meteor-icon-kit/icons/image/name-multicolor.svg的外部供应商目录中解析您的自定义图标。

您可以尝试添加路径别名到您的webpack配置中,指向您的自定义图标。

英文:

Since Shopware 6.5 the meteor icon kit is being used. Furthermore most icons are being loaded async. The naming convention intends that the word before the first dash is the folder the icon is in. So for example regular-circle will be resolved into an asynchronous import like so:

import(&#39;@shopware-ag/meteor-icon-kit/icons/regular/circle.svg&#39;)

See the sw-icon component for reference.

That should explain why your custom icon can't be resolved within a foreign vendor directory at @shopware-ag/meteor-icon-kit/icons/image/name-multicolor.svg.

You could try adding a path alias to your webpack config pointing to your custom icons.

huangapple
  • 本文由 发表于 2023年4月19日 21:33:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76055180.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定