将Typescript库编译为es2015和CommonJs模块

时间:2018-07-13 15:17:23

标签: javascript typescript webpack

我有一个使用Typescript编译的共享React组件库。目前,我正在使用CommonJs模块编译该库,但我想同时提供ES2015模块,以使Webpack tree shaking在消费者项目中可用。

库文件夹结构如下:

folder structure

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

// Components
export { default as AlertBox } from './components/AlertBox/AlertBox';
export { default as Button } from './components/Button/Button';

我有这个打字稿配置(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.*"]
}

我试图做这样的事情:

tsc && tsc -m es6 --outDir lib-esm

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

我要实现的目标是只有两个不同的index.ts(公共API)。一个带有es6模块(index.es.ts),另一个带有CommonJ(index.ts)。就像material-ui那样。

material-ui folder structure

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

0 个答案:

没有答案