FredKSchott/pika-pack

How to exporting subfolder files in Node.js?

Closed this issue ยท 4 comments

rxjs provides something like this to developers:

import { tap } from 'rxjs/operators';

When writing a big package, this is a nice design.

But I don't know to do this using @pika/pack, do I have to exporting everything in my src/index.ts(I'm using TypeScript) ?

And by doing this, I would lose the namespacing design.

Thank you.

OK, I see @pika/plugin-build-node is using Rollup, so it's impossible.

But for Node.js package (especially big framework), bundle all entry points in one file isn't ideal.

@pika/pack and most of the Pika toolchain encourage a single entrypoint per package. We do support consuming packages that export multiple file entrypoints (ex: @pika/web can install any file entrypoint) but for package creation (@pika/pack) we only support the one entrypoint.

There are a few reasons for this:

  • There's no standard on whether internal files are a part of the public API. Is moving an internal file a breaking change? This can lead to unexpected issues.
  • There's no way to support multiple distributions (ex: if your package imports some-other-package/dist-node/some-entrypoint.js, now all other packages will use the hardcoded Node CJS file, even in dist-web and others.
  • There's no way for tooling to know which files are entrypoints and which are internal

Thank you for your reply @FredKSchott ! Now I fully understand the design philosophy behind @pika/pack, and I do agree with this.

Maybe export * as SubNamspace from './sub-namespace'; would solve this? (TypeScript doesn't have this yet.)

I think this could be documented, others may have this question too (I searched before this issue). This doc maybe even can help push the standards (or implementations) forward. ๐Ÿ˜ƒ

Cheers.

Yes, I think something like that will make sense. The best approach I've seen so far is https://github.com/preactjs/preact, which uses sub-packages: preact has a package.json, but preact/hooks also has a package.json. So you can import packages by sub-path and still support "main"/"module"/"browser"/etc.

+1 more documentation on all this is definitely needed, so thanks for asking and letting us at least answer it here. A better docs portal is coming, I swear :)