esbuild_deno_loader icon indicating copy to clipboard operation
esbuild_deno_loader copied to clipboard

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

Open BlackAsLight opened this issue 1 year ago • 2 comments

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/[email protected]/mod.ts'
import { Peer } from 'https://esm.sh/[email protected]?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/[email protected]/mod.d.ts'
import { build, stop } from 'https://deno.land/x/[email protected]/mod.js'
import { denoPlugins } from 'https://deno.land/x/[email protected]/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());

BlackAsLight avatar Jan 23 '24 08:01 BlackAsLight

Does this also happen if you use npm: specifiers?

lucacasonato avatar Jan 23 '24 14:01 lucacasonato

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

BlackAsLight avatar Jan 24 '24 03:01 BlackAsLight