ym-project / gulp-esbuild

gulp plugin for esbuild bundler
MIT License
42 stars 7 forks source link

Fix Type Declarations for ESModule #18

Closed manuth closed 1 year ago

manuth commented 1 year ago

Changes made in this PR will add type declarations for the ESModule version of this package. In doing so, this PR will fix #17.

ym-project commented 1 year ago

Look. We have problems with index.d.mts file. When we use inside mjs file this code snippet:

import gulpEsbuild, {createGulpEsbuild} from "gulp-esbuild";

console.log(gulpEsbuild);
console.log(createGulpEsbuild);
console.log(gulpEsbuild.createGulpEsbuild);

import("gulp-esbuild")
    .then(result => {
        console.log('dynamic import');
        console.log(result.createGulpEsbuild);
        console.log(result.default);
        console.log(result.default.createGulpEsbuild);
    })

Typescript uses index.d.mts and shows us, what out package has gulpEsbuild.createGulpEsbuild. But actually it is not the truth.

[Function: plugin]
[Function: createGulpEsbuild]
undefined
dynamic import
[Function: createGulpEsbuild]
[Function: plugin]
undefined

Should mts declaration file have import from index.js file? I think no.

ym-project commented 1 year ago

I mean mts declaration file should looks like:

import {Transform} from 'stream'
import {BuildOptions} from 'esbuild'

type Options = Omit<
    BuildOptions,
    'write' | 'incremental' | 'entryPoints' | 'stdin' | 'watch' | 'allowOverwrite' | 'absWorkingDir' | 'nodePaths'
> & {
    metafileName?: string
}

interface CreateOptions {
    incremental?: boolean
    pipe?: boolean
}

type GulpEsbuild = (options: Options) => Transform
type CreateGulpEsbuild = (options: CreateOptions) => GulpEsbuild

declare const gulpEsbuild: GulpEsbuild;
declare const createGulpEsbuild: CreateGulpEsbuild

export {createGulpEsbuild}
export default gulpEsbuild

Without & from d.ts

manuth commented 1 year ago

Thanks, I totally missed that one I'll fix it asap

manuth commented 1 year ago

Alright! Fixed it now. Sorry for the inconvenience