I've included an example code snippet here: https://github.com/thejohnfreeman/bugs/commit/b4ff15a670691ada024589693d22f4fd0abae08d
The module called parent
is primarily composed of type declarations written in TypeScript. The source entrypoint for TypeScript is pointed to by the types
field in its package.json
, while the compiled JavaScript entrypoint is specified in the main
field. It utilizes a tsconfig.json
file in its build process.
In contrast, the module named child
depends on parent
and is also authored in TypeScript. This module aims to create a bundle using Rollup and includes its own tsconfig.json
.
Upon setup completion:
cd child
yarn
The following command functions correctly:
npx tsc --project tsconfig.json --outDir out --module esnext
The expected output is generated at out/index.js
:
import parent from 'parent';
console.log(parent);
(Interestingly, the produced JavaScript matches the original TypeScript code.)
However, the command below does not yield the desired outcome:
npx rollup --config
An error message is displayed:
src/index.ts → dist/index.js...
[!] Error: Unexpected token (Note that you need plugins to import files that are not JavaScript)
node_modules/parent/src/internal/index.ts (1:12)
1: import type Parent from './Parent'
^
2: declare const parent: Parent
3: export default parent
Error: Unexpected token (Note that you need plugins to import files that are not JavaScript)
at error (node_modules/rollup/dist/shared/rollup.js:5309:30)
at Module.error (node_modules/rollup/dist/shared/rollup.js:9765:16)
at Module.tryParse (node_modules/rollup/dist/shared/rollup.js:10169:25)
at Module.setSource (node_modules/rollup/dist/shared/rollup.js:10068:24)
at ModuleLoader.addModuleSource (node_modules/rollup/dist/shared/rollup.js:18451:20)
at async ModuleLoader.fetchModule (node_modules/rollup/dist/shared/rollup.js:18507:9)
at async Promise.all (index 0)
at async ModuleLoader.fetchStaticDependencies (node_modules/rollup/dist/shared/rollup.js:18533:34)
at async Promise.all (index 0)
at async ModuleLoader.fetchModule (node_modules/rollup/dist/shared/rollup.js:18509:9)
Suspicions arise that Rollup compiles the child
module successfully to an ESNext module with the line import parent from 'parent'
. However, instead of resolving this module specifier to the parent module's JavaScript, it resolves it to the TypeScript version containing invalid JavaScript (
import type Parent from './Parent'
). Is this the root cause? How can I resolve this issue?