lucacasonato / esbuild_deno_loader

Deno module resolution for `esbuild`
https://jsr.io/@luca/esbuild-deno-loader
MIT License
176 stars 46 forks source link

Bug: Failing to treeshake dead imports of sub files that have node type imports in them #106

Open BlackAsLight opened 10 months ago

BlackAsLight commented 10 months ago

Esbuild seems to be failing to exclude dead imports of sub files believed to be because they have a node type import. I am not sure if this bug is caused by this plugin or esbuild itself.

Reproduction

deno run -A bundle.ts

main.ts

import { random } from './lib.ts'

console.log('Hello World')
console.log(random())

lib.ts

import { Iter } from 'https://deno.land/x/iterstar@v1.1.2/mod.ts'
import { Peer } from 'https://esm.sh/peerjs@1.5.2?bundle-deps'

export function createPeer() {
    return new Peer()
}

export function createIter() {
    return new Iter((function* () {
        let i = 0
        while (true)
            yield i++
    })())
}

export function random() {
    return Math.random()
}

bundle.ts

// @deno-types='https://deno.land/x/esbuild@v0.19.11/mod.d.ts'
import { build, stop } from 'https://deno.land/x/esbuild@v0.19.11/mod.js'
import { denoPlugins } from 'https://deno.land/x/esbuild_deno_loader@0.8.4/mod.ts'

await esbuild('./main.ts', './main.js')

stop()

async function esbuild(inPath: string, outPath: string) {
    {
        const { errors, warnings } = await build({
            plugins: denoPlugins(),
            entryPoints: [ inPath ],
            outfile: outPath,
            format: 'esm',
            bundle: true,
            treeShaking: true,
        })
        errors.forEach(x => console.error(x))
        warnings.forEach(x => console.warn(x))
    }
}

main.js expected output

The actual output includes the entire contents of the Peer import, but not the IterStar import

// lib.ts
function random() {
  return Math.random();
}

// main.ts
console.log("Hello World");
console.log(random());
lucacasonato commented 10 months ago

Does this also happen if you use npm: specifiers?

BlackAsLight commented 10 months ago

It does not. Changing the URL to npm:peerjs seems to make it produce the expected output.