Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import non-module vendor script in webpack

I'm building an app with the AirConsole JS service. AirConsole only provides their library as a .js file you would include in your page with the usual:

<script type="text/javascript" src="https://www.airconsole.com/api/airconsole-1.6.0.js"></script>

However, I'm using Webpack and would like to import the script into my other JS files. I have tried a few methods with no luck:

  1. Create an entry file named vendor which imports the airconsole.js file. This creates a vendor.bundle.js file which I can include on my page
  2. Add the AirConsole path to my index entry point so the script is included in the bundle.js file. With this method I can verify the AirConsole code is included in the bundle.js file but attempting to create a new instance of AirConsole results in AirConsole is undefined

Am I on the right track with these methods? If not, what is the recommended way to import a non-module .js file?

like image 748
Brett DeWoody Avatar asked Feb 03 '17 22:02

Brett DeWoody


People also ask

Does webpack use CommonJS?

Webpack supports the following module types natively: ECMAScript modules. CommonJS modules.

Does webpack support import?

The imports loader allows you to use modules that depend on specific global variables. This is useful for third-party modules that rely on global variables like $ or this being the window object. The imports loader can add the necessary require('whatever') calls, so those modules work with webpack.


1 Answers

The best way is by an action which we call "shimming". You can check out our new docs page for information. There are a few different ways to do on it (that depend on the needs) for your non-module.

https://webpack.js.org/guides/shimming/

like image 96
Sean Larkin Avatar answered Sep 28 '22 11:09

Sean Larkin