如何在webpack中允许命名空间?

问题描述 投票:0回答:1

我使用以下方法创建了TypeScript React应用程序:

yarn create react-app my-app --template typescript

这样,我的项目用Babel编译,然后被webpack捆绑。现在,我想使用TypeScript namespaces,默认情况下Babel不支持TypeScript can be enabled,但它们是@babel/plugin-transform-typescript。我安装了所有必需的软件包:

customize-cra中,我将customize-cra更改为package.json

最后,我创建了一个react-scripts start文件:

react-app-rewired start

但是,编译仍然会引发语法错误,就像没有启用任何插件一样:

SyntaxError:/home/m93a/my-app/script.ts:未标记名称空间的仅声明类型。非声明性名称空间仅在Babel中受实验支持。要启用和查看警告,请参阅:config-overrides.js

如何正确设置项目,以便甚至编译非声明性的名称空间?

typescript webpack babel react-scripts
1个回答
0
投票

1-您还应该添加const { override, addExternalBabelPlugin } = require("customize-cra"); module.exports = override( addExternalBabelPlugin([ "@babel/plugin-transform-typescript", { allowNamespaces: true } ]) ); ,例如https://babeljs.io/docs/en/babel-plugin-transform-typescript

2-您必须对addBabelPluginsaddExternalBabelPlugin函数使用扩展语法。

addExternalBabelPlugin
© www.soinside.com 2019 - 2024. All rights reserved.