javascriptwebpacktether

webpack: Tether is not defined


I'm creating a WebExtension, I need to use the selection-menu library in my content.js. It has in tether dependencies, so I installed:

npm install tether
npm install selection-menu

Then in my code I write:

import * as Tether from 'tether'
import SelectionMenu from 'selection-menu'

...
    this.selection_menu = new SelectionMenu({
      container: document.body,
      content: '<button> click me </button>',
      onselect: function(e) {
        this.menu.innerHTML = 'Selection length: ' + this.selectedText.length;
      }
    });

but when I select text, I get an error:

VM1492:23 Uncaught ReferenceError: Tether is not defined
    at i.show (<anonymous>:23:2899)
    at <anonymous>:23:4192

but in the page code I see that tether was importedscreen

how to fix this error?


Solution

  • This work for me:

    in my webpack.config.js:

    config.plugins = (config.plugins || []).concat([
    ...
      new webpack.ProvidePlugin({
        tether: 'tether',
        Tether: 'tether',
        'window.Tether': 'tether',
      })
    ]);