Overwrite an existing plugin JS in Shopware 6
Asked Answered
P

4

12

I am currently trying to overwrite a javascript file from an existing plugin.

I've been following the documentation but I am struggling with the path for the JS class to overwrite.

In the docs is an example code:

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

export default class MyCookiePermission extends CookiePermissionPlugin {
}

So I implemented the following code:

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

export default class ExampleQuantityField extends QuantityField {

This code does not work for me, since the original file is in the vendor directory and my plugin is in the custom directory. When trying to compile (eg bin/build-storefront.sh) I receive the following error message:

Module not found: Error: Can't resolve 'src/plugin/FilterRangeSlider/filter-range-slider.plugin' in '<project root>/custom/plugins/ExampleProductFilter/src/Resources/app/storefront/src/filter-range-slider'

Any idea how I can import that class as stated in the docs?

Pademelon answered 5/8, 2021 at 16:22 Comment(3)
In your example you paste code from the CookiePermissionPlugin - In your Error message it's the FilterRangeSlider from within the plugin. Please show the exact code which you have and not what's written within the docs.Felucca
@ChristopherDosin MweisIMI edited the question. I guess it would work to import from ../../../../(no clue how many times)/../vendor/store.shopware.com/..... but is that a clean solution?Solorio
I think if the plugin would extend the webpack config like this: developer.shopware.com/docs/guides/plugins/plugins/… and register an alias, it would be very simpleSolorio
T
4

Node.js provides a bunch of in-built file-system functionalities. The __dirname points to the root directory.

So, this should work.

import QuantityField from `${__dirname}/vendor/store.shopware.com/mmeesrangesliderpro/src/Resources/app/storefront/src/script/filter-range-slider.plugin`
Trichinopoly answered 26/11, 2021 at 14:28 Comment(3)
Is there something like a plugin root variable, to make it portable is the plugin was installed via admin panel and not composer?Solorio
I does seem to work. with backticks I get a syntax error, without a "can't resolve"Solorio
You cannot use backticks in import, this won't work: #54436591Trogon
P
2

My current solution is not really clean...

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

Isnt there any plugin root variable or something similar?

Pademelon answered 6/8, 2021 at 8:42 Comment(1)
wondering if there is a better way, avoiding this series of "../"Solorio
S
0

There is - I believe - no easier way to accomplish this.

If each plugin would extend the webpack config as described in

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')
        }
    }
    };
};

The alias could be used instead of the Plugin Root.

But this is not the case, so the following is not working:

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

You can add a console.log(webpackConfig) to the bottom of Resources/app/storefront/webpack.config.js to validate this.

   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'
    }

And those again to not really allow locating the module.

Solorio answered 28/11, 2021 at 16:11 Comment(0)
M
0

If it is a third-party plugin, replace the path with an absolute path like the following

import ThirdPartyPlugin from '/app/custom/plugins/ThirdPartyPlugin/src/Resources/app/storefront/src/third-party-plugin/third-party-plugin.plugin';
Melton answered 23/6, 2022 at 5:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.