将 typescript lib 编译为 es2015 和 CommonJs 模块

Ger*_*ull 6 javascript typescript webpack

我有一个用打字稿编译的共享 React 组件库。目前我正在使用 CommonJs 模块编译库,但我也希望 ES2015 模块可用,以便在消费者项目中使用Webpack 树摇动

库文件夹结构如下所示:

文件夹结构

index.ts文件是我拥有公共 API 的地方。一个例子是:

// Components
export { default as AlertBox } from './components/AlertBox/AlertBox';
export { default as Button } from './components/Button/Button';
Run Code Online (Sandbox Code Playgroud)

我有这个打字稿配置(tsconfig.json):

{
  "compilerOptions": {
    "baseUrl": ".",
    "module": "commonjs",
    "moduleResolution": "node",
    "target": "es5",
    "noImplicitAny": false,
    "sourceMap": true,
    "outDir": "./dist",
    "jsx": "react",
    "allowSyntheticDefaultImports": true,
    "declaration": true,
    "paths": {
      "*": ["public/*", "src/*"]
    }
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules", "**/*.test.*"]
}
Run Code Online (Sandbox Code Playgroud)

我试图做这样的事情:

tsc && tsc -m es6 --outDir lib-esm
Run Code Online (Sandbox Code Playgroud)

但这会将所有项目编译为 es6 模块,因此我最终会复制所有代码。

我想要实现的是只有 2 个不同的index.ts(公共 API)。一个带有 es6 模块 ( index.es.ts),另一个带有 CommonJs ( index.ts)。不知何故,就像material-ui正在做的那样。

material-ui 文件夹结构

有没有办法只使用打字稿获取这些文件?