question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`emitDeclarationOnly` in v0.33.0 -- what if I want an empty chunk and only declarations?

See original GitHub issue

Troubleshooting

  1. Does tsc have the same output? If so, please explain why this is incorrect behavior

No

  1. Does your Rollup plugin order match this plugin’s compatibility? If not, please elaborate

Yes

  1. Can you create a minimal example that reproduces this behavior? Preferably, use this environment for your reproduction

https://stackblitz.com/edit/rpt2-repro-1kadjh?file=package.json

What happens and why it is incorrect

The build crashes if emitDeclarationOnly is set to true in overrides or tsconfig. tsc produces the correct output. Used to work until v0.33.0.

Environment

Versions

  System:
    OS: Linux 5.4 Debian GNU/Linux 11 (bullseye) 11 (bullseye)
    CPU: (4) x64 Intel(R) Xeon(R) Platinum 8272CL CPU @ 2.60GHz
    Memory: 5.13 GB / 7.77 GB
    Container: Yes
    Shell: 5.1.4 - /bin/bash
  Binaries:
    Node: 18.4.0 - /usr/local/share/nvm/versions/node/v18.4.0/bin/node
    Yarn: 1.22.19 - /usr/bin/yarn
    npm: 8.18.0 - /usr/local/share/nvm/versions/node/v18.4.0/bin/npm
  npmPackages:
    rollup: 2.78.0 => 2.78.0 
    rollup-plugin-typescript2: 0.33.0 => 0.33.0 
    typescript: 4.7.4 => 4.7.4 

rollup.config.js

:
import typescript from 'rollup-plugin-typescript2';

export default {
  input: './src/index.ts',
  output: {
    file: './dist/index.ts',
    format: 'esm',
    exports: 'named',
  },
  plugins: [
    typescript({
      verbosity: 3,
      clean: true,
    }),
  ],
};

tsconfig.json

:
{
  // https://github.com/agilgur5/tsconfig
  "extends": "@agilgur5/tsconfig/src/tsconfig.library.json",
  // exclude node_modules (the default), dist dir, coverage dir, and example for now
  "exclude": ["node_modules/", "dist/", "coverage/", "example/"],
  // all TS files in the src/ dir
  "include": ["src/**/*"],
  // see https://www.typescriptlang.org/tsconfig to better understand tsconfigs
  "compilerOptions": {
    // output to dist/ dir
    "outDir": "./dist/",
    // match output dir to input dir. e.g. dist/index instead of dist/src/index
    "rootDir": "./src",
    "emitDeclarationOnly": true
  }
}

package.json

:
{
  "name": "rpt2-repro",
  "version": "0.0.0",
  "scripts": {
    "clean": "rm -rf dist/",
    "build": "rollup -c",
    "tsc": "tsc"
  },
  "devDependencies": {
    "@agilgur5/tsconfig": "^0.0.2",
    "rollup": "^2.75.6",
    "rollup-plugin-typescript2": "^0.33.0",
    "typescript": "^4.7.3"
  }
}

plugin output with verbosity 3

