我目前遇到了一个奇怪的错误。我正在尝试使用json-loader和import来加载一个json文件。
import * as ApiConfig from "../../src/conf.json";
我已经创建了一个 typings.d.ts 来定义 css 和 json 模块。
declare module "*.json" {
const value: any;
export default value;
}
declare module "*.css" {
const content: any;
export default content;
}
当我没有在WebStorm中打开我的typings.d.ts文件时,我会遇到这个错误。
Error:(1, 28) TS2307:Cannot find module '../../src/conf.json'.
但是当我打开它时,就不会出现这个错误。
以下是我的tsconfig.json文件:
{
"compilerOptions": {
"module": "commonjs",
"target": "es2015",
"jsx": "react",
"sourceMap": true,
"allowJs": true,
"experimentalDecorators": true
},
"exclude": [
"node_modules"
]
}
这是我的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"
}
]
谢谢