简体   繁体   English

支持汇总中的子路径导入

[英]supporting subpath imports in rollup

Hello after spending hours and hours reading many articles (especially this one https://medium.com/singapore-gds/how-to-support-subpath-imports-using-react-rollup-typescript-1d3d331f821b ) I got my subpath imports to work(at least I think).你好,花了几个小时阅读了很多文章(尤其是这篇https://medium.com/singapore-gds/how-to-support-subpath-imports-using-react-rollup-typescript-1d3d331f821b )我得到了我的子路径导入工作(至少我认为)。 However, I am generating an extra subdirectory.但是,我正在生成一个额外的子目录。 Here is what I am talking about.这就是我所说的。 This is my current code.这是我当前的代码。

src/index.ts
import * from "./Button"


src/Button/index.ts
export { default as Button1 } from "./Button1"
... other exports ...


src/Button/Button1.ts
export default Button1

My goal is not to import the whole library, but single components (just like when you use material-ui or some other lib) through the syntax import Button 1 from @lib/Button"我的目标不是导入整个库,而是导入单个组件(就像您使用 material-ui 或其他一些库时一样)通过语法import Button 1 from @lib/Button"

However after using rollup I get one extra Button directory但是在使用 rollup 之后我得到了一个额外的 Button 目录

dist/esm/Button:
Button            CustomButton.d.ts index.js          package.json
Button.d.ts       index.d.ts        index.js.map

with the following content具有以下内容

dist/esm/Button/Button:
Button.d.ts       CustomButton.d.ts index.d.ts

I have absolutely no idea why there is one extra directory containing the type declarations.我完全不知道为什么会有一个包含类型声明的额外目录。 I believe that my error lies in my tsconfig.我相信我的错误在于我的 tsconfig. Could you have a look at the tsconfig.json and rollup.config.js to spot the bug?你能看看 tsconfig.json 和 rollup.config.js 来发现错误吗?

tsconfig.json
{
  "compilerOptions": {
    "target": "es5",
    "module": "ESNext",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true,
    "noImplicitAny": true,
    "jsx": "react",
    "noImplicitReturns": true,
    "exactOptionalPropertyTypes": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,

    "rootDir": "src",
    "declaration": true,
    "declarationDir": "dist",
    "sourceMap": true,
    "moduleResolution": "node",
    "allowSyntheticDefaultImports": true,
    "emitDeclarationOnly": true
  },
    "include": ["src/**/*"],
    "exclude": [
        "node_modules",
        "build"
    ]
}
rollup.config.json
export default [
    {
        input: "src/index.ts",
        output: [
            {
                file: packageJson.main,
                sourcemap: true,
                format: "cjs",
            },
            {
                file: packageJson.module,
                sourcemap: true,
                format: "esm",
            },
        ],
        plugins: plugins
    },
    {
        input: "dist/esm/index.d.ts",
        output: [{ file: "dist/index.d.ts", format: "esm" }],
        plugins: [dts()],
        external: [/\.css$/, /\.scss$/, /\.sass$/]
    },
    ...folderBuilds
]


const subfolderPlugins = (folderName) => [
    ...plugins,
    generatePackageJson({
        baseContents: {
            name: `${packageJson.name}/${folderName}`,
            private: true,
            main: "../cjs/index.js",
            module: "../esm/index.js",
            types: "./index.d.ts"
        }
    })
]

const folderBuilds = getFolders("./src").map(folder => (
    {
        input: `src/${folder}/index.ts`,
        output: {
          file: `dist/esm/${folder}/index.js`,
          sourcemap: true,
          format: 'esm',
        },
        plugins: subfolderPlugins(folder),
    }
));

The medium article mentioned above used the plugin "rollup-plugin-typescript2", and config the plugin with上面提到的媒体文章使用了插件“rollup-plugin-typescript2”,并配置了插件

  typescript({
    tsconfig: './tsconfig.json',
    useTsconfigDeclarationDir: true,
  }),

Then there will no extra subdirectory after build然后构建后不会有额外的子目录

you can view the full config here: https://github.com/GovTechSG/sgds-govtech-react/blob/v2/rollup.config.js你可以在这里查看完整的配置: https://github.com/GovTechSG/sgds-govtech-react/blob/v2/rollup.config.js

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM