Webpack 5: Ändra katalog för TypeScript-deklarationsfiler


Mitt projekt är strukturerat så här (src/ts-mappen är viktig):

- 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

Problemet:

Jag vill att Webpack ska skapa deklarationsfiler. Utdatakatalogen ska vara dist/types. Men när jag kör min webpack-konfiguration hamnar deklarationerna i en dist/src/ts/-katalog.

Min uppställning:

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",
    }
}

ts-loader i min webpack-konfigurationsfil.

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

Svar #1

Försök att lägga till alternativet DeclarationDir i tsconfig.

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

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

Relaterade artiklar