Skip to content
New issue

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

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

Already on GitHub? # to your account

TypeScript build tool that maps require('mymodule/foo') into './src/foo.ts' #130

Closed
papb opened this issue Jan 20, 2021 · 10 comments
Closed

Comments

@papb
Copy link

papb commented Jan 20, 2021

Rewording the request by @lifeiscontent here:

Consider the following TypeScript project that will be distributed as a npm package:

.
├── package.json
├── src
│   ├── index.ts
│   └── utils.ts
├── tsconfig.json
└── yarn.lock

Compiling this will generate ./dist/index.js and ./dist/utils.js. To import the module, require('the-module') works as long as the main field of package.json is setup correctly, which is fine and very common to do. However, there is no good way to also provide ./utils.ts directly as require('the-module/utils'). Instead, people would have to do require('the-module/dist/utils') which is ugly.

There could be a command line utility to be called within prepack that:

  • Reorders everything in a way that automatically puts everything available there directly; or
  • (hackier) Injects a smart call to pirates on the index.js file.

Thoughts?

@Richienb
Copy link

This can be done in the resolveId part of a rollup plugin.

@papb
Copy link
Author

papb commented Jan 20, 2021

Hi @Richienb, thanks for the fast response!! I may look into it later. @lifeiscontent, what do you think?

@lifeiscontent
Copy link

@Richienb I'd love to see an example, its hard to visualize as I don't have a full understanding of the ecosystem of rollup.

@Richienb
Copy link

The resolveId function in a rollup plugin is provided with the exact path within an import statement and the file that is importing it as arguments. We can use those arguments to generate a new path to be imported from.

@Richienb
Copy link

rollup/rollup#558

@sindresorhus
Copy link
Owner

You can solve this with the exports field in package.json, which is the modern way to expose subpaths.

@lifeiscontent
Copy link

@sindresorhus is this functionality that will work out of the box right now? or something that could be implemented in rollup as a potential feature?

@sindresorhus
Copy link
Owner

There's no need to use Rollup at all.

@Richienb
Copy link

Ohhhh, I misunderstood the question. I thought you wanted to remap imports from within the module itself on build time, like this rejected TypeScript request: microsoft/TypeScript#15479.

@papb
Copy link
Author

papb commented Jan 21, 2021

Ahhh!! Awesome! Thank you all very much!!

@Richienb I was lucky that you misunderstood, because that TS issue you just linked happened to help me with something else here 😅

@papb papb closed this as completed Jan 21, 2021
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants