简体   繁体   中英

Webpack 2 ES6 import UMD without default export

I'm trying to import UMD libraries using Webpack 2 and ts-loader . It used to work using Webpack 1 and Rollup (without TypeScript), but Webpack 2 appends .default when invoking imported function.

For example:

import canvg from 'canvg';
canvg();

transforms into

var canvg_1 = require("canvg");
canvg_1.default();

and I get Uncaught TypeError: canvg_1.default is not a function .

How to fix it?

The problem was in TypeScript config, I've added a module: 'es2015' into my tsconfig.json and it worked. Also allowSyntheticDefaultImports: true may help in some cases (not necessary in my case, some analog for babel-plugin-add-module-exports described by @alejandro-garcia-anglada).

{
    "compilerOptions": {
        "module": "es2015",
        "allowSyntheticDefaultImports": true
    }
}

Using babel-plugin-add-module-exports you can make sure everything works as expected.

https://www.npmjs.com/package/babel-plugin-add-module-exports

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM