Running pnpm install && pnpm build
on the simple project below is throwing warning
"this" has been rewritten to "undefined"
Investigating a bit it seems that the code trying to be compiled is coming from tslib. The compiler doesn't know what value this
is, so it replaces it with undefined
. I'm trying to create a library here that should be run in browser and nodejs. I don't know how to fix this.
I can reproduce it with these:
- nodejs: v18.16.0
- pnpm: 8.5.1
- TypeScript: 5.0.4
- rollup: v3.22.0
package.json
{
"name": "rollup-test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "rollup -c rollup.config.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"fp-ts": "^2.15.0"
},
"devDependencies": {
"@rollup/plugin-node-resolve": "^15.0.2",
"@rollup/plugin-typescript": "^11.1.1",
"tslib": "^2.5.2"
}
}
rollup.config.js
const typescript = require('@rollup/plugin-typescript');
const { nodeResolve } = require('@rollup/plugin-node-resolve');
module.exports = [
{
input: 'src/index.ts',
output: {
file: 'dist/index.cjs.js',
format: 'cjs'
},
plugins: [
nodeResolve(),
typescript()
]
}
];
src/index.ts
import * as E from 'fp-ts/Either'
function mylog(str: string) {
const x = E.right(str)
if (E.isLeft(x)) {
console.log(x.left)
} else {
console.log(x.right)
}
}
mylog('hi')
Running pnpm build
outputs:
> rollup-test@1.0.0 build /Users/theuser/Desktop/rollup-test
> rollup -c rollup.config.js
src/index.ts → dist/index.cjs.js...
(!) "this" has been rewritten to "undefined"
https://rollupjs.org/troubleshooting/#error-this-is-undefined
node_modules/.pnpm/fp-ts@2.15.0/node_modules/fp-ts/es6/function.js
1: var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
^
2: if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
3: if (ar || !(i in from)) {
...and 1 other occurrence
node_modules/.pnpm/fp-ts@2.15.0/node_modules/fp-ts/es6/internal.js
1: var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
^
2: if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
3: if (ar || !(i in from)) {
...and 1 other occurrence
created dist/index.cjs.js in 369ms