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

ESM Code Splitting Imports #2489

Closed
GravlLift opened this issue Aug 22, 2022 · 4 comments
Closed

ESM Code Splitting Imports #2489

GravlLift opened this issue Aug 22, 2022 · 4 comments

Comments

@GravlLift
Copy link

GravlLift commented Aug 22, 2022

Apologies if this is a very obvious question, but I couldn't find anything in the docs to explain this:

I'm looking to use esbuild to generate esm formatted output without bundling. As a simple case, I tried this.

a.ts

import { writeBLog } from "./b";
console.log("a.ts");
writeBLog();

b.ts

export function writeBLog() {
  console.log("b.js")
}

esbuild a.ts --outdir=out --format=esm

I was expecting to see both a a.js and b.js file in my out dir. Instead I only get a.js

a.js

import { writeLog } from "./b";
console.log("a.ts");
writeLog();

And of course, that import now refers to a non-existent b.js file.

It seems like esbuild is treating all files as external by default. What setting do I need to set to get esbuild to ouput a b.js file (and any other imports) as well?

EsBuild version 0.15.5

@evanw
Copy link
Owner

evanw commented Aug 22, 2022

There’s nothing in the docs about this because this isn’t something esbuild does. You can do this by instead passing a.ts and b.ts together on the command line.

Can you say more about your use case? Why do you want to do this instead of bundling?

@GravlLift
Copy link
Author

GravlLift commented Aug 22, 2022

I'm looking to share a module in memory across multiple entry points, make use of asynchronous loading of said modules, and not duplicate code in the outputs. So in the above example, I'd like to have a third file, c.ts, that also imports b.ts. I'd list a and c and entry points, and then all three files would be output to the out directory as a.js, b.js, and c.js. Rather than duplicating the b module code in both a.js and c.js, the b module would sit in its own file.

Sounds like I'll need to declare all my files as entry points in order to accomplish what I'm trying to do? Might need to get creative with files that import a node_module...

@hyrious
Copy link

hyrious commented Aug 22, 2022

Maybe you're looking for the splitting feature.

If you want esbuild to preserve folder structure (preserveModules) #708, or manually control what files it will emit (manualChunks) #207, these are not supported yet.

@GravlLift
Copy link
Author

Yeah, I guess #708 is indeed what I'm looking for. Thank you.

@GravlLift GravlLift closed this as not planned Won't fix, can't repro, duplicate, stale Aug 22, 2022
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

3 participants