Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
BlackAsLight opened this issue Jan 23, 2024 · 2 comments

Comments

@BlackAsLight
Copy link
Contributor

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());
@lucacasonato
Copy link
Owner

Does this also happen if you use npm: specifiers?

@BlackAsLight
Copy link
Contributor Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants