我目前正在将应用程序转换为打字稿。我的 npm 启动没有任何问题,但是当我尝试运行反应故事书时,我遇到了此错误
ERROR in ./src/conf.json
Module build failed: SyntaxError: Unexpected token m in JSON at position 0
at Object.parse (native)
at Object.module.exports (/node_modules/json-loader/index.js:7:48)
@ ./srcReact/actions/ApiActions.js 4:16-46
两者都在 webpack.config 中使用相同的加载器.js
loaders: [
{
exclude: [
/.html$/,
/.(js|jsx)$/,
/.css$/,
/.json$/,
/.svg$/
],
loader: 'url-loader',
query: {
limit: 10000,
name: 'static/media/[name].[hash:8].[ext]'
}
},
{
test: /.js$/,
loader: 'babel-loader',
include: [ path.resolve(__dirname, '../') ],
exclude: [ path.resolve(__dirname, 'bundles') ],
query: {
presets: ['react', 'es2015'],
plugins: ['transform-decorators-legacy'],
cacheDirectory:true
},
exclude: /(node_modules|bower_components)/
},
{
test: /.css$/,
include: [ path.resolve(__dirname, '../') ],
exclude: [ path.resolve(__dirname, 'bundles') ],
loader: "style-loader!css-loader"
},
{
test: /.svg$/,
loader: "svg-loader"
},
{
test: /.json$/,
loader: "json-loader"
}
]
我还有一个用于json的json.d.ts
declare module "*.json" {
const value: any;
export default value;
}
经过一些测试,看起来故事书无法加载我的json定义(json.d.ts(。
谢谢
最后通过将我的模块声明更改为
declare module "!json!*" {
const value: any;
export default value;
}
添加第一个!消除了json-loader和TypeScript模块之间的混淆。受到这个 github 问题的启发 https://github.com/webpack-contrib/json-loader/issues/13