简体   繁体   English

使用 Rollup 混合默认导出和命名导出

[英]Mixing default and named exports with Rollup

I am writing a Bluetooth library for Node.js using TypeScript and Rollup.我正在使用 TypeScript 和 Rollup为 Node.js 编写一个蓝牙库 I want to enable users to import my libraries components in these ways我想让用户以这些方式导入我的库组件

import Sblendid from "@sblendid/sblendid";
import Sblendid, { Peripheral } from "@sblendid/sblendid";

const Sblendid = require("@sblendid/sblendid");
const { Peripheral } = require("@sblendid/sblendid");

My project structure looks like this:我的项目结构如下所示:

root
 ∟ rollup.config.ts
 ∟ src
    ∟ index.ts
    ∟ sblendid.ts
    ∟ peripheral.ts

And the according code is this:而相应的代码是这样的:

index.ts

export {
  default,
} from "./sblendid";

export {
  default as Peripheral,
} from "./peripheral";

sblendid.ts

export default class Sblendid {}

peripheral.ts

export default class Peripheral {}

I am bundling everything with Rollup and my entire config is this:我将所有内容与 Rollup 捆绑在一起,我的整个配置是这样的:

import typescript from "typescript";
import commonjs from "rollup-plugin-commonjs";
import resolve from "rollup-plugin-node-resolve";
import typescriptPlugin from "rollup-plugin-typescript2";
import autoExternal from "rollup-plugin-auto-external";
import { terser } from "rollup-plugin-terser";
import pkg from "./package.json";

export default {
  input: "src/index.ts",
  output: [
    {
      file: pkg.main,
      format: "cjs",
      sourcemap: true
    },
    {
      file: pkg.module,
      format: "es",
      sourcemap: true
    }
  ],
  plugins: [
    autoExternal(),
    resolve({ preferBuiltins: true }),
    commonjs(),
    typescriptPlugin({ typescript, objectHashIgnoreUnknownHack: true }),
    terser()
  ]
};

You can find the entire code here你可以在这里找到完整的代码

https://github.com/LukasBombach/sblendid/tree/master/packages/sblendid https://github.com/LukasBombach/sblendid/tree/master/packages/sblendid

Now, this setup does not work.现在,此设置不起作用。 Rollup tells me汇总告诉我

$ rollup -c rollup.config.ts src/index.ts → dist/index.cjs.js, dist/index.es.js... (,) Mixing named and default exports Consumers of your bundle will have to use bundle['default'] to access the default export. which may not be what you want. Use `output:exports: 'named'` to disable this warning

which is true.这是真的。 This这个

const Sblendid = require("@sblendid/sblendid");

simply does not work.根本行不通。 What I have to do is this:我要做的是:

const Sblendid = require("@sblendid/sblendid").default;

I can fix this behavior by not mixing named ad default exports, ok, but then I lose the ability to do this:我可以通过混合命名广告默认导出来修复此行为,好的,但是我失去了执行此操作的能力:

import Sblendid, { Peripheral } from "@sblendid/sblendid";

So I am wondering.所以我想知道。 Is there any way, maybe using multiple bundles, I can achieve having users be able to do both:什么办法,也许使用多个包,我可以实现让用户能够做到这两点:

// This
import Sblendid from "@sblendid/sblendid";
import Sblendid, { Peripheral } from "@sblendid/sblendid";

// And this
const Sblendid = require("@sblendid/sblendid");
const { Peripheral } = require("@sblendid/sblendid");

If you target only nodejs environment you can export like this (index.ts)如果您只针对 nodejs 环境,您可以像这样导出(index.ts)

import Sblendid from "./sblendid";
import Peripheral from "./peripheral";

Sblendid.Peripheral = Peripheral;
export default Sblendid;

Commonjs does not have the concept of default export. Commonjs 没有默认导出的概念。 When you are able do:当你能做的时候:

const Splendid = require("@sblendid/sblendid");
const { Peripheral } = require("@sblendid/sblendid");

It does mean that这确实意味着

assert.equal(Splendid.Peripheral, Peripheral);

That Peripheral is a property of Splendid .PeripheralSplendid的财产。

This is basically achieved by这基本上是通过

Splendid.Peripheral = /* something */;
module.exports = Splendid;

When cjs code is transpiled from esm code (what rollup does) the only choice is to introduce a default property on the exports object.当从 esm 代码转译 cjs 代码时(rollup 所做的),唯一的选择是在exports object 上引入default属性。

If you're not comfortable with adding properties just for the sake of exporting, add a snipped like this to your docs.如果您不习惯仅仅为了导出而添加属性,请在您的文档中添加这样的片段。

const { default: Splendid, Peripheral } = require('...');

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

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