Pedro Arantes
Pedro Arantes

Reputation: 5379

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. 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.

I created a repository with my attempt.

Some Considerations

Rollup and package versions

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

tsconfig.json

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

Upvotes: 1

Views: 1482

Answers (1)

tmhao2005
tmhao2005

Reputation: 17474

The fact is you set tsc compile your ts files back to commonjs causing the issue. This job is supposed to be rollup's instead.

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.

Here's the thing you might need to change:

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

Upvotes: 1

Related Questions