【发布时间】:2018-03-21 13:29:54
【问题描述】:
我目前正在使用React 进行一个项目,我使用Typescript 作为语言。在我的项目中,我安装了Webpack。一切正常,但现在,由于我们要投入生产,我希望有一种简单的方法来存储/检索config settings,例如服务器 URL(通常在开发、测试和生产阶段之间有所不同),但我被卡住了。我尝试通过添加"externals" 键来使用webpack.config.js 文件:
externals: {
'config': JSON.stringify(process.env.ENV === 'production' ? {
serviceUrl: "https://prod.myserver.com"
} : {
serviceUrl: "http://localhost:8000"
})
}
然后尝试从我的tsx 组件文件中引用该文件(考虑到webpack.config.js 位于根文件夹中,而我的组件位于/ClientApp/components 中):
import config from '../../webpack.config.js';
或
import {externals} from '../../webpack.config.js';
但我收到以下错误消息:
'webpack.config.js' 被解析为 '[PROJECT_DIR]/webpack.config.js', 但 '--allowJs' 没有设置。
任何解决方案/替代方案来解决这个问题?谢谢
【问题讨论】:
-
你试过这个解决方案了吗?
If you're using typescript, just rename the file extension to .ts instead of .js. (foo.ts) . Then when you import, do not use the file extension: import ( Foo ) from '../node_modules/foo/foo';(来自coffeenexusin typescript Github issue)
标签: reactjs typescript webpack config