I am attempting to integrate the "virtual-select-plugin" library into my Typescript project using webpack. Unfortunately, this library does not have any type definitions. Upon installation via npm, I encountered the following error in the browser:
TypeError: virtual_select_plugin__WEBPACK_IMPORTED_MODULE_13___default(...).init is not a function
Surprisingly, Typescript and webpack did not raise any errors.
In comparison, webpack successfully handles other libraries like bootstrap and flickity without any issues:
...
/* harmony import */ var flickity__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! flickity */ "./node_modules/flickity/js/index.js");
/* harmony import */ var flickity__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__webpack_require__.n(flickity__WEBPACK_IMPORTED_MODULE_2__);
...
/* harmony import */ var virtual_select_plugin__WEBPACK_IMPORTED_MODULE_13__ = __webpack_require__(/*! virtual-select-plugin */ "./node_modules/virtual-select-plugin/dist/virtual-select.min.js");
/* harmony import */ var virtual_select_plugin__WEBPACK_IMPORTED_MODULE_13___default = /*#__PURE__*/__webpack_require__.n(virtual_select_plugin__WEBPACK_IMPORTED_MODULE_13__);
...
An observation made was that
var flickity__WEBPACK_IMPORTED_MODULE_2__
contains properties and functions, while var virtual_select_plugin__WEBPACK_IMPORTED_MODULE_13__
appears as an empty object. The code from virtual-select-plugin is imported later in the bundle from the file "./node_modules/virtual-select-plugin/dist/virtual-select.min.js"
.
To address this issue, I created a typedef in decs.d.ts:
declare module 'virtual-select-plugin'
The plugin is imported with the following statement:
import VirtualSelect from 'virtual-select-plugin';
Subsequently, when attempting to call the init function:
// However, the browser says no...
VirtualSelect.init({
ele: ...,
options: ...,
});
An alias has been included in the webpack configuration:
...
resolve: {
alias: {
'virtual-select-plugin': 'virtual-select-plugin/dist/virtual-select.min',
},
extensions: [
'.js',
'.jsx',
'.ts',
'.tsx',
],
},
...
After spending 6 hours on research, I still cannot find a solution. Any assistance would be greatly appreciated. Thank you.