rollup-plugin-typescript2: rpt2 not ignoring `*.d.cts` and `*.d.mts` files (resulting in `Unexpected token` error)

Troubleshooting

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

    N/A

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

    yes

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

    https://stackblitz.com/edit/rpt2-repro-3j5vny?file=src%2Findex.ts,package.json,src%2Flib%2Findex.ts,README.md,tsconfig.json

What happens and why it is incorrect

Error is thrown, should have support for *.cts/*.mts files

[!] (plugin rpt2) RollupError: Unexpected token (Note that you need plugins to import files that are not JavaScript)
../../node_modules/helmet/index.d.cts (3:5)
1: import {IncomingMessage, ServerResponse} from "http"
2: 
3: type ContentSecurityPolicyDirectiveValueFunction = (req: IncomingMessage, res: ServerResponse) => string
        ^
4: type ContentSecurityPolicyDirectiveValue = string | ContentSecurityPolicyDirectiveValueFunction
5: interface ContentSecurityPolicyOptions {

    at error (/home/alexander/Workspace/company/internal/devplatform-server-api/node_modules/rollup/dist/shared/rollup.js:279:30)
    at Module.error (/home/alexander/Workspace/company/internal/devplatform-server-api/node_modules/rollup/dist/shared/rollup.js:13947:16)
    at Module.tryParse (/home/alexander/Workspace/company/internal/devplatform-server-api/node_modules/rollup/dist/shared/rollup.js:14658:25)
    at Module.setSource (/home/alexander/Workspace/company/internal/devplatform-server-api/node_modules/rollup/dist/shared/rollup.js:14260:39)
    at ModuleLoader.addModuleSource (/home/alexander/Workspace/company/internal/devplatform-server-api/node_modules/rollup/dist/shared/rollup.js:24225:20)
[!] (plugin rpt2) RollupError: Unexpected token (Note that you need plugins to import files that are not JavaScript)
../../node_modules/helmet/index.d.mts (3:5)
1: import {IncomingMessage, ServerResponse} from "http"
2: 
3: type ContentSecurityPolicyDirectiveValueFunction = (req: IncomingMessage, res: ServerResponse) => string
        ^
4: type ContentSecurityPolicyDirectiveValue = string | ContentSecurityPolicyDirectiveValueFunction
5: interface ContentSecurityPolicyOptions {

    at error (/home/alexander/Workspace/company/internal/devplatform-server-api/node_modules/rollup/dist/shared/rollup.js:279:30)
    at Module.error (/home/alexander/Workspace/company/internal/devplatform-server-api/node_modules/rollup/dist/shared/rollup.js:13947:16)
    at Module.tryParse (/home/alexander/Workspace/company/internal/devplatform-server-api/node_modules/rollup/dist/shared/rollup.js:14668:25)
    at Module.setSource (/home/alexander/Workspace/company/internal/devplatform-server-api/node_modules/rollup/dist/shared/rollup.js:14270:39)
    at ModuleLoader.addModuleSource (/home/alexander/Workspace/company/internal/devplatform-server-api/node_modules/rollup/dist/shared/rollup.js:24266:20)

Environment

Ubuntu 22.04

Versions

  System:
    OS: Linux 5.17 Ubuntu 22.04.2 LTS 22.04.2 LTS (Jammy Jellyfish)
    CPU: (20) x64 12th Gen Intel(R) Core(TM) i7-12700H
    Memory: 43.96 GB / 62.51 GB
    Container: Yes
    Shell: 5.8.1 - /usr/bin/zsh
  Binaries:
    Node: 18.16.0 - /usr/local/bin/node
    npm: 9.5.1 - /usr/local/bin/npm
  npmPackages:
    rollup: ^3.22.0 => 3.22.0 
    rollup-plugin-typescript2: ^0.34.1 => 0.34.1 
    typescript: ^5.0.4 => 5.0.4 
  npmGlobalPackages:
    typescript: 5.0.4

rollup.config.js

:
/* global process */
import { nodeResolve } from '@rollup/plugin-node-resolve';
import ts from 'rollup-plugin-typescript2';
import dts from 'rollup-plugin-dts';
import json from '@rollup/plugin-json';
import typescript from 'typescript';
import terser from '@rollup/plugin-terser';
import commonjs from '@rollup/plugin-commonjs';
import externals from 'rollup-plugin-node-externals';

const PACKAGE_NAME = process.cwd();

export default [
  {
    input: `${PACKAGE_NAME}/src/index.ts`,
    output: {
      dir: `${PACKAGE_NAME}/dist`,
      format: 'es'
    },
    plugins: [
      externals(),
      json(),
      nodeResolve({ preferBuiltins: false }),
      commonjs(),
      ts({
        useTsconfigDeclarationDir: false,
        sourceMap: false,
        typescript,
        tsconfig: `${PACKAGE_NAME}/tsconfig.json`
      }),
      terser()
    ]
  },
  {
    input: `${PACKAGE_NAME}/dist/index.d.ts`,
    output: [{ file: `${PACKAGE_NAME}/dist/index.d.ts`, format: 'es' }],
    plugins: [dts()]
  }
];

tsconfig.json

:
{
  "compilerOptions": {
    "module": "ES2020",
    "target": "es2022",
    "lib": ["ES2021", "dom", "dom.iterable"],
    "sourceMap": true,
    "declaration": true,
    // "declarationDir": "./dts",
    "moduleResolution": "node",
    "rootDirs": ["middlewares", "utils", "routes"],
    "noImplicitReturns": true,
    "noImplicitThis": true,
    "noImplicitAny": false,
    "strictNullChecks": true,
    "esModuleInterop": true,
    "typeRoots": ["./@types", "./node_modules/@types"],
    "resolveJsonModule": true,
    "types": ["node"]
  },
  "exclude": ["__tests__", "node_modules", "dist", "*.d.ts", "*.d.cts"]
}

package.json

:
{
  "name": "@kilabs/devplatform-server-api",
  "type": "module",
  "private": true,
  "version": "0.0.1",
  "description": "The API monorepo for the devplatform",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "commit": "npx git-cz"
  },
  "author": "Alexander Vassbotn Røyne-Helgesen <alexander.royne-helgesen@company.no>",
  "license": "SEE LICENSE IN LICENSE",
  "devDependencies": {
    "@rollup/plugin-commonjs": "^24.1.0",
    "@rollup/plugin-json": "^6.0.0",
    "@rollup/plugin-node-resolve": "^15.0.2",
    "@rollup/plugin-terser": "^0.4.1",
    "@types/express": "^4.17.17",
    "@typescript-eslint/eslint-plugin": "^5.54.1",
    "@typescript-eslint/parser": "^5.54.1",
    "eslint": "^8.25.0",
    "eslint-config-airbnb": "^19.0.4",
    "eslint-config-prettier": "^8.5.0",
    "eslint-plugin-compat": "^4.1.2",
    "eslint-plugin-import": "^2.27.5",
    "eslint-plugin-prettier": "^4.2.1",
    "lerna": "^6.6.2",
    "prettier": "^2.7.1",
    "prettier-eslint": "^15.0.1",
    "rollup": "^3.22.0",
    "rollup-plugin-dts": "^5.3.0",
    "rollup-plugin-node-externals": "^6.0.1",
    "rollup-plugin-typescript2": "^0.34.1",
    "tslib": "^2.3.1",
    "typescript": "^5.0.4"
  },
  "workspaces": [
    "utils/*",
    "routes/*",
    "middlewares/*"
  ]
}

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 20 (8 by maintainers)

Commits related to this issue

Most upvoted comments

@ezolenko yep! that worked like a charm!

@agilgur5 well, that kind of goes without saying 😃 but I am not including the declaration files directly, nor is helmet.

@ezolenko thanks for the great work! I really appreciate it!

@phun-ky try explicitly including d.cts and d.mts:

include: [ "*.ts+(|x)", "**/*.ts+(|x)", '**/*.d.cts', '**/*.d.mts' ]

This will run them through typescript and I guess produce empty js.

Somehow import 'helmet' resolves to .d.cts:

rpt2: dependency '/home/projects/rpt2-repro-vnppxj/node_modules/helmet/index.d.cts'
rpt2:     imported by '/home/projects/rpt2-repro-vnppxj/src/index.ts'
rpt2: resolving 'helmet' imported by '/home/projects/rpt2-repro-vnppxj/src/index.ts'
rpt2:     to '/home/projects/rpt2-repro-vnppxj/node_modules/helmet/index.d.cts'