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

Request: Allow AMD builds #938

Open
marciel-truesoft opened this issue Nov 19, 2020 · 4 comments
Open

Request: Allow AMD builds #938

marciel-truesoft opened this issue Nov 19, 2020 · 4 comments
Labels
good first issue Good for newcomers kind: feature New feature or request PR welcome problem: stale Issue has not been responded to in some time

Comments

@marciel-truesoft
Copy link

Current Behavior

UMD build doesn't support code-splitting and dynamic imports (rollup/rollup#3490, rollup/rollup#3491) due to Rollup current limitations, and tsdx cli don't accept the format option amd when creating build config

Suggested Solution

Consider support AMD module format, or at least accept amd option, so we can customize the build output using the tsdx.config.js

tsdx build --format amd

Who does this impact? Who is this for?

This behavior impact who need an AMD compatible bundle and code-splitting support

Describe alternatives you've considered

For now, the only alternative to having an AMD compatible bundle with code-splitting is to use Rollup directly

@agilgur5 agilgur5 added the kind: feature New feature or request label Nov 20, 2020
@agilgur5
Copy link
Collaborator

agilgur5 commented Nov 23, 2020

Yes the lack of code-splitting in UMD also impacts #367; upstream in rollup/rollup#3422 (comment) I was told it isn't currently supported as UMD lacks a way to load deps. I suppose the "ideas" mentioned there refer to the issues you linked.

Could you provide a rationale for why you want AMD output support as opposed to ESM or CJS?
For a similar example, #788 was looking for IIFE support but found UMD usage satisfactory.

Each of the current build options has some of there own nuances, such as ESM not having dev/prod split vs CJS and UMD not being able to code-split. Adding another format option may add some more complexity if it has its own nuances as well (though I believe AMD can be treated the same way as CJS) which therein adds a maintenance and potential breakage burden (like #367 and UMD's lack of code-split).
Browser ESM is fairly well-supported now and Node ESM support is growing, so I'm seeing less and less benefits of various formats over time in general as well.

@marciel-truesoft
Copy link
Author

marciel-truesoft commented Nov 24, 2020

Hi, thanks for your time @agilgur5

Could you provide a rationale for why you want AMD output support as opposed to ESM or CJS?

I agree with you, as for today ESM is well-supported, but sometimes (and in my specific scenario) we need to deliver libraries to some fair old codebase that is heavily based on AMD, UMD output do this trick very well, though I still use tsdx and use to bundle targeting ESM (aka greenfield projects) the lack of code-split for UMD is really a blocker, so I keep a custom rollup config aside only for bundle targeting AMD.

@agilgur5
Copy link
Collaborator

agilgur5 commented Nov 24, 2020

some fair old codebase that is heavily based on AMD

Gotcha, so basically just for legacy codebases. If your library does multiple formats (e.g. CJS, ESM, and AMD), how do you import AMD given the lack of ability to specify it in a package.json field? Or do you just import the build directly?

For CJS, TSDX uses the main field and outputs the dev/prod switch entry. That dev/prod entry is CJS so it would need a separate version for AMD. Support for just outputting the dev & prod AMD bundles can be added more simply I believe, but the package.json field I'm not sure about and an AMD dev/prod switch would take more time (I'm also in the middle of improving some issues with the dev/prod switch by rewriting it as a Rollup plugin). If you import the bundle directly, that shouldn't be a huge deal though.

A PR would be welcome for the simple version -- can see in #788 the place in the code that would need to be modified to add any new format (that and the build and watch CLIs need some small modifications). This would probably be less code than keeping a totally separate Rollup config 😅


Also, there are codemods to help convert AMD to ESM or CJS, e.g. jscodeshift + other scripts and this recast script. I had to do it a handful of years ago for a 10k+ LoC project before too.

@agilgur5 agilgur5 added the good first issue Good for newcomers label Nov 24, 2020
@jaredpalmer
Copy link
Owner

Should we add iife? It's useful for building 3rd-party JS that is used in script tags (like embed widgets or analytics scripts)

@agilgur5 agilgur5 added the problem: stale Issue has not been responded to in some time label Mar 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers kind: feature New feature or request PR welcome problem: stale Issue has not been responded to in some time
Projects
None yet
Development

No branches or pull requests

3 participants