rollup-plugin-typescript2 icon indicating copy to clipboard operation
rollup-plugin-typescript2 copied to clipboard

`preserveModules` creates a `_tslib.js` and `_tslib.js.map` files in `_virtual` folder

Open cyclops24 opened this issue 5 years ago • 3 comments

What happens and why it is wrong

The build process creates two files for me like this:

build/_virtual/_tslib.js
build/_virtual/_tslib.js.map

How I can remove this? Why we need these files?

Environment

Versions

  • typescript: 3.7.2
  • rollup: 1.27.4
  • rollup-plugin-typescript2: 0.27.0

rollup.config.js

// ...
typescript({ useTsconfigDeclarationDir: true }),
// ...

tsconfig.json

{
  "compilerOptions": {
    "rootDir": "src",
    "declaration": true,
    "declarationDir": "build",
    "module": "esnext",
    "target": "es5",
    "lib": ["es6", "dom", "es2016", "es2017"],
    "sourceMap": true,
    "jsx": "react",
    "moduleResolution": "node",
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "experimentalDecorators": true,
    "resolveJsonModule": true
  },
  "include": ["src/**/*", "types/images.d.ts"],
  "exclude": [
    "node_modules",
    "build",
    "storybook-static",
    "src/**/stories.tsx",
    "src/**/*.stories.tsx",
    "src/**/*.test.tsx"
  ]
}

package.json

plugin output with verbosity 3

cyclops24 avatar Aug 14 '20 07:08 cyclops24

Maybe related to this: https://github.com/ezolenko/rollup-plugin-typescript2/pull/170

cyclops24 avatar Aug 14 '20 08:08 cyclops24

Per #170 it sounds like this only happens with preserveModules, but you didn't include a full Rollup config in order to confirm that. It also sounds like it may be necessary per that PR as rpt2 uses tslib, but I'm not 100% sure.

I'm also curious if this still happens with Rollup v2

agilgur5 avatar Apr 26 '22 22:04 agilgur5

Seems like #282 reproduced this

agilgur5 avatar Apr 26 '22 23:04 agilgur5

I created a reproduction for this issue here. Per my comments there, the _virtual dir will only be created if you're using preserveModules and are using tslib helpers (e.g. you're using syntax that isn't natively supported by the target you've specified in your tsconfig.json, similar to @babel/runtime. I specifically used async while targeting ES2015 in the repro).

This is necessary for how preserveModules works in Rollup, as "all exports are rewritten in a way that does not depend on Node's dependency resolution algorithm". tslib is resolved as a virtual module in rpt2 so that it can be used without knowledge of Node's resolution algorithm (i.e. without @rollup/plugin-node-resolve).

If you don't want this _virtual dir with preserveModules and don't want tslib helpers in your output, you can either:

  1. target a higher version of ECMAScript OR
  2. don't use unsupported syntax OR
  3. add @rollup/plugin-node-resolve which will resolve tslib through node_modules
    • this option is commented out in the repro above, for reference

agilgur5 avatar Sep 17 '22 16:09 agilgur5