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

Making package handle more complex use cases. #4828

Closed
antony opened this issue May 5, 2022 · 1 comment · Fixed by #8922
Closed

Making package handle more complex use cases. #4828

antony opened this issue May 5, 2022 · 1 comment · Fixed by #8922
Labels
pkg:svelte-package Issues related to svelte-package
Milestone

Comments

@antony
Copy link
Member

antony commented May 5, 2022

Describe the problem

We use the svelte-kit package command to give us a clean, publishable asset at build time.

Whilst this works fine for a lot of simple cases, small components etc, we have the following use cases which it doesn't handle well:

  • Sapper (yes, some of us haven't ported just yet)
  • Nested package structures
  • Inside a monorepo

As real example, I might have the following structure:

src/
├─ lib/
│  ├─ index.js
│  ├─ component-a/
│  │  ├─ ComponentA.svelte
│  ├─ component-b/
│  │  ├─ ComponentB.svelte

This will, by default, generate the following package/package.json attributes

{
   ...
   "exports": {
     "./package.json": "./package.json",
     ".": "./index.js",
     "./ComponentA.svelte": "./ComponentA.svelte",
     "./ComponentB.svelte": "./ComponentA.svelte"
  },
  "svelte":"./index.js"
}

this is because package collapses the internal structure, and writes exports. This is fine for modern + SvelteKit consumers.

I don't actually want the two components exported individually so I do this:

// svelte.config.js
{
  kit: {
    exports: file => file === 'index.js'
  }
}

which gives me:

{
   ...
   "exports": {
     "./package.json": "./package.json",
     ".": "./index.js"
  },
  "svelte":"./index.js"
}

However in Sapper, I need to ensure that "main" and "module" aren't specified if the package is "type":"module", because these cause errors, so I need to instead specify exports within the source package.json:

{
   ...
   "exports": {
     ".": "./src/lib/index.js"
  },
  "svelte":"./src/lib/index.js"
}

when I run package now, it generates the same content as before, but overrides what is generated using the source package.json, which is a reasonable thing to do:

{
   ...
   "exports": {
     "./package.json": "./package.json",
     ".": "./src/lib/index.js"
  },
  "svelte":"./src/lib/index.js"
}

The issue here is that, package generates a flat file structure:

package/
├─ package.json
├─ index.js
├─ ComponentA.svelte
├─ ComponentB.svelte

so these overridden paths are wrong.

Ultimately this leaves me in a position where I can write a package which works in Prod or Dev, in a monorepo or not, or in Sapper. It's a bit awkard.

Describe the proposed solution

My proposal would be that we enhance the exports configuration property.
Right now it returns a simple Boolean to determine whether a specific file should be included or not.
Definitions within the source package.json are then merged (applied) to the output.

My proposal would be that we allow the exports function to receive two parameters:

  • a Map, of the generated output, before any changes

  • a Map of the source exports from the original package.json (though you could probably just import package.json to get this.

  • and allow you to modify it before returning it.

exports: (generated, source) => {
  if (file === 'index.js') {
    generated.set('./', './something-else.js')
  }
  return generated
}

Alternatives considered

  • exports could receive the proposed, generated output as a Map and allow you to modify it to your needs.
  • exports could receive files one by one as it does now and allow you to return a single mapping at a time, or an array of [ export_name, export_path ]
  • we can use Object hashes rather than Maps.
  • we can use a flag to indicate to svelte-kit package that it shouldn't override values from the source file.
  • I can hack the output using bash scripts as I do now.

Importance

would make my life easier

Additional Information

blocking all use of package, or rather, making it very much redundant.

@antony
Copy link
Member Author

antony commented May 5, 2022

cc @ghostdevv

@benmccann benmccann added the pkg:svelte-package Issues related to svelte-package label May 5, 2022
@Rich-Harris Rich-Harris added this to the post-1.0 milestone May 11, 2022
dummdidumm added a commit that referenced this issue Jan 31, 2023
- closes #7258
- closes #6824
- closes #4828
- part of #2242
- better message for #2884
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg:svelte-package Issues related to svelte-package
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants