typescript Webpack 5:更改打字脚本声明文件的目录

inkz8wg9  于 2023-01-06  发布在  TypeScript
关注(0)|答案(1)|浏览(146)

我的项目结构如下(src/ts文件夹很重要):

- dist/
    - js/
    - css/
    - index.html
    - about.html
- src/
    - assets/
        - fonts/
        - images/
    - sass/
    - ts/
        - services/
            - service1.ts
            - service2.ts
        - utils/
            - util1.ts
            - util2.ts
        - index.ts
        - about.ts

问题:

我希望Webpack创建声明文件。输出目录应该是dist/types。但是当我运行我的Webpack配置时,声明被扔到了dist/src/ts/目录中。

我的设置:

tsconfig.json

{
    "compilerOptions": {
        "outDir": "./dist/",
        "sourceMap": true,
        "strict": true,
        "noUnusedLocals": true,
        "noUnusedParameters": true,
        "noImplicitAny": true,
        "removeComments": true,
        "declaration": true, // Creates declaration files .d.ts
        "strictNullChecks": true,
        "module": "es6",
        "target": "es5",
        "jsx": "react",
        "allowJs": true,
        "moduleResolution": "node",
    }
}

我的webpack配置文件中的ts-loader

module: {
        rules: [
            {
                test: /\.tsx?$/,
                use: 'ts-loader',
                exclude: /node_modules/,
            },
        ],
    },
pvabu6sv

pvabu6sv1#

尝试将DeclarationDir选项添加到tsconfig。

// tsconfig.json
{
...
  "declaration": true,
  "declarationDir": "./dist/types"
...
}

https://www.typescriptlang.org/tsconfig#declarationDir

相关问题