awesome-typescript-loader无法接收JSON中的更改

时间:2019-02-14 16:49:22

标签: json typescript webpack webpack-dev-server awesome-typescript-loader

假设我有一些JSON文件(将其命名为template.json

{
    "myField1": "",
    "myField2": ""
}

我也有一个通用类

export default GenericClass<T> {
    // Creating an empty constuctor with passed type.
    // to allow define type automatically.
    // This allow us not to manually set generic type for class
    // and also allows Webpack to pick up changes.
    constructor(template?: T) {} 

    // ...some fields and methods

    get typedField(): T {
        return /* something slightly calculated */
    }
}

我在Typescript项目中像一种类型一样使用它:

import GenericClass from "path/to/GenericClass"
import template from "template.json"

export type TemplateType = typeof template
export default new GenericClass(template)

// we can also write
//     export default new GenericClass<TemplateType>()
// but in this case the changes in template.json
// won't be picked up by Webpack.
// However, this does not affects the problem,
// it occurs in both cases.

我正在运行webpack开发服务器,并在某处使用它:

import * as React from "react"
import GenericInstance from "path/to/GenericInstance"

export default MyComponent extends React.Component {
    render() {
        var { typedField } = GenericInstance
        return (
            <main>
                <p>{typedField.myField1} {/* ok */}</p>
                <p>{typedField.myField2} {/* ok */}</p>
            </main>
        )
    }
}

此后,我在template.json中添加了一个新字段:

{
    "myField1": "",
    "myField2": "",
    "myField3": ""   
}

保存。 webpack开发服务器在template.json中进行了此更改。行。 重要的一点是VSCode的自动完成功能有效(它在可用字段列表中显示此myField3)。很好

此刻,当我尝试在myField3中使用MyComponent(例如<p>{typedField.myField3}</p>)时,awesome-typescript-loader在编译期间发送错误:

  

属性'myField3'在类型'{不存在,“ myField1”:string; “ myField2”:字符串; }'

很显然,awesome-typescript-loader并没有在template.json中用作我的GenericClass类型的更改。

我该如何打败它?重新启动开发服务器后,它可以正常工作,直到我在template.json中进行更改。

部分webpack.config.jspackage.jsontsconfig.json

config = {
    rules: {
        {
            test: /\.tsx?$/,
            loader: "awesome-typescript-loader",
            exclude: /node_modules/
        },
        {
            enforce: "pre",
            test: /\.js$/,
            loader: "source-map-loader"
        },
    }
}
{
    "devDependencies": {
        "awesome-typescript-loader": "^5.2.1",
        "source-map-loader": "^0.2.4",
        "typescript": "^3.3.3",
        "webpack": "^4.29.3",
        "webpack-cli": "^3.2.3",
        "webpack-dev-server": "^3.1.14"
    }
}
{
    "compilerOptions": {
        "module": "esnext",
        "target": "es5",
        "moduleResolution": "node",
        "baseUrl": "src",
        "allowSyntheticDefaultImports": true,
        "noImplicitAny": true,
        "strict": false,
        "sourceMap": true,
        "outDir": "dist/",
        "jsx": "react",
        "traceResolution": true,
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true,
        "allowJs": true,
        "declaration": false,
        "removeComments": true,
        "noLib": false,
        "preserveConstEnums": true,
        "suppressImplicitAnyIndexErrors": true,
        "types": [ "node" ],
        "lib": ["es6", "dom", "dom.iterable"],
        "downlevelIteration": true,
        "resolveJsonModule": true,
        "typeRoots": [
            "./node_modules/@types"
        ]
    },
    "include": [
        "src/**/*"
    ]
}

更新

我可以确认这仅在导入的* .json中发生。问题可能与TypeScript的resolveJsonModule设置有关,但不确定。在useCache中为usePrecompiledFiles显式设置falseawesome-typescript-loaderwebpack.config.js并没有帮助。我的意思是,更改后的webpack.config.js现在看起来像:

{
    test: /\.(t|j)sx?$/,
    loader: "awesome-typescript-loader",
    options: {
        useCache: false,
        usePrecompiledFiles: false
    },
    exclude: /node_modules\/(?!superagent)/,
},

1 个答案:

答案 0 :(得分:3)

这是awesome-typescript-loader中的错误。从v5.2.1开始,这是一个快速解决方案:

// node_modules/awesome-typescript-loader/dist/instance.js

// ln: 214:
- var EXTENSIONS = /\.tsx?$|\.jsx?$/;
+ var EXTENSIONS = /\.tsx?$|\.jsx?|\.json$/;

显然,作者忘记了将.json扩展名作为有效目标。