javascript - Typescript/babel 导入导致 "_1.default is not a function"

标签 javascript typescript webpack babeljs

我正在尝试使用https://github.com/timmywil/panzoom来自使用 webpack 和 babel 编译的 typescript 项目。

问题在于 typescript 方法调用:

import Panzoom from '@panzoom/panzoom';
Panzoom(document.querySelector("#pic"));

被转译为以下 JavaScript:

panzoom_1.default(document.querySelector("#pic"));

然后生成以下运行时错误:

Uncaught TypeError: panzoom_1.default is not a function

如果我调试 JavaScript,则 panzoom_1 具有预期的函数签名,并且没有 default 成员。

这是无数不同类型的模块、默认导出以及 babel 和 typescript 导入它们的方式差异之间的某种类型的问题,但我完全迷失了。根据文档,panzoom 是一个 UMD 模块(如果有帮助的话)。

我找到了一种解决方法,可以以不同的方式导入,然后将其强制转换为任何,但这显然很疯狂,对吧?:

import * as Panzoom from '@panzoom/panzoom';
(<any>Panzoom)(document.querySelector("#pic"));

这是项目配置:

测试.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">   
</head>
<body>
    <img src="pic.jpg" id="pic" />
</body>
<script src="dist/bundle.js" type = "text/javascript"></script>
</html>

测试.ts

import Panzoom from '@panzoom/panzoom';
Panzoom(document.querySelector("#pic"));

tsconfig.json

{
    "compilerOptions": {
      "outDir": ".",
      "sourceMap": false,
      "noImplicitAny": true,
      "suppressImplicitAnyIndexErrors": true,
      "module": "commonjs",
      "target": "es6",
      "jsx": "react",
      "allowSyntheticDefaultImports": true,
      "traceResolution": true,
      "experimentalDecorators": true,
      "baseUrl": ".",
    }
  }

package.json

{
  "name": "grr",
  "version": "0.0.0",
  "private": true,
  "dependencies": {
    "@babel/polyfill": "^7.8.7",
    "@panzoom/panzoom": "^4.1.0",
    "npm-update-all": "^1.0.1",
    "webpack": "^4.43.0",
    "webpack-cli": "^3.3.11"
  },
  "devDependencies": {
    "@babel/cli": "^7.8.4",
    "@babel/core": "^7.9.6",
    "@babel/plugin-proposal-class-properties": "^7.8.3",
    "@babel/plugin-proposal-object-rest-spread": "^7.9.6",
    "@babel/plugin-transform-async-to-generator": "^7.8.3",
    "@babel/preset-env": "^7.9.6",
    "@babel/preset-react": "^7.9.4",
    "@babel/preset-typescript": "^7.9.0",
    "awesome-typescript-loader": "^5.2.1",
    "babel-loader": "^8.1.0",
    "typescript": "^3.8.3"
  }
}

webpack.config.js

var webpack = require("webpack");
var path = require('path');

module.exports = (env, options) => {

    var PROD = (options.mode === 'production');

    return {

        entry: [
            "@babel/polyfill",
            path.resolve(__dirname, "test.ts")
        ],

        output: {
            filename: "bundle.js",
            libraryTarget: "var"
        },

        resolve: {
            modules: [
                'node_modules'
            ],
            extensions: [".ts", ".tsx", ".js", ".json"]
        },

        module: {
            rules: [
                {
                    test: /\.tsx?$/, 
                    loaders: [
                        {
                            loader: 'babel-loader',
                            options:
                                {
                                    compact: false,
                                    presets: [
                                        [
                                            "@babel/preset-env",
                                            {
                                                targets: "> 0.25%, not dead"
                                            }
                                        ]
                                    ]
                                }
                        },
                        'awesome-typescript-loader'
                    ]
                }
            ]
        },
        devtool : false,
        optimization: {
            minimize: PROD
        }
    }
};

.babelrc

{
  "presets": ["@babel/env"],
  "plugins": ["@babel/transform-async-to-generator"],
  "compact":false
}

最佳答案

我已成功通过将 "esModuleInterop": true 添加到 tsconfig.json 来修复此问题。

https://www.typescriptlang.org/docs/handbook/compiler-options.html

Emit __importStar and __importDefault helpers for runtime babel ecosystem compatibility and enable --allowSyntheticDefaultImports for typesystem compatibility.

这对我来说没有任何意义,但这里有更多信息:

Understanding esModuleInterop in tsconfig file

关于javascript - Typescript/babel 导入导致 "_1.default is not a function",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61524130/

相关文章:

javascript - JavaScript花絮如何将远程小部件插入到页面上?

javascript - 根据其他接口(interface)的属性创建接口(interface)

typescript - 在 TS 文件中的 Vue 组件上使用 VS 调试器时出现 "Unverified Breakpoints"

css - 样式不适用于使用 react-css-modules

javascript - 在没有 JQuery 的情况下使用响应式图像映射

javascript - HTML5 超链接表格数据

javascript - TypeScript - 获取泛型类参数的类型

Angular-cli Webpack 第三部分 css 文件

javascript - 在canvas中上传多张图片转换为一张图片(Html5)

reactjs - 导入文件时用 typescript 开 Jest 会引发错误