How to add an older typescript class to a webpack project


I have inherited a project with some just JS (written in a typescript library). I am trying to update the project to use Webpack, but am getting issues loading the ts library into my JS script.

The module has installed via Yarn/NPM OK (as well as it’s dependencies), I updated the typescript class to the following:

import { fabric } from 'fabric';

export class PlatePreview {

I also updated the tsconfig.json to identify it as a module

{
    "compilerOptions": {
        "target": "es5",
        "watch": true,
        "module": "CommonJS",
        "sourceMap": false,
        "rootDir": ".",
        "isolatedModules": false,
        "removeComments": true,
        "preserveConstEnums": true,
        "noImplicitUseStrict": false,
        "declaration": false
    },
    "include": (
        "src/*"
    ),
    "exclude": (
        "node_modules",
        "**/*.spec.ts"
    )
}

My full webpack config is as follows:

const Path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const PATHS = {
  MODULES: Path.resolve('node_modules'), // your node_modules folder name, or full path
  SC: Path.resolve('../silvercommerce/src'),
  FILES_PATH: '../', // relative path from your css files to your other files, such as images and fonts
  ROOT: Path.resolve(), // the root path, where your webpack.config.js is located.
  SRC: Path.resolve('src'), // the root path, where your webpack.config.js is located.
  SCSS: Path.resolve('src/scss'), // the root path to your source files
  CSS: Path.resolve('css'), // the root path to your source files
  JS: Path.resolve('javascript'), // the root path to your built files
};

module.exports = {
    entry: {
        bundle: (
            `${PATHS.MODULES}/jquery/dist/jquery.js`,
            `${PATHS.MODULES}/popper.js/dist/umd/popper.js`,
            `${PATHS.MODULES}/tether/dist/js/tether.js`,
            `${PATHS.MODULES}/bootstrap/dist/js/bootstrap.js`,
            `${PATHS.MODULES}/imagesloaded/imagesloaded.pkgd.js`,
            `${PATHS.MODULES}/jquery-match-height/dist/jquery.matchHeight-min.js`,
            `${PATHS.MODULES}/jquery-zoom/jquery.zoom.min.js`,
            `${PATHS.MODULES}/@fortawesome/fontawesome-free/js/all.js`
        ),
        script: (
            `${PATHS.MODULES}/fabric/dist/fabric.js`,
            `${PATHS.MODULES}/@ilateral/bestplate-previewer/src/plate-preview.ts`,
            `${PATHS.SC}/javascript/script.js`,
            `${PATHS.SRC}/javascript/script.js`
        ),
        bundlecss: (
            `${PATHS.SCSS}/bundle.scss`
        ),
        editor: (
            `${PATHS.SCSS}/editor.scss`
        )
    },
    output: {
        path: PATHS.ROOT,
        filename: 'javascript/(name).js',
        publicPath: PATHS.ROOT
    },
    module: {
        rules: (
            {
                test: /.tsx?$/,
                loader: 'ts-loader'
            },
            {
                test: /.(sa|sc|c)ss$/,
                use: (
                {
                    loader: MiniCssExtractPlugin.loader,
                    options: {
                        hmr: process.env.NODE_ENV === 'development',
                    },
                },
                'css-loader',
                'sass-loader',
                )
            },
            {
                test: /.(png|jpe?g|gif)$/,
                use: (
                    {
                        loader: 'file-loader',
                        options: {
                            outputPath: 'images',
                            publicPath: '../images',
                            name: '(name).(ext)?(hash)'
                        },
                    },
                )
            },
            {
                test: /.(woff(2)?|ttf|eot|svg)(?v=d+.d+.d+)?$/,
                use: (
                    {
                        loader: 'file-loader',
                        options: {
                            name: '(name).(ext)',
                            outputPath: 'fonts/',
                            publicPath: '../fonts'
                        }
                    }
            )   
            }
        )
    },
    plugins: (
        new MiniCssExtractPlugin(
            {
                path: PATHS.ROOT,
                filename: 'css/(name).css',
                chunkFilename: 'css/(id).css',
            }
        )
    ),
    resolve: {
        // Expose Jquery Globally
        alias: {
            jquery: "jquery/src/jquery",
            fabric: 'fabric'
        }
    }
};

Everything else works OK, and webpack generates the JS files, but when I add the following to my script.js:

import PlatePreview from '@ilateral/bestplate-previewer';

var preview = new PlatePreview(`PlatePreview${id}`);

I get the error Uncaught TypeError: _ilateral_bestplate_previewer__WEBPACK_IMPORTED_MODULE_0___default.a is not a constructor

I also tried new PlatePreview.PlateReview(PlatePreview${id});, but that also generated a similar error.

I also tried changing the import to import { PlatePreview } from '@ilateral/bestplate-previewer'; but still get the same error.

Anyone got any ideas what I am doing wrong?

Many thanks,

Mo