构建打字稿库

Building a typescript library

我正在使用 Typescript 和 React 创建一个新库。

但是我的 tsconfig 有问题,因为当我查看 dist 目录时,只有打字稿定义, 没有 代码 javascript, 所有代码已删除!

我的tsconfig.json

{
  "compilerOptions": {
    "target": "es6",
    "module": "es6",
    "lib": ["es6", "dom"],
    "allowJs": true,
    "jsx": "react",
    "declaration": true,
    "emitDeclarationOnly": true,
    "declarationMap": true,
    "outDir": "dist",
    "rootDir": "lib",
    "strict": true,
    "moduleResolution": "node",
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  },
  "exclude": ["lib/**/*.spec.tsx", "lib/**/*.stories.tsx", "lib/**/*.styles.tsx", "lib/settings/*.ts", "dist/**/*"]
}

这是输出文件的示例(在 dist 目录中)

Button.d.ts

import React from 'react';
export declare class ButtonProps {
    label: string;
    buttonStyle: 'default' | 'primary';
    disabled: boolean;
    iconLeft: string;
    iconRight: string;
}
declare const Button: React.FunctionComponent<ButtonProps>;
export default Button;
//# sourceMappingURL=Button.d.ts.map

并且其他任何文件中都没有代码!

我做错了什么?所有代码都在构建中(使用 tsc 构建),只有类型定义。

解决了将 tsconfig 更改为

{
  "exclude": ["lib/**/*.spec.tsx", "lib/**/*.stories.tsx", "lib/**/*.styles.tsx", "dist/**/*"],
  "compilerOptions": {
    "skipLibCheck": true,
    "target": "es6",
    "module": "es6",
    "lib": ["es6", "dom"],
    "jsx": "react",
    "declaration": true,
    "declarationMap": true,
    "sourceMap": true,
    "outDir": "dist",
    "rootDirs": ["lib"],
    "strict": true,
    "moduleResolution": "node",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true
  }
}