gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 20:54:31 24 4
gpt4 key购买 nike

我正在尝试使用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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com