-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Description
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 originalpackage.json
(though you could probably just importpackage.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.