10

我目前正在尝试从现有插件覆盖 javascript 文件。

我一直在关注文档,但我在为 JS 类覆盖的路径而苦苦挣扎。

在文档中是一个示例代码:

import CookiePermissionPlugin from 'src/plugin/cookie/cookie-permission.plugin';

export default class MyCookiePermission extends CookiePermissionPlugin {
}

所以我实现了以下代码:

import QuantityField from 'src/plugin/FilterRangeSlider/filter-range-slider.plugin';

export default class ExampleQuantityField extends QuantityField {

此代码对我不起作用,因为原始文件位于供应商目录中,而我的插件位于自定义目录中。尝试编译(例如bin/build-storefront.sh)时,我收到以下错误消息:

找不到模块:错误:无法解析“<项目根目录>/custom/plugins/ExampleProductFilter/src/Resources/app/storefront/src/filter-中的“src/plugin/FilterRangeSlider/filter-range-slider.plugin”范围滑块'

知道如何按照文档中的说明导入该类吗?

4

3 回答 3

3

Node.js 提供了一堆内置的文件系统功能。__dirname指向根目录。

所以,这应该有效。

import QuantityField from `${__dirname}/vendor/store.shopware.com/mmeesrangesliderpro/src/Resources/app/storefront/src/script/filter-range-slider.plugin`
于 2021-11-26T14:28:02.783 回答
2

我目前的解决方案不是很干净......

import QuantityField from '../../../../../../../../../vendor/store.shopware.com/mmeesrangesliderpro/src/Resources/app/storefront/src/script/filter-range-slider.plugin';

没有任何插件根变量或类似的东西吗?

于 2021-08-06T08:42:47.890 回答
0

有 - 我相信 - 没有更简单的方法可以做到这一点。

如果每个插件都将扩展 webpack 配置,如中所述

https://developer.shopware.com/docs/guides/plugins/plugins/administration/extending-webpack

const path = require('path');

module.exports = () => {
    return {
    resolve: {
        alias: {
            MmeesRangeSliderPro: path.join(__dirname, '..', 'src')
        }
    }
    };
};

可以使用别名代替插件根。

但事实并非如此,因此以下方法不起作用

import QuantityField from 'MmeesRangeSliderPro/plugin/FilterRangeSlider/filter-range-slider.plugin';

您可以console.log(webpackConfig)在底部添加一个Resources/app/storefront/webpack.config.js来验证这一点。

   alias: {
      src: '/home/user/example/projects/example.de/vendor/shopware/storefront/Resources/app/storefront/src',
      assets: '/home/user/example/projects/example.de/vendor/shopware/storefront/Resources/app/storefront/assets',
      jquery: 'jquery/dist/jquery.slim',
      scss: '/home/user/example/projects/example.de/vendor/shopware/storefront/Resources/app/storefront/src/scss',
      vendor: '/home/user/example/projects/example.de/vendor/shopware/storefront/Resources/app/storefront/vendor'
    }

那些再次不允许定位模块。

于 2021-11-28T16:11:35.543 回答