我有一个基于Vite的React库,目前 struct 如下:

import { Button, Typography, Box, Flex, Color, TypographyVariant } from '@placeholder-library';

我想分开导入以添加子模块,这样组件和共享就可以从不同的路径导入:

import { Button, Typography, Box, Flex } from '@placeholder-library/components’;
import { Color, TypographyVariant } from ‘@placeholder-library/shared’;

index.ts

import './index.scss';
export * from './components';
export * from './shared';

vite.config.ts:

import react from '@vitejs/plugin-react';
import path from 'path';
import { defineConfig } from 'vite';
import dts from 'vite-plugin-dts';
import svgr from 'vite-plugin-svgr';
import tsconfigPaths from 'vite-tsconfig-paths';
import commonjs from 'vite-plugin-commonjs';

export default defineConfig({
  resolve: {
    alias: {
      src: path.resolve(__dirname, './src'),
    },
  },
  build: {
    outDir: 'build',
    lib: {
      entry: './src/index.ts',
      name: 'Placeholder Library',
      fileName: 'index',
    },
    rollupOptions: {
      external: ['react', 'react-dom'],
      output: [
        {
          globals: {
            react: 'React',
            'react-dom': 'ReactDOM',
          },
        },
        {
          dir: 'build/cjs',
          format: 'cjs',
          globals: {
            react: 'React',
            'react-dom': 'ReactDOM',
          },
        },
        {
          dir: 'build/esm',
          format: 'esm',
          globals: {
            react: 'React',
            'react-dom': 'ReactDOM',
          },
        },
      ],
    },
    sourcemap: true,
    emptyOutDir: true,
  },
  plugins: [
    svgr(),
    react(),
    commonjs(),
    tsconfigPaths(),
    dts({
      outDir: ['build/cjs', 'build/esm', 'build'],
      include: ['./src/**/*'],
      exclude: ['**/*.stories.*'],
    }),
  ],
});

package.json:

{
  "name": "@placeholder-library",
  "version": "0.0.26",
  "description": "Placeholder Library components library",
  "license": "ISC",
  "main": "build/cjs/index.js",
  "module": "build/index.mjs",
  "files": ["*"],
  "scripts": {
    "build": "tsc && vite build",
    "build-storybook": "storybook build",
    "build-storybook-docs": "storybook build --docs",
    "dev": "vite",
    "format": "prettier --write .",
    "lint:fix": "eslint . --fix --ignore-path .gitignore",
    "prepare": "husky install",
    "preview": "vite preview",
    "storybook": "storybook dev -p 6006",
    "storybook-docs": "storybook dev --docs"
  },
  "dependencies": {
    "..."
  },
  "devDependencies": {
    "..."
  },
  "peerDependencies": {
    "react": "^18.2.0"
  }
}

我的文件夹 struct : 我的文件夹 struct

src
    index.ts
    components
        index.ts
        Button
            index.ts
    shared
        hooks
        index.ts

组件中的index. ts:

export * from './Button'

如何配置Vite和我的包 struct 来实现组件和共享/utils的分离?任何建议或例子将不胜感激.

我试图修改100文件,以包含组件和shared/utils的单独条目,但我不知道如何正确配置路径.我还try 调整101文件,以便为组件和shared/utils指定不同的入口点,但我不确定如何正确地构造它.

import { Button, Typography, Box, Flex } from '@placeholder-library/components’;

import { Color, TypographyVariant } from ‘@placeholder-library/shared’;

然而,我找不到一个清晰的例子或文档来说明如何在基于Vite的React库中设置这个功能.如能提供关于如何实现这一点的任何指导或实例,将不胜感激.

推荐答案

我认为演示会比来回走容易:https://github.com/quyentho/submodule-demo

您可以在placeholder-lib中判断我的dist/文件夹,查看生成的构建是否具有类似的 struct .我在我的consumer中导入没有问题:

import { Button } from "placeholder-lib/components";
import useMyHook from "placeholder-lib/shared";

我想,你的问题可能是package.json年后错过了这些出口线

  "exports": {
    ".": "./dist/index.js",
    "./components": "./dist/components/index.js",
    "./shared": "./dist/shared/index.js"
  },

Javascript相关问答推荐

Phaser框架-将子对象附加到Actor

未捕获错误:在注销后重定向到/login页面时找不到匹配的路由

有没有可能使滑动img动画以更快的速度连续?

setcallback是什么时候放到macrotask队列上的?

获取Uint8ClampedArray中像素数组的宽度/高度

如何从网站www.example.com获取表与Cheerio谷歌应用程序脚本

WhatsApp Cloud API上载问题:由于MIME类型不正确而导致接收&Quot;INVALID_REQUEST";错误

如何将数据块添加到d3力有向图中?

为什么这个.add.group({})在教程中运行得很好,但在我的游戏中就不行了?

TinyMCE 6导致Data:Image对象通过提供的脚本过度上载

获取';无法解决导入和导入";slick-carousel/slick/slick-theme.css";';错误

Phaser3 preFX addGlow不支持zoom

当我点击一个按钮后按回车键时,如何阻止它再次被点击

处理TypeScrip Vue组件未初始化的react 对象

是否设置以JavaScript为背景的画布元素?

Chart.js Hover线条在鼠标离开时不会消失

在Puppeteer中使用promise进行日志(log)记录时出现TargetCloseError

找不到处于状态的联系人

如何按区域进行过滤并将其从结果数组中删除?

MUI-TABLE:MUI表组件中交替行的不同 colored颜色 不起作用