-
Notifications
You must be signed in to change notification settings - Fork 69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is it possible to import declaration files with this plugin? #273
Comments
For rollup, the problem is that d.ts don't contain any code that translate to java script -- translated d.ts file is an empty js file. Normally you are not supposed to import d.ts files directly, but let typescript find them itself (by setting |
Actually, if you have plugin order correctly, rpt2 will translate ts file and those imports will be gone from resulting js file, so rollup should not see d.ts files at all. Could you post output with verbosity 3? |
Say I have an import type Foo from './foo'
declare const foo: Foo
export default foo Next to this file is The code I want to bundfle imports the Right now I either get a parse error from Rollup when it goes to parse |
I'll try to distill a minimal example to demonstrate, but if you can answer the hypothetical, I'd greatly appreciate it. |
Import type is supposed to get fully erased by typescript, so rollup shouldn't see anything. Are there other plugins in the chain? |
Before it in the chain are just |
Another way to debug this: if you enable verbosity 3 it will print path to cache files that contain translated js, find one for the ts file that does the import and check if js still has the import for some reason. Also you could run tsc directly and check if it generates any imports there and if js it produces is different from what plugin makes. Also check typescript version the plugin uses and make sure it is the one you expect. |
Going through the exercise of distilling the problem helped me identify the true cause, and it's unrelated to this plugin. It was the import after the one I first suspected, that imported an enumeration. Following that road led me to this example: If a Here is an example you can clone and try. This repository contains two NPM packages named |
I don't think you are using types correctly there. If you expect anything to be actually exported, it needs to be in ts file. Try writing a typescript module that exports what you need (enums, etc) and then run |
I did write that TypeScript module. It is the module When I run |
You might have to actually assign that value to a var in the module and then export that. If I do: declare const bar: number; // convincing ts there is a global var with that name somewhere
export let bar1 = bar; // exporting local var from this module I get this in js export var bar1 = bar; And this in d.ts: export declare let bar1: number; For enums, you don't declare them, you define them: export enum A { a, b, c }
export const enum B { a, b, c } Makes this js (A is exported and B is erased because typescript will substitute the values directly) export var A;
(function (A) {
A[A["a"] = 0] = "a";
A[A["b"] = 1] = "b";
A[A["c"] = 2] = "c";
})(A || (A = {})); And this d.ts export declare enum A {
a = 0,
b = 1,
c = 2
}
export declare const enum B {
a = 0,
b = 1,
c = 2
} Unless I misunderstand what you are trying to do :) |
Going to close this out as I believe this has been answered well and in entirety -- declarations are indeed not supposed to be imported and indeed would not produce valid ES modules. That is TS behavior and not specific to this plugin. OP also suggested themselves that the issue was not with this plugin and has not responded to further comments. Note that triple-slash directives may behave differently, which may be what OP intended to do instead? |
I have a dependency with a single
.ts
source file that imports declarations from many.d.ts
declaration files. When Rollup hits a declaration file, I get aPARSE_ERROR
telling me that Rollup is trying to parse the TypeScript declaration file as an ES module. I tracked that down to theexclude
setting for this plugin which skips.d.ts
files by default. After settingexclude: []
, this plugin parses the file, but whatever ES module it returns to Rollup is missing the declarations inside, because I get aMISSING_EXPORT
error. Is there a way to see what ES module this plugin is returning for a given TS declaration file? Is there a way to get it to include all the exports in a declaration file?The text was updated successfully, but these errors were encountered: