Svelte kit makes it easier to publish packages on npm using svelte-package
. We can both have the npm package and docs web page in a single repo. Svelte also automatically generate types for our svelte component and js files.
- Create Svelte project
pnpm create svelte@latest my-package
- CD into the project
cd my-package
- Create some component (src/lib/components/AwesomeComponent.svelte) or export something from
src/lib/index.js
file.
Important
Note that we need to re-export components in lib/index.js too!
- We then need Nodejs to know where is the entry point of our package. By default running
svelte-package
outputssrc/lib
files todist
folder at the root of the project folder. We need to define anexports
field in our packcage.json file pointing to those files in the dist folder.
"exports": {
".": {
"types": "./dist/index.d.ts",
"svelte": "./dist/index.js"
},
"./dist/components/LamyDebugbar.svelte": {
"types": "./dist/components/AwesomeComponent.svelte.d.ts",
"svelte": "./dist/components/AwesomeComponent.svelte"
}
}
Note
The "exports" field is used to restrict external access to non-exported module files, also enables a module to import itself using "name".
The above exports mapping also ties the types to our svelte and js files. Typically exports would be written as:
"exports": {
".": "./lib/index.js",
}
But with Community Condition Definitions such as the types
field we can tell yping systems to resolve the typing file for the given export.
Also see nodejs docs for Community Conditions Definitions
- Run
svelte-package
command (this is provided by @sveltejs/package package).
// To make our lives easier, let's put the svelte-package command in package.json scripts
"scripts": {
"package": "svelte-kit sync && svelte-package && publint",
"prepublishOnly": "npm run package",
}
Now we can run the command like so:
pnpm package
Tip
Running pnpm package
does the following:
svelte-kit sync
Synchronises generated type definitionssvelte-package
Packages/copies src/lib to ./dist folder by defaultpublint
Checks for linting errors Note that we no longer need to copy package.json to package dist/output folder.
Note
Take note of the prepublishOnly
script as it will be run automatically when running npm publish
later.
- Add a
files
property in package.json file and add ["dist"] as the property value.
// package.json
"files": [
"dist"
],
Note
If there is a "files" list in package.json, then only the files specified will be included. (If directories are specified, then they will be walked recursively and their contents included, subject to the same ignore rules.). In this case only dist folder will be included when we publish to npm. Certain files that are relevant to package installation and distribution are always included such as package.json, README.md, LICENSE, and so on.
- Finally login with npm cli via
npm login
and simply runnpm publish --access public
The default tag is latest. If publishing a release candidate or beta version run the command with--tag flag
Tip
To publish a version preview of a package i.e. x.0.0-next.1
npm publish --tag next --access public
Use the word next, beta or rc etc. depending on your context.
Our npm package will then have the following structure on npmjs website:
package-name/
├── dist/
│ ├── components/
│ │ ├── AwesomeComponent.svelte.d.ts
│ │ └── AwesomeComponent.svelte
│ ├── some-file.d.ts
│ ├── some-file.js
│ ├── index.d.ts
│ └── index.js
├── LICENSE
├── README.md
└── package.json