:
./src/index.ts → ./dist/index.ts...
rpt2: built-in options overrides: {
    "noEmitHelpers": false,
    "importHelpers": true,
    "noResolve": false,
    "noEmit": false,
    "noEmitOnError": false,
    "inlineSourceMap": false,
    "outDir": "/home/projects/rpt2-repro-1kadjh/node_modules/.cache/rollup-plugin-typescript2/placeholder",
    "moduleResolution": 2,
    "allowNonTsExtensions": true,
    "module": 5
}
rpt2: parsed tsconfig: {
    "options": {
        "strict": true,
        "allowUnusedLabels": false,
        "allowUnreachableCode": false,
        "exactOptionalPropertyTypes": true,
        "noFallthroughCasesInSwitch": true,
        "noImplicitOverride": true,
        "noImplicitReturns": true,
        "noPropertyAccessFromIndexSignature": true,
        "noUncheckedIndexedAccess": true,
        "noUnusedLocals": true,
        "noUnusedParameters": true,
        "importsNotUsedAsValues": 2,
        "checkJs": true,
        "esModuleInterop": true,
        "skipLibCheck": true,
        "forceConsistentCasingInFileNames": true,
        "outDir": "/home/projects/rpt2-repro-1kadjh/node_modules/.cache/rollup-plugin-typescript2/placeholder",
        "sourceMap": true,
        "moduleResolution": 2,
        "resolveJsonModule": true,
        "jsx": 2,
        "noEmit": false,
        "declaration": true,
        "declarationMap": true,
        "rootDir": "/home/projects/rpt2-repro-1kadjh/src",
        "emitDeclarationOnly": true,
        "configFilePath": "/home/projects/rpt2-repro-1kadjh/tsconfig.json",
        "noEmitHelpers": false,
        "importHelpers": true,
        "noResolve": false,
        "noEmitOnError": false,
        "inlineSourceMap": false,
        "allowNonTsExtensions": true,
        "module": 5
    },
    "fileNames": [
        "/home/projects/rpt2-repro-1kadjh/src/index.ts"
    ],
    "typeAcquisition": {
        "enable": false,
        "include": [],
        "exclude": []
    },
    "raw": {
        "extends": "@agilgur5/tsconfig/src/tsconfig.library.json",
        "exclude": [
            "node_modules/",
            "dist/",
            "coverage/",
            "example/"
        ],
        "include": [
            "src/**/*"
        ],
        "compilerOptions": {
            "outDir": "./dist/",
            "rootDir": "./src",
            "emitDeclarationOnly": true
        },
        "compileOnSave": false
    },
    "errors": [],
    "wildcardDirectories": {
        "/home/projects/rpt2-repro-1kadjh/src": 1
    },
    "compileOnSave": false
}
rpt2: typescript version: 4.7.3
rpt2: tslib version: 2.4.0
rpt2: rollup version: 2.75.6
rpt2: rollup-plugin-typescript2 version: 0.33.0
rpt2: plugin options:
{
    "check": true,
    "verbosity": 3,
    "clean": true,
    "cacheRoot": "/home/projects/rpt2-repro-1kadjh/node_modules/.cache/rollup-plugin-typescript2",
    "include": [
        "*.ts+(|x)",
        "**/*.ts+(|x)"
    ],
    "exclude": [
        "*.d.ts",
        "**/*.d.ts"
    ],
    "abortOnError": true,
    "rollupCommonJSResolveHack": false,
    "useTsconfigDeclarationDir": false,
    "tsconfigOverride": {},
    "transformers": [],
    "tsconfigDefaults": {},
    "objectHashIgnoreUnknownHack": false,
    "cwd": "/home/projects/rpt2-repro-1kadjh",
    "typescript": "version 4.7.3"
}
rpt2: rollup config:
{
    "external": [],
    "input": "./src/index.ts",
    "plugins": [
        {
            "name": "rpt2"
        },
        {
            "name": "stdin"
        }
    ],
    "output": [
        {
            "exports": "named",
            "file": "./dist/index.ts",
            "format": "esm",
            "plugins": []
        }
    ]
}
rpt2: tsconfig path: /home/projects/rpt2-repro-1kadjh/tsconfig.json
rpt2: included:
[
    "*.ts+(|x)",
    "**/*.ts+(|x)"
]
rpt2: excluded:
[
    "*.d.ts",
    "**/*.d.ts"
]
rpt2: transpiling '/home/projects/rpt2-repro-1kadjh/src/index.ts'
rpt2: generated declarations for '/home/projects/rpt2-repro-1kadjh/src/index.ts'
rpt2: emitDeclarationOnly enabled, not transforming TS'
[!] (plugin rpt2) Error: Unexpected token (Note that you need plugins to import files that are not JavaScript)
src/index.ts (4:29)
2: // run `npm run tsc` in the terminal to type-check this file with tsc
3: 
4: export default function sum(a: number, b: number) {
                                ^
5:   return a + b;
6: }

    at error (/home/projects/rpt2-repro-1kadjh/node_modules/rollup/dist/shared/rollup.js:198:30)
    at Module.error (/home/projects/rpt2-repro-1kadjh/node_modules/rollup/dist/shared/rollup.js:12553:16)
    at Module.tryParse (/home/projects/rpt2-repro-1kadjh/node_modules/rollup/dist/shared/rollup.js:12930:25)
    at Module.setSource (/home/projects/rpt2-repro-1kadjh/node_modules/rollup/dist/shared/rollup.js:12835:24)
    at ModuleLoader.addModuleSource (/home/projects/rpt2-repro-1kadjh/node_modules/rollup/dist/shared/rollup.js:22309:20)

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
BitPattycommented, Aug 25, 2022

Thanks a lot for your time and the hint regarding the useTsconfigDeclarationDir option. With that configuration it works again (and better) as before 👍

0reactions
agilgur5commented, Aug 25, 2022

In my case I already have the declaration file as output.file, so not sure how/if this would work.

Oh woops, my bad, I meant the input. Point the input to an empty file.

(I happened to have been changing a Rollup config at the same time, so I screwed that up, sorry! Of course, the output.file doesn’t exist yet).

rollup.config.js

Thanks for providing a detailed example, that made it much easier to understand! There are in fact other alternatives to achieve this (which was much easier to see at-a-glance with an example!).

A commonly used one by rpt2 users is to set useTsconfigDeclarationDir: true and declarationDir: './dist/types'. For instance:

// rollup.config.js
import typescript from 'rollup-plugin-typescript2';

export default [{
  input: './src/index.ts',
  output: [{
    file: './dist/esm/index.js',
    format: 'esm',
    exports: 'named',
    sourcemap: true,
  }, {
    file: './dist/cjs/index.js',
    format: 'cjs',
    exports: 'named',
    sourcemap: true,
  }],
  plugins: [
    typescript({
      verbosity: 3,
      clean: true,
      useTsconfigDeclarationDir: true,
      tsconfigOverride: {
        compilerOptions: {
          declarationDir: './dist/types',
        },
      },
    }),
  ],
}];

Note that I’m also using Rollup’s multi-output feature here for ESM + CJS, which should optimize your build a bit too, since Rollup won’t need to analyze the input a second time around or re-run any input plugins.

The caveat with useTsconfigDeclarationDir is that other plugins that process declarations won’t work (it writes directly to the declarationDir, bypassing Rollup’s emit phase), but it doesn’t seem like you need that in your case anyway. And it seems that the vast majority of users are okay with that caveat/trade-off as well, so I think I can safely recommend that option.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using together with Babel plugins and emitDeclarationOnly ...
Using babel to compile files since I need to run code through a plugin: ... empty if typescript plugin is loaded and dist/index.umd.js...
Read more >
How to create R markdown with empty code chunk?
I have found several options to manipulate the html to give more white space. But I would like to present some empty lines...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found