rootDir和outDir的意外行为

时间:2016-11-13 21:45:42

标签: typescript react-native

我正在尝试将TypeScript与React Native一起使用。

这是我的tsconfig.json

{
    "compilerOptions": {
        "target": "es2015",
        "module": "es2015",
        "jsx": "react",
        "outDir": "build",
        "rootDir": "src",
        "allowSyntheticDefaultImports": true,
        "noImplicitAny": true,
        "experimentalDecorators": true,
        "preserveConstEnums": true,
        "allowJs": true,
        "sourceMap": true
    },
    "filesGlob": [
        "typings/index.d.ts",
        "src/**/*.ts",
        "src/**/*.tsx"
    ],
    "exclude": [
        "index.android.js",
        "index.ios.js",
        "build",
        "node_modules"
    ],
    "compileOnSave": false
}

我希望将src目录中的所有.ts(x)文件编译到构建目录。

预期:

MyAwesomeReactNativeApp/
├── src/
│   └── index.tsx
└── build/
    └── index.js

得到:

MyAwesomeReactNativeApp/
├── src/
│   └── index.tsx
└── build/
    ├── src/
    │   └── index.js
    ├── index.android.js
    ├── index.ios.js
    └── __tests__/

编译器抱怨:

error TS6059: File '[...]/__tests__/index.android.js' is not under 'rootDir' '[...]/src'. 'rootDir' is expected to contain all source files.
error TS6059: File '[...]/__tests__/index.ios.js' is not under 'rootDir' '[...]/src'. 'rootDir' is expected to contain all source files.
error TS6059: File '[...]/index.android.js' is not under 'rootDir' '[...]/src'. 'rootDir' is expected to contain all source files.
error TS6059: File '[...]/index.ios.js' is not under 'rootDir' '[...]/src'. 'rootDir' is expected to contain all source files.

我在这里做错了什么?

提前谢谢!

2 个答案:

答案 0 :(得分:1)

我偶然发现了同样的问题。我认为当您使用react-native-cli初始化项目并使用react-native run-ios / run-android测试项目时会发生这种情况。然后开始将项目转换为react-native typescript项目。这会在以后创建“正常”的react-native构建工件和typescript构建工件。

对我而言,它有助于将__tests__放入tsconfig.json中的排除。

{
"compilerOptions": {
    "module": "es2015",
    "target": "es2015",
    "jsx": "react",
    "outDir": "build",
    "rootDir": "src",
    "allowSyntheticDefaultImports": true,
    "noImplicitAny": true,
    "experimentalDecorators": true,
    "preserveConstEnums": true,
    "allowJs": true,
    "sourceMap": true
},
"filesGlob": [
    "typings/index.d.ts",
    "src/**/*.ts",
    "src/**/*.tsx"
],
"exclude":[
    "index.android.js",
    "index.ios.js",
    "build",
    "node_modules",
    "__tests__"
],
"compileOnSave": false

}

答案 1 :(得分:0)

对我来说,问题(不是相同的错误消息,而是处理类似的复杂性)是我为几个库使用了符号链接(来自Dim list_ As New List(Of List(Of String)) Dim addition As New List(Of String) addition.Add("aa") addition.Add("bb") addition.Add("cc") list_.Add(addition) list_.Add(addition) addition.Clear() addition.Add("aa") addition.Add("hh") addition.Add("cc") list_.Add(addition) For x = 0 To 2 For y = 0 To 2 Console.Write(list_(x)(y)) Next Console.WriteLine() Next ),并且npm link正在加载某些文件从他们那里。

显然ts-loader可能会对路径和/或tsconfig位置感到困惑,因此认为库B正在从库A“ B的rootDir外部”导入文件,而实际上它并未导入任何内容完全来自库B。

要解决此问题,我限制了ts-loader仅在实际需要加载的确切路径/文件夹下处理文件:

ts-loader