Skip to content

Error with import assertions in rollup.config.ts: SyntaxError: Unexpected identifier 'assert' #474

@earshinov

Description

@earshinov

Summary

Running rollup -c --configPlugin=typescript2 with

rollup.config.ts:

import pkg from './package.json' with { type: 'json' };

export default {
  input: 'src/index.mjs',
  output: [{ file: pkg.exports['.'].import, format: 'es', sourcemap: true }]
};

under Node 22.9.0 produces

[!] SyntaxError: Unexpected identifier 'assert'
    at compileSourceTextModule (node:internal/modules/esm/utils:337:16)
    at ModuleLoader.moduleStrategy (node:internal/modules/esm/translators:166:18)
    at callTranslator (node:internal/modules/esm/loader:437:14)
    at ModuleLoader.moduleProvider (node:internal/modules/esm/loader:443:30)
    at async ModuleJob._link (node:internal/modules/esm/module_job:106:19)

Repro: https://github.com/earshinov/repro-rollup-import-assertion

Troubleshooting

  1. Does tsc have the same output?
    No, tsc doesn't have the problem
  2. Does your Rollup plugin order match this plugin's compatibility?
    Yes
  3. Can you create a minimal example that reproduces this behavior? Preferably, use this environment for your reproduction.
    https://github.com/earshinov/repro-rollup-import-assertion

What happens and why it is incorrect

I resumed working on one of my older projects, ran it under Node 22 and discovered that Rollup + rollup-plugin-typescript2 are no longer able to process my rollup.config.ts, where I had import pkg from './package.json' assert { type: 'json' }.

I converted this import into import pkg from './package.json' with { type: 'json' }; as expected by the ECMAScript standard and Node 22, but the problem persisted.

Upgrading Rollup and rollup-plugin-typescript2 didn't help.

Typescript itself (tsc) has no problems with transpiling rollup.config.ts whatsoever.

Environment

Copying package.json from the test repo:

"devDependencies": {
    "rollup": "4.13.0",
    "rollup-plugin-typescript2": "^0.36.0",
    "typescript": "5.4.3"
},
"engineStrict": true,
"engines": {
    "node": "22.9.0"
}
$ node --version
v22.9.0

Windows 11.

Versions

$ npx envinfo --npmPackages typescript,rollup,rollup-plugin-typescript2 --npmGlobalPackages typescript --system os,cpu --binaries node,yarn,npm
  System:
    OS: Windows 11 10.0.22631
    CPU: (12) x64 Intel(R) Core(TM) i7-9850H CPU @ 2.60GHz
    Memory: 24.59 GB / 47.71 GB
  Binaries:
    Node: 22.9.0 - C:\Program Files\nodejs\node.EXE
    Yarn: 4.1.1 - C:\Program Files\nodejs\yarn.CMD
    npm: 10.8.3 - C:\Program Files\nodejs\npm.CMD
  npmPackages:
    rollup: 4.13.0 => 4.13.0
    rollup-plugin-typescript2: ^0.36.0 => 0.36.0
    typescript: 5.4.3 => 5.4.3

rollup.config.ts

:
import pkg from './package.json' with { type: 'json' };

export default {
  input: 'src/index.mjs',
  output: [{ file: pkg.exports['.'].import, format: 'es', sourcemap: true }]
};

tsconfig.json

:
// https://github.com/tsconfig/bases/blob/main/bases/next.json
{
    "$schema": "https://json.schemastore.org/tsconfig",
    "compilerOptions": {
        "declaration": true,
        "esModuleInterop": true,
        "forceConsistentCasingInFileNames": true,
        "isolatedModules": true,
        "lib": [
            "es2020"
        ],
        "module": "esnext",
        "moduleResolution": "bundler",
        "skipLibCheck": true,
        "strict": true,
        "target": "ESNext",
    },
    "include": [
        "src"
    ],
    "references": [
        {
            "path": "./tsconfig.dev.json"
        }
    ]
}

tsconfig.dev.json

:
{
    "extends": "./tsconfig.json",
    "compilerOptions": {
        "composite": true,
        // in ./tsconfig.json: referenced project 'tsconfig.dev.json' may not disable emit
        "noEmit": false,
        // `outDir` is only needed for running `tsc -p tsconfig.dev.json` manually
        "outDir": "out"
    },
    "include": [
        "package.json",
        "rollup.config.ts"
    ]
}

package.json

:
{
  "name": "test",
  "description": "",
  "exports": {
    ".": {
      "import": "dist/index.mjs"
    }
  },
  "scripts": {
    "build": "bash scripts/build.sh"
  },
  "packageManager": "yarn@4.1.1",
  "devDependencies": {
    "rollup": "4.13.0",
    "rollup-plugin-typescript2": "^0.36.0",
    "typescript": "5.4.3"
  },
  "engineStrict": true,
  "engines": {
    "node": "22.9.0"
  }
}

