【问题标题】:WebStorm compiling TypeScript file in-place instead of generating into target folderWebStorm 就地编译 TypeScript 文件而不是生成到目标文件夹中
【发布时间】:2020-05-11 15:57:48
【问题描述】:

我们正在使用带有 TypeScript 项目的 WebStorm。我们已经使用 tsconfig.json 正确配置了项目,因为一切都可以从命令行正常运行。

但是,当在 WebStorm 中打开项目时,它会在适当的位置编译 ts 文件(它在与相应 ts 文件相同的文件夹中生成 js 文件),正如 tsconfig 所说,它应该在 dist 目录中。

例如,在下面的屏幕截图中,evaluator.jsevaluator.ts 文件位于同一文件夹中。知道如何配置 WebStorm 使其不会生成 js 文件吗?

这里是 Typescript 首选项。

这里是 tsconfig.json 文件

{
  "compilerOptions": {
    "allowJs": true,
    "allowSyntheticDefaultImports": true,
    "charset": "utf-8",
    "declaration": true,
    "experimentalDecorators": true,
    "module": "commonjs",
    "moduleResolution": "node",
    "outDir": "dist",
    "pretty": false,
    "rootDir": "src",
    "sourceMap": true,
    "strict": true,
    "stripInternal": true,
    "target": "es2016",
    "typeRoots": ["node_modules/@types"],
    "types": ["node", "jest"],
    "lib": [
      "es2019"
    ]
  },
  "include": ["src/**/*.ts", "src/**/*.js", "src/index.ts"],
  "exclude": ["node_modules"]
}

【问题讨论】:

    标签: typescript webstorm


    【解决方案1】:

    你的package.json需要这样的东西

    ...
      "main": "app/src/index.js",
      "scripts": {
        "copyjson": "copyfiles 'src/**/*.json' app",
        "copyhbs": "copyfiles 'src/**/*.hbs' app",
        "copyhtml": "copyfiles 'src/**/*.html' app",
        "prebuild": "tslint -c tslint.json -p tsconfig.json --fix",
        "build": "tsc && npm run copyjson && npm run copyhbs && npm run copyhtml",
        "prestart": "npm run build",
        "start": "node app/src/index"
      }
    ...
    

    然后做一个

    npm install
    npm run build
    npm run start
    

    【讨论】:

    • 我认为这对我没有帮助,因为命令行对我有用,WebStorm 中的用法才是问题所在。
    • 嗯,在我像上面那样做之前,我遇到了同样的问题。现在它完美地工作了。看看这里的设置:github.com/joergi/learning-typescript/blob/master/package.json(我使用 IntelliJ(这意味着 WebStorm ......)但老实说我在命令行上构建)
    【解决方案2】:

    Apparently Webstorm transpiles typescript files to javascript independently

    确保在“首选项”中禁用“启用 TypeScript 编译器” |语言和框架 | TypeScript,而你没有 在 Preferences | 中设置的 TypeScript 文件观察器工具 |文件观察者

    【讨论】:

    • 我没有任何文件观察器设置,只是发布了我的打字稿首选项。我在首选项中没有看到“启用 Typescript 编译器”选项。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-15
    • 2016-01-17
    • 2017-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多