每个组件的汇总+ Typescript输出JS文件

时间:2020-02-28 09:12:50

标签: typescript rollupjs

我正在使用React,TypeScript和Rollup创建一个可重用的组件库。

将此库捆绑到单个输出文件index.js中效果很好。但是,没有为我的单个组件生成任何.js输出文件。

但是,我试图确保可以在TypeScript项目中使用特定的导入。

例如:import { ComponentA } from "my-components/lib/ComponentA;

这样可以确保以后捆绑项目时不包括我的其他组件。

所以我的问题是:我如何配置汇总,以便将所有组件也输出到单独的组件输出文件中?

我的文件结构如下:

src
│ index.ts
└─components
  │ index.ts
  ├─ComponentA
  │   index.ts
  │   ComponentA.tsx
  └─ComponentB
      index.ts
      ComponentB.tsx

我的汇总配置为:

import commonjs from "@rollup/plugin-commonjs";
import resolve from "@rollup/plugin-node-resolve";
import external from "rollup-plugin-peer-deps-external";
import typescript from "rollup-plugin-typescript2";

export default {
  input: "src/index.ts",
  output: [
    {
      file: pkg.main,
      format: "cjs",
      exports: "named",
      sourcemap: true
    },
    {
      file: pkg.module,
      format: "es",
      exports: "named",
      sourcemap: true
    }
  ],
  plugins: [
    external(),
    resolve({
      browser: true
    }),
    typescript({
      rollupCommonJSResolveHack: true,
      exclude: "**/__tests__/**",
      clean: true
    }),
    commonjs({
      include: ["node_modules/**"],
      exclude: ["**/*.stories.js"],
      namedExports: {
        "node_modules/react/react.js": [
          "Children",
          "Component",
          "PropTypes",
          "createElement"
        ],
        "node_modules/react-dom/index.js": ["render"]
      }
    })
  ]
};

哪个输出到:

lib
│ index.d.ts
│ index.es.js
│ index.es.js.map
│ index.js
│ index.js.map
└─components
  │ index.d.ts
  ├─ComponentA
  │   index.d.ts
  │   ComponentA.d.ts
  └─ComponentB
      index.d.ts
      ComponentB.d.ts

我要实现的目标是,在 output (库)文件夹中,例如还有ComponentA.jsComponentB.js文件。 / p>

1 个答案:

答案 0 :(得分:1)

尝试添加标志export default { ... preserveModules: true, ... }

guild.id

它应该可以工作,为此是一个示例性回购协议:https://github.com/daleyjem/rollup-preserve-modules