egoist / tsup

The simplest and fastest way to bundle your TypeScript libraries.
https://tsup.egoist.dev
MIT License
9.24k stars 218 forks source link

'ModuleName' is not exported by #14

Closed OmgImAlexis closed 2 years ago

OmgImAlexis commented 4 years ago

This line causes the following error.

import { Database as Connection } from 'better-sqlite3';
> tsup index.ts --dts --bundle

Error: 'Database' is not exported by node_modules/@types/better-sqlite3/index.d.ts, imported by lib/drivers/sqlite.ts
    at error (/Users/xo/code/scratchdb/database/node_modules/rollup/dist/shared/rollup.js:161:30)
    at Module.error (/Users/xo/code/scratchdb/database/node_modules/rollup/dist/shared/rollup.js:15097:16)
    at handleMissingExport (/Users/xo/code/scratchdb/database/node_modules/rollup/dist/shared/rollup.js:14994:28)
    at Module.traceVariable (/Users/xo/code/scratchdb/database/node_modules/rollup/dist/shared/rollup.js:15478:24)
    at ModuleScope.findVariable (/Users/xo/code/scratchdb/database/node_modules/rollup/dist/shared/rollup.js:14047:39)
    at FunctionScope.findVariable (/Users/xo/code/scratchdb/database/node_modules/rollup/dist/shared/rollup.js:9527:38)
    at Identifier$1.bind (/Users/xo/code/scratchdb/database/node_modules/rollup/dist/shared/rollup.js:9938:40)
    at AssignmentPattern.bind (/Users/xo/code/scratchdb/database/node_modules/rollup/dist/shared/rollup.js:9571:23)
    at AssignmentPattern.bind (/Users/xo/code/scratchdb/database/node_modules/rollup/dist/shared/rollup.js:11615:15)
    at FunctionDeclaration.bind (/Users/xo/code/scratchdb/database/node_modules/rollup/dist/shared/rollup.js:9567:31)

This is the better-sqlite3 definition file it's referencing.

// Type definitions for better-sqlite3 5.4
// Project: http://github.com/JoshuaWise/better-sqlite3
// Definitions by: Ben Davies <https://github.com/Morfent>
//                 Mathew Rumsey <https://github.com/matrumz>
//                 Santiago Aguilar <https://github.com/sant123>
//                 Alessandro Vergani <https://github.com/loghorn>
//                 Andrew Kaiser <https://github.com/andykais>
//                 Mark Stewart <https://github.com/mrkstwrt>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 3.0

import Integer = require("integer");

type VariableArgFunction = (...params: any[]) => any;
type ArgumentTypes<F extends VariableArgFunction> = F extends (...args: infer A) => any
  ? A
  : never;

declare namespace BetterSqlite3 {
    interface Statement<BindParameters extends any[]> {
        database: Database;
        source: string;
        reader: boolean;

        run(...params: BindParameters): Database.RunResult;
        get(...params: BindParameters): any;
        all(...params: BindParameters): any[];
        iterate(...params: BindParameters): IterableIterator<any>;
        pluck(toggleState?: boolean): this;
        expand(toggleState?: boolean): this;
        raw(toggleState?: boolean): this;
        bind(...params: BindParameters): this;
        columns(): ColumnDefinition[];
        safeIntegers(toggleState?: boolean): this;
    }

    interface ColumnDefinition {
        name: string;
        column: string | null;
        table: string | null;
        database: string | null;
        type: string | null;
    }

    interface Transaction<F extends VariableArgFunction> {
        (...params: ArgumentTypes<F>): ReturnType<F>;
        default(...params: ArgumentTypes<F>): ReturnType<F>;
        deferred(...params: ArgumentTypes<F>): ReturnType<F>;
        immediate(...params: ArgumentTypes<F>): ReturnType<F>;
        exclusive(...params: ArgumentTypes<F>): ReturnType<F>;
    }

    interface Database {
        memory: boolean;
        readonly: boolean;
        name: string;
        open: boolean;
        inTransaction: boolean;

        // tslint:disable-next-line no-unnecessary-generics
        prepare<BindParameters extends any[] | {} = any[]>(source: string): BindParameters extends any[]
          ? Statement<BindParameters>
          : Statement<[BindParameters]>;
        transaction<F extends VariableArgFunction>(fn: F): Transaction<F>;
        exec(source: string): this;
        pragma(source: string, options?: Database.PragmaOptions): any;
        checkpoint(databaseName?: string): this;
        function(name: string, cb: (...params: any[]) => any): this;
        function(name: string, options: Database.RegistrationOptions, cb: (...params: any[]) => any): this;
        aggregate(name: string, options: Database.AggregateOptions): this;
        loadExtension(path: string): this;
        close(): this;
        defaultSafeIntegers(toggleState?: boolean): this;
        backup(destinationFile: string, options?: Database.BackupOptions): Promise<Database.BackupMetadata>;
    }

    interface DatabaseConstructor {
        new(filename: string, options?: Database.Options): Database;
        (filename: string, options?: Database.Options): Database;
        prototype: Database;

        Integer: typeof Integer;
        SqliteError: typeof SqliteError;
    }
}

declare class SqliteError implements Error {
    name: string;
    message: string;
    code: string;
    constructor(message: string, code: string);
}

declare namespace Database {
    interface RunResult {
        changes: number;
        lastInsertRowid: Integer.IntLike;
    }

