I'm trying to use fabric.js with Typescript and Webpack aside some other modules in a Laravel 5.4 application that work fine in the browser. @types/fabric ist installed and Typescript behaves correct. New to Typescript as well as Webpack i tried some variants with no success.
The problem
result.js:198 Uncaught TypeError: __WEBPACK_IMPORTED_MODULE_1_fabric___default.a.Canvas is not a constructor
Variant A
code.ts
import fabric from "fabric";
const canvas = new fabric.Canvas('my-canvas');
result.js
var canvas = new __WEBPACK_IMPORTED_MODULE_1_fabric__default.a.Canvas('my-canvas');
WEBPACK_IMPORTED_MODULE_1_fabric__default requires WEBPACK_IMPORTED_MODULE_1_fabric which is an object with fabric as key.
const canvas = new fabric.fabric.Canvas('my-canvas');
would be working for Webpack but is not conform to type inspections.
Variant B
code.ts
import * as fabric from "fabric";
const canvas = new fabric.Canvas('my-canvas');
result.js
var canvas = new __WEBPACK_IMPORTED_MODULE_1_fabric__["Canvas"]('my-canvas');
WEBPACK_IMPORTED_MODULE_1_fabric contains an object with fabric as key so that the situation is like with variant A.
Variant C
code.ts
import {Canvas} from "fabric";
const canvas = new Canvas('my-canvas');
result.js
var canvas = new __WEBPACK_IMPORTED_MODULE_1_fabric__["Canvas"]('my-canvas');
At the end it is the same as with variant B.
excerpt of webpack.mix.js
.webpackConfig({
module: {
rules: [
{
test: /\.js$/,
loader: 'babel-loader',
include: [
path.resolve('app/public/js'),
path.resolve('node_modules/countable')
],
},
{
test: /\.vue$/,
loader: 'vue-loader',
exclude: [
path.resolve('resources/assets/ts/components')
],
options: {
loaders: {
'scss': 'vue-style-loader!css-loader!sass-loader',
'sass': 'vue-style-loader!css-loader!sass-loader?indentedSyntax',
}
}
},
{
test: /\.tsx?$/,
loader: 'ts-loader',
exclude: /node_modules/,
options: {
appendTsSuffixTo: [/\.vue$/],
}
},
]
},
plugins: [
new webpack.ProvidePlugin({
'_': 'lodash',
'$': 'jquery',
'jQuery': 'jquery',
'window.jQuery': 'jquery',
}),
new LiveReloadPlugin()
],
resolve: {
extensions: ['.js', '.ts', '.vue', '.jsx', '.tsx', '.vuex'],
alias: {
'vue$': 'vue/dist/vue.esm.js',
}
}
The problem seem to be in Webpack and/or the babel-loader doesn't take the fabric namepace into account.
So the question is if there is any method to tell Webpack to handle this import in a way that it directly references fabric (or an imported library of that kind) or to import it into Typescript in a way that also Webpack is happy?