【发布时间】:2020-04-25 22:39:17
【问题描述】:
我使用带有 typescript 模板的 create-react-app 创建了一个 react 应用程序,并运行了弹出命令。
我只需要从外部 json 文件中获取配置并从应用程序中的任何位置的组件文件中读取它。 所以我做了以下更改以使用 webpack config json
tsconfig.json
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react"
},
"include": [
"src"
]
}
webpack.configjson // 加载外部配置文件
externals: {
'Configurator': JSON.stringify(process.env.NODE_ENV === 'production' ? require('./config.prod.json') : require('./config.dev.json'))
}
Ts 文件
import Configurator from 'Configurator';
但它给出了以下错误
【问题讨论】:
标签: reactjs typescript webpack