    interface Options {
        memory?: boolean;
        readonly?: boolean;
        fileMustExist?: boolean;
        timeout?: number;
        verbose?: (message?: any, ...additionalArgs: any[]) => void;
    }

    interface PragmaOptions {
        simple?: boolean;
    }

    interface RegistrationOptions {
        varargs?: boolean;
        deterministic?: boolean;
        safeIntegers?: boolean;
    }

    interface AggregateOptions extends RegistrationOptions {
        start?: any;
        step: (total: any, next: any) => any;
        inverse?: (total: any, dropped: any) => any;
        result?: (total: any) => any;
    }

    interface BackupMetadata {
        totalPages: number;
        remainingPages: number;
    }
    interface BackupOptions {
        progress: (info: BackupMetadata) => number;
    }

    type Integer = typeof Integer;
    type SqliteError = typeof SqliteError;
    type Statement<BindParameters extends any[] | {} = any[]> = BindParameters extends any[]
      ? BetterSqlite3.Statement<BindParameters>
      : BetterSqlite3.Statement<[BindParameters]>;
    type ColumnDefinition = BetterSqlite3.ColumnDefinition;
    type Transaction = BetterSqlite3.Transaction<VariableArgFunction>;
    type Database = BetterSqlite3.Database;
}

declare const Database: BetterSqlite3.DatabaseConstructor;
export = Database;
OmgImAlexis commented 4 years ago

Switching to import sqlite3 from 'better-sqlite3'; for this specific library does allow tsup to build.

egoist commented 4 years ago

I guess you don't need to bundle types from node_modules right? I can make that disabled by default to fix it..

OmgImAlexis commented 4 years ago

Actually I kinda like being able to bundle all my types for production.

For example i have a few deps they’re dynamically imported and are optional but I’d still like their types available.

Having an option to disable might be good for some users though.

OmgImAlexis commented 4 years ago

Got hit with this again. Seems like a parsing issue on tsup's side since the import itself works and intellisense even shows it.

For example this is the typing showing for the following import yet we still get the error thrown.

(alias) type NextHandleFunction = (req: IncomingMessage, res: http.ServerResponse, next: NextFunction) => void
import NextHandleFunction
import { NextHandleFunction } from 'connect';
Error: 'NextHandleFunction' is not exported by node_modules/@types/connect/index.d.ts, imported by node_modules/@types/body-parser/index.d.ts
    at error (/Users/xo/code/unraid/core/node_modules/rollup/dist/shared/rollup.js:161:30)
    at Module.error (/Users/xo/code/unraid/core/node_modules/rollup/dist/shared/rollup.js:15097:16)
    at handleMissingExport (/Users/xo/code/unraid/core/node_modules/rollup/dist/shared/rollup.js:14994:28)
    at Module.traceVariable (/Users/xo/code/unraid/core/node_modules/rollup/dist/shared/rollup.js:15478:24)
    at ModuleScope.findVariable (/Users/xo/code/unraid/core/node_modules/rollup/dist/shared/rollup.js:14047:39)
    at FunctionScope.findVariable (/Users/xo/code/unraid/core/node_modules/rollup/dist/shared/rollup.js:9527:38)
    at Identifier$1.bind (/Users/xo/code/unraid/core/node_modules/rollup/dist/shared/rollup.js:9938:40)
    at AssignmentPattern.bind (/Users/xo/code/unraid/core/node_modules/rollup/dist/shared/rollup.js:9571:23)
    at AssignmentPattern.bind (/Users/xo/code/unraid/core/node_modules/rollup/dist/shared/rollup.js:11615:15)
    at FunctionDeclaration.bind (/Users/xo/code/unraid/core/node_modules/rollup/dist/shared/rollup.js:9567:31)
egoist commented 4 years ago

Seems rollup-plugin-dts doesn't support exportEqual namespace yet?:

// types.d.ts
declare namespace Foo {
  export const bar: string
}

export = Foo

This doesn't work:

import { bar } from './types'
OmgImAlexis commented 4 years ago

I've opened an issue over there. https://github.com/Swatinem/rollup-plugin-dts/issues/93

Swatinem commented 4 years ago

Actually I kinda like being able to bundle all my types for production.

Please don‘t do that. :-D:-D

Sometimes the things from @types have really strange idioms in them, and its a nightmare to work with.

This example in particular. I think, this is whats going on:

OmgImAlexis commented 4 years ago

Please don‘t do that. :-D:-D

@Swatinem it may not make sense in all cases but in my specific case it makes a lot of sense since it's an internal library and I'm specifically pulling in types from those modules to type my interfaces.

Swatinem commented 4 years ago

This actually works because TS uses structural typing. With nominal typing, you would have to re-export the actual thing from your external library, otherwise you wouldn’t be able to use that stuff. Anyway, I digress

OmgImAlexis commented 4 years ago

Either way is there a possibility of this being fixed?

egoist commented 4 years ago

FYI I disabled this by default in the latest version

Swatinem commented 4 years ago

Either way is there a possibility of this being fixed?

I don‘t think an equivalent to "import default interop" is possible in dts. Or at least I wouldn’t know how.

IanVS commented 2 years ago

I'm hitting this error in a different package (fast-json-stringify). What do I need to do to avoid the error?

FYI I disabled this by default in the latest version

@egoist what setting is this? Maybe the project I'm working in turned it back on?