我正在使用TypeScript和Rollup编写Bluetooth library for Node.js。我想使用户能够通过这些方式导入我的库组件
import Sblendid from "@sblendid/sblendid";
import Sblendid, { Peripheral } from "@sblendid/sblendid";
const Sblendid = require("@sblendid/sblendid");
const { Peripheral } = require("@sblendid/sblendid");
我的项目结构如下:
root
∟ rollup.config.ts
∟ src
∟ index.ts
∟ sblendid.ts
∟ peripheral.ts
相应的代码是这样的:
index.ts
export {
default,
} from "./sblendid";
export {
default as Peripheral,
} from "./peripheral";
sblendid.ts
export default class Sblendid {}
peripheral.ts
export default class Peripheral {}
我将所有内容与汇总捆绑在一起,我的整个配置是这样:
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()
]
};
您可以在此处找到完整的代码
https://github.com/LukasBombach/sblendid/tree/master/packages/sblendid
现在,此设置不起作用。汇总告诉我
$ 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
是真的。这个
const Sblendid = require("@sblendid/sblendid");
根本不起作用。我要做的是这样:
const Sblendid = require("@sblendid/sblendid").default;
我可以通过 not 混合命名的广告默认导出来解决此问题,好的,但是我失去了执行此操作的能力:
import Sblendid, { Peripheral } from "@sblendid/sblendid";
所以我想知道。有没有任何方式,也许使用多个捆绑包,我可以实现让用户能够同时做到这两种方式:
// This
import Sblendid from "@sblendid/sblendid";
import Sblendid, { Peripheral } from "@sblendid/sblendid";
// And this
const Sblendid = require("@sblendid/sblendid");
const { Peripheral } = require("@sblendid/sblendid");
答案 0 :(得分:0)
如果您仅针对nodejs环境,则可以像这样(index.ts)导出
import Sblendid from "./sblendid";
import Peripheral from "./peripheral";
Sblendid.Peripheral = Peripheral;
export default Sblendid;