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

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

我想使用 Rollup.js 生成一个文件供 AWS Lambda 使用。捆绑包必须:

  1. 成为普通人;
  2. 已命名导出;
  3. 解析节点包并将所有代码放在一个文件中。

我正在努力实现这一目标,但没有成功。我当前的配置没有将 Day.js 代码添加到最终包中。

I created a repository with my attempt.

一些注意事项

汇总和打包版本

"@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",

最终捆绑包

The generated bundle can be seen here。我不想在文件中导入这些文件:

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

相反,我希望将 tslibdayjs 中的所有必要代码嵌入到文件中。

tsconfig.json

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

事实是您设置 tscts 文件编译回 commonjs 导致了问题。这项工作应该是 rollup 的。

我建议您将 tsc 转换回 esnextesm 这是 rollup 的需要),然后您可以将所有东西与 cjs风格。

以下是您可能需要更改的内容:

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