简体   繁体   English

如何使用 Rollup.js 创建一个通用的 js 和命名的导出包?

[英]How to use Rollup.js to create a common js and named export bundle?

I want to generate a single file to be used by an AWS Lambda using Rollup.js.我想使用 Rollup.js 生成一个供 AWS Lambda 使用的文件。 The bundle must:捆绑包必须:

  1. be commonjs;做个普通人;
  2. have named exports;已命名出口;
  3. resolve node packages and put all code in a single file.解析节点包并将所有代码放在一个文件中。

I'm trying to achieve this but without success.我正在努力实现这一目标,但没有成功。 My current configuration doesn't add Day.js code to the final bundle.我当前的配置没有将 Day.js 代码添加到最终包中。

I created a repository with my attempt .我尝试创建了一个存储库

Some Considerations一些注意事项

Rollup and package versions汇总和 package 版本

"@rollup/plugin-commonjs": "^17.0.0",
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^11.0.0",
"@rollup/plugin-typescript": "^8.0.0",
"rollup": "^2.34.2",

Final bundle最终捆绑

The generated bundle can be seen here . 生成的包可以在这里看到 I don't want these imports in the file:我不希望文件中有这些导入:

const tslib_1 = require("tslib");
const dayjs_1 = tslib_1.__importDefault(require("dayjs"));

Instead, I want all the necessary code from tslib and dayjs embedded in the file.相反,我希望将来自tslibdayjs的所有必要代码嵌入到文件中。

tsconfig.json tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs"
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  },
  "include": ["src"]
}

The fact is you set tsc compile your ts files back to commonjs causing the issue.事实是您设置tsc将您的ts文件编译回commonjs导致问题。 This job is supposed to be rollup 's instead.这项工作应该是rollup的。

I would suggest you to leave tsc transpiles back to esnext ( esm which is rollup needs), then you could combine all things with cjs style.我建议你将tsc转译留给esnextesm这是rollup需要),然后你可以将所有东西与cjs风格结合起来。

Here's the thing you might need to change:这是您可能需要更改的内容:

typescript({
  module: 'esnext' // change `commonjs` -> `esnext`
}),

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

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