rollup使用报告接口和枚举作为typescript



我自己创建了一个库(例如:library- a),其中包括一个接口,枚举和常量列表,并作为tar文件发布

我还创建了另一个react库(例如:library- b),它使用(library- a)并使用rollup来构建一个dist文件夹。

然而,当我在我的主项目(project- a)中安装这个react库(library- b)时,我似乎无法访问(library- a)的接口,枚举和常量。是否有任何方法可以使用(library-A)中由(library-B)重新导出的接口,枚举和常量

图书馆

export interface PERSON {
name: string;
age: number;
}
export enum GENDER {
male= 'male',
female= 'female'
}

图书馆→rollup.config.js

import commonjs from '@rollup/plugin-commonjs';
import resolve from '@rollup/plugin-node-resolve';
import peerDepsExternal from 'rollup-plugin-peer-deps-external';
import postcss from 'rollup-plugin-postcss';
import typescript from 'rollup-plugin-typescript2';
import packageJson from './package.json';
const config = {
input: 'src/index.ts',
output: [
{
file: packageJson.main,
format: 'cjs',
sourcemap: true,
},
{
file: packageJson.module,
format: 'esm',
sourcemap: true,
},
],
plugins: [
peerDepsExternal(),
resolve(),
commonjs(),
typescript({
tsconfig: 'tsconfig-rollup.json',
}),
postcss(),
],
};
export default config;

图书馆→index.ts

export { GENDER } from 'library-A'; // enum
export type { PERSON, CLASSES } from 'library-A'; // interfaces

—→home.ts

import { GENDER } from "library-B" 
GENDER.male // this is not found

如果我理解正确,您希望在lib-B中访问lib-A中的某些内容,反之亦然。我不确定这是否可能,但在我看来这不是个好主意。

我建议,如果你想在两个项目中共享常量和接口,把它们放在lib-C中,并把这个lib-C包含在lib-A和lib-B中。

最新更新