Swatinem / rollup-plugin-dts

A rollup plugin to generate .d.ts rollup files for your typescript project
GNU Lesser General Public License v3.0
804 stars 68 forks source link

[import ModuleName = require('module-name')] Syntax not yet supported #89

Closed OmgImAlexis closed 4 years ago

OmgImAlexis commented 4 years ago
Error: Could not load /Users/xo/code/scratchdb/database/node_modules/@types/better-sqlite3/index.d.ts (imported by lib/drivers/sqlite.ts): Syntax not yet supported

  10 | // TypeScript Version: 3.0
  11 | 
> 12 | import Integer = require("integer");
     | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  13 | 
  14 | type VariableArgFunction = (...params: any[]) => any;
  15 | type ArgumentTypes<F extends VariableArgFunction> = F extends (...args: infer A) => any
    at Transformer.convertStatement (/Users/xo/code/scratchdb/database/node_modules/tsup/dist/rollup-plugin-dts-a7b98f98.js:972:19)
    at new Transformer (/Users/xo/code/scratchdb/database/node_modules/tsup/dist/rollup-plugin-dts-a7b98f98.js:880:18)
    at transformFile (/Users/xo/code/scratchdb/database/node_modules/tsup/dist/rollup-plugin-dts-a7b98f98.js:1234:29)
    at Object.load (/Users/xo/code/scratchdb/database/node_modules/tsup/dist/rollup-plugin-dts-a7b98f98.js:1280:33)
    at /Users/xo/code/scratchdb/database/node_modules/rollup/dist/shared/rollup.js:18234:25
    at processTicksAndRejections (internal/process/task_queues.js:82:5)
Swatinem commented 4 years ago

TBH, I have never understood what the semantics of import foo = require("bar"); are supposed to be. Isn’t that statement deprecated in favor of some standard import syntax?

Either way, are you explicitly allowing external modules? Usually anything from node_modules should be excluded.

OmgImAlexis commented 4 years ago

Me either but it’s how the library is using it.

I’ll have to check I’m using https://github.com/egoist/tsup

Swatinem commented 4 years ago

Oh nice! Your project is https://github.com/scratchdb/database right? Can you try this plugin directly? I don’t know why its even trying to load things from node_modules.

OmgImAlexis commented 4 years ago

Yes that’s the repo.

I can try. I haven’t used rollup before outside of tooling like tsup. Any pointers?

Swatinem commented 4 years ago

from my readme:

import dts from "rollup-plugin-dts";

const config = [
  // …
  {
    input: "./my-input/index.d.ts",
    output: [{ file: "dist/my-library.d.ts", format: "es" }],
    plugins: [dts()],
  },
];

export default config;
OmgImAlexis commented 4 years ago

I’ll give this a go and get back to you. Thanks.

OmgImAlexis commented 4 years ago

If you pull that database repo and run rollup -c you'll see this.

➜  database git:(master) rollup -c

./lib/index.ts → dist/database.d.ts...
(!) Circular dependency: lib/index.ts -> lib/drivers/index.ts -> lib/drivers/sqlite.ts -> lib/index.ts
created dist/database.d.ts in 2.8s

If I check the output file it looks okay from a quick inspection.

import { Database } from 'better-sqlite3';

interface DriverConnection {
    uri: string;
}
interface DriverOptions {
    connections: DriverConnection[];
}
declare type Logger = Console;
declare class Driver {
    protected log: Logger;
    constructor(options: DriverOptions);
}

interface Options extends DriverOptions {
}
declare class SQLite<Tables> extends Driver {
    connection: Database;
    constructor(options: Options);
    protected serialiseParams(params: {
        [key: string]: unknown;
    }): {
        [k: string]: unknown;
    };
    insert<T extends keyof Tables>(table: T, params: DatabaseInsert<Tables[T]>): Promise<Tables[T]>;
    update<T extends keyof Tables>(table: T, id: string, params: DatabaseInsert<Tables[T]>): Promise<void>;
    buildSelect<T extends keyof Tables>(table: T, fields?: string[], params?: Partial<Tables[T]>): Promise<{
        sqlQuery: string;
        sqlParams: {
            [k: string]: unknown;
        };
    }>;
    findOne<T extends keyof Tables>(table: T, fields?: string[], params?: Partial<Tables[T]>): Promise<Tables[T]>;
    find<T extends keyof Tables>(table: T, fields?: string[], params?: Partial<Tables[T]>): Promise<Tables[T][]>;
}

interface Options$1 extends DriverOptions {
}
declare class MySQL<Tables> extends Driver {
    constructor(options: Options$1);
}

declare type DatabaseInsert<T> = Pick<T, Exclude<keyof T, 'id' | 'lastUpdated'>>;
interface Connection {
    uri: string;
}
declare type Driver$1 = 'SQLite' | 'MySQL';
interface Options$2 {
    connections: Connection[];
}
interface Drivers<Tables> {
    SQLite: SQLite<Tables>;
    MySQL: MySQL<Tables>;
}
declare function createDatabase<Tables>(driver: 'SQLite', options: Options$2): Drivers<Tables>['SQLite'];
declare function createDatabase<Tables>(driver: 'MySQL', options: Options$2): Drivers<Tables>['MySQL'];

export { Connection, DatabaseInsert, Driver$1 as Driver, Options$2 as Options, createDatabase };
OmgImAlexis commented 4 years ago

Could this be a tsup issue?

Swatinem commented 4 years ago

possible. they use a custom resolver, maybe that is not correctly flagging externals from node_modules.

https://github.com/egoist/tsup/blob/57dfa9633c3d9fa159579d3ef32e3f15a2128b5d/src/index.ts#L65-L69

Swatinem commented 4 years ago

either way, this still is a real issue with my plugin that I need to fix at some point.

egoist commented 4 years ago

@Swatinem possible. they use a custom resolver, maybe that is not correctly flagging externals from node_modules.

Yes I should probably make it externalize types from node_modules by default, that was there because I want to bundle types for Deno.

Thanks for making this plugin btw, I use it a lot!

Swatinem commented 4 years ago

Thanks for making this plugin btw, I use it a lot!

glad to hear ;-)

that was there because I want to bundle types for Deno.

How does that even work in deno?

egoist commented 4 years ago

Deno supports .d.ts for js files:

// @deno-types="https://unpkg.com/mod/types.d.ts"
import { foo } from 'https://unpkg.com/mod/index.js'

But it can't resolve types from node_modules, so I had to bundle them in the .d.ts.