plugin output with verbosity 3

:
rpt2: built-in options overrides: {
    "noEmitHelpers": false,
    "importHelpers": true,
    "noResolve": false,
    "noEmit": false,
    "noEmitOnError": false,
    "inlineSourceMap": false,
    "outDir": "D:/tmp/repro-rollup-import-assertion/node_modules/.cache/rollup-plugin-typescript2/placeholder",
    "allowNonTsExtensions": true
}
rpt2: parsed tsconfig: {
    "options": {
        "declaration": true,
        "esModuleInterop": true,
        "forceConsistentCasingInFileNames": true,
        "isolatedModules": true,
        "lib": [
            "lib.es2020.d.ts"
        ],
        "module": 99,
        "moduleResolution": 100,
        "skipLibCheck": true,
        "strict": true,
        "target": 99,
        "composite": true,
        "noEmit": false,
        "outDir": "D:/tmp/repro-rollup-import-assertion/node_modules/.cache/rollup-plugin-typescript2/placeholder",
        "configFilePath": "D:/tmp/repro-rollup-import-assertion/tsconfig.dev.json",
        "noEmitHelpers": false,
        "importHelpers": true,
        "noResolve": false,
        "noEmitOnError": false,
        "inlineSourceMap": false,
        "allowNonTsExtensions": true
    },
    "fileNames": [
        "D:/tmp/repro-rollup-import-assertion/rollup.config.ts",
        "D:/tmp/repro-rollup-import-assertion/package.json"
    ],
    "typeAcquisition": {
        "enable": false,
        "include": [],
        "exclude": []
    },
    "raw": {
        "extends": "./tsconfig.json",
        "compilerOptions": {
            "composite": true,
            "noEmit": false,
            "outDir": "out"
        },
        "include": [
            "package.json",
            "rollup.config.ts"
        ],
        "compileOnSave": false
    },
    "errors": [],
    "wildcardDirectories": {},
    "compileOnSave": false
}
rpt2: typescript version: 5.4.3
rpt2: tslib version: 2.7.0
rpt2: rollup version: 4.13.0
rpt2: rollup-plugin-typescript2 version: 0.36.0
rpt2: plugin options:
{
    "check": true,
    "verbosity": 3,
    "clean": true,
    "cacheRoot": "D:\\tmp\\repro-rollup-import-assertion\\node_modules\\.cache\\rollup-plugin-typescript2",
    "include": [
        "*.ts+(|x)",
        "**/*.ts+(|x)",
        "**/*.cts",
        "**/*.mts"
    ],
    "exclude": [
        "*.d.ts",
        "**/*.d.ts",
        "**/*.d.cts",
        "**/*.d.mts"
    ],
    "abortOnError": true,
    "rollupCommonJSResolveHack": false,
    "tsconfig": "tsconfig.dev.json",
    "useTsconfigDeclarationDir": false,
    "tsconfigOverride": {},
    "transformers": [],
    "tsconfigDefaults": {},
    "objectHashIgnoreUnknownHack": false,
    "cwd": "D:\\tmp\\repro-rollup-import-assertion",
    "typescript": "version 5.4.3"
}
rpt2: rollup config:
{
    "input": "D:\\tmp\\repro-rollup-import-assertion\\rollup.config.ts",
    "plugins": [
        {
            "name": "rpt2"
        }
    ],
    "treeshake": false
}
rpt2: tsconfig path: D:/tmp/repro-rollup-import-assertion/tsconfig.dev.json
rpt2: included:
[
    "*.ts+(|x)",
    "**/*.ts+(|x)",
    "**/*.cts",
    "**/*.mts"
]
rpt2: excluded:
[
    "*.d.ts",
    "**/*.d.ts",
    "**/*.d.cts",
    "**/*.d.mts"
]
rpt2: transpiling 'D:\tmp\repro-rollup-import-assertion\rollup.config.ts'
rpt2: generated declarations for 'D:/tmp/repro-rollup-import-assertion/rollup.config.ts'
rpt2: generating target 1
rpt2: emitting declarations for 'D:/tmp/repro-rollup-import-assertion/rollup.config.ts' to 'rollup.config.d.ts'
[!] SyntaxError: Unexpected identifier 'assert'
    at compileSourceTextModule (node:internal/modules/esm/utils:337:16)
    at ModuleLoader.moduleStrategy (node:internal/modules/esm/translators:166:18)
    at callTranslator (node:internal/modules/esm/loader:437:14)
    at ModuleLoader.moduleProvider (node:internal/modules/esm/loader:443:30)
    at async ModuleJob._link (node:internal/modules/esm/module_job:106:19)

Metadata

Metadata

Assignees

No one assigned

    Labels

    scope: configPluginRelated to usage as a configPlugin for reading rollup.config.ts (vs. regular plugin)

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions