我有一个打字稿库,由多个文件夹组成。每个文件夹都包含一个 index.ts 文件,该文件导出一些业务逻辑。我正在尝试将此与汇总捆绑在一起以在呼叫站点上实现此行为:
import { Button, ButtonProps } from 'my-lib/button'
import { Input, Textarea } from 'my-lib/input'
import { Row, Column } from 'my-lib/grid'
这是目录结构:
我有一个主要内容index.ts,src/其中包含:
export * from './button';
export * from './input';
export * from './grid';
使用这种风格,我可以做到:
import { Button, Input, InputProps, Row, Column } from 'my-lib'
但我不想要这个。我想通过它们的命名空间访问每个模块。如果我从文件中删除导出index.ts,我所能做的就是:
import { Button } from 'my-lib/dist/button'
这是我以前没有看到的。添加dist/到导入语句意味着我正在通过相对路径访问模块。我想要my-lib/Button。
我正在使用汇总。我尝试使用alias插件但没有用。以下是我的汇总配置:
const customResolver = resolve({
extensions: ['ts'],
});
export default {
input: `src/index.ts`,
output: [
{
file: pkg.main,
format: 'cjs',
sourcemap: true,
// plugins: [terser()],
},
{
file: pkg.module,
format: 'es',
sourcemap: true,
plugins: [terser()],
},
],
// Indicate here external modules you don't wanna include in your bundle (i.e.: 'lodash')
external: [],
watch: {
include: 'src/**',
},
plugins: [
// Allow json resolution
json(),
// Compile TypeScript files
typescript({ useTsconfigDeclarationDir: true }),
// Allow bundling cjs modules (unlike webpack, rollup doesn't understand cjs)
commonjs(),
// Allow node_modules resolution, so you can use 'external' to control
// which external modules to include in the bundle
// https://github.com/rollup/rollup-plugin-node-resolve#usage
resolve(),
// Resolve source maps to the original source
sourceMaps(),
alias({
entries: [
{ find: 'my-lib/button', replacement: './dist/button' },
{ find: 'my-lib/input', replacement: './dist/input' },
{ find: 'my-lib/grid', replacement: './dist/grid' },
],
customResolver,
}),
],
};
慕尼黑8549860
沧海一幻觉
当年话下
相关分类