When I try to use the imports
rule to import a module, I find that it causes an error. Here you can use pnpm run build
to compile this project, and then you will see this error:
src/index.ts → dist...
[!] RollupError: src/internal.ts (1:7): Expected '{', got 'type' (Note that you need plugins to import files that are not JavaScript)
src/internal.ts (1:7)
1: export type Internal = 'internal'
^
2: export const internal = 'internal'
The actual reason for this error is that the rollup-plugin-dts plugin does not handle imports when compiling dts files. We need to let the plugin handle the corresponding imports.
Here we can run pnpm run compile to simulate the compilation process and observe the corresponding output. You can see:
src/internal.ts is from external library: true
Expected write file to be called
write file was called
Click file to view the script details.
emit -> emitWorker -> emitFiles -> getSourceFilesToEmit -> sourceFileMayBeEmitted
Through the above call stack, we can find that the reason for no output is that the program treats src/internal.ts
as an external dependency, which results in no compilation information being output for this file.
In the above code, it can be found that the plugin actually reuses the related program during compilation, and when we need to compile the src/internal.ts
file, it actually uses the program with src/index.ts
as the root.