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

Add --preserveModules (similar to babel --out-dir) #321

Closed
yordis opened this issue Nov 12, 2019 · 3 comments · May be fixed by #535
Closed

Add --preserveModules (similar to babel --out-dir) #321

yordis opened this issue Nov 12, 2019 · 3 comments · May be fixed by #535
Labels
solution: duplicate This issue or pull request already exists solution: workaround available There is a workaround available for this issue topic: multi-entry Related to multi-entry support topic: preserveModules Related to Rollup's preserveModules feature

Comments

@yordis
Copy link
Contributor

yordis commented Nov 12, 2019

Current Behavior

We need to specify an entry point.

Desired Behavior

Specify --outDir over an entry point

Suggested Solution

I am interested in having TypeScript/Babel CLI like compilations rather than outputting a single file.

Who does this impact? Who is this for?

All users, friendly for ESM builds without having to import everything in the entry file.

Describe alternatives you've considered

  • TypeScript compiler but I don't have support for the Babel plugins 🤷‍♂
  • Babel CLI but I don't have support for SASS loaders 🤷‍♂

Additional context

@swyxio
Copy link
Collaborator

swyxio commented Dec 4, 2019

popular feature request. i'll leave it to jared.

@agilgur5
Copy link
Collaborator

agilgur5 commented Dec 11, 2019

@yordis it sounds like what you're looking for is Rollup's preserveModules option. If that's correct, you could do that by modifying tsdx.config.js to add config.preserveModules = true:

module.exports = {
  rollup(config, opts) {
    if (opts.format === 'esm') {
        config = { ...config, preserveModules: true }
        config.output = { ...config.output, dir: 'dist/', entryFileNames: '[name].esm.js' }
        delete config.output.file
    }
    return config
}

One last edit: updated to the same as #276 (comment), please see #276 for discussion and evolution of the preserveModules use-case and workaround.

You'll have to change your package.json module to index.esm.js for this (unavoidable with output.dir).

This is only for ESM as, per #276 (comment), CJS preserveModules doesn't quite work the same way and has various consequences. For multi-entry support, please see #367


EDIT: I tried this myself, and had to add config.output.dir = ... above as well as delete config.output.file, because output.dir is required when using preserveModules and because output.file is mutually exclusive with output.dir.

The problem with this is that it only outputs minified CJS files, because output.file can't be specified, and therefore the various file extensions like .esm.js and .cjs.js etc end up overwriting each other.
That might be fine for your use case, but isn't ideal. The only other simple workaround would be to have a separate output.dir for each build format (e.g. /dist/cjs/, /dist/esm/, etc), but that's non-trivial and potentially impossible to do with just a tsdx.config.js (might require internal changes to support). EDIT: can use config.output.dir = process.cwd() + '/dist/' + config.output.format + '/' for this like in #382 , but I believe it will still be missing non-minified/dev CJS output.

@agilgur5
Copy link
Collaborator

Going to close this as a duplicate of #276 since these are around the same feature and the use-case has evolved there.

Please see #276 for discussion on preserveModules, #535 for a WIP flag for that, and #367 for multi-entry support.

I've updated my comment above to the latest workaround from #276 as well.

@agilgur5 agilgur5 added solution: duplicate This issue or pull request already exists solution: workaround available There is a workaround available for this issue topic: multi-entry Related to multi-entry support and removed kind: feature New feature or request labels Apr 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
solution: duplicate This issue or pull request already exists solution: workaround available There is a workaround available for this issue topic: multi-entry Related to multi-entry support topic: preserveModules Related to Rollup's preserveModules feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants