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?

Alex
  • 32,506
  • 16
  • 106
  • 171
MweisIMI
  • 629
  • 3
  • 13
  • 1
    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. – Christopher Dosin Aug 06 '21 at 06:15
  • @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? – Alex Aug 06 '21 at 08:08
  • I think if the plugin would extend the webpack config like this: https://developer.shopware.com/docs/guides/plugins/plugins/administration/extending-webpack and register an alias, it would be very simple – Alex Nov 28 '21 at 15:49

4 Answers4

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`
Yash Sonalia
  • 378
  • 2
  • 8
  • Is there something like a plugin root variable, to make it portable is the plugin was installed via admin panel and not composer? – Alex Nov 26 '21 at 18:47
  • I does seem to work. with backticks I get a syntax error, without a "can't resolve" – Alex Nov 28 '21 at 15:34
  • You cannot use backticks in import, this won't work: https://stackoverflow.com/questions/54436091/technical-explaination-why-backtick-doesnt-work-with-import-statement – Jolan Mar 10 '22 at 09:13
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?

MweisIMI
  • 629
  • 3
  • 13
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.

Alex
  • 32,506
  • 16
  • 106
  • 171
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';
Abin John
  • 103
  • 10