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.jsfile.
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-packageoutputssrc/libfiles todistfolder at the root of the project folder. We need to define anexportsfield 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-packagecommand (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 packageTip
Running pnpm package does the following:
svelte-kit syncSynchronises generated type definitionssvelte-packagePackages/copies src/lib to ./dist folder by defaultpublintChecks 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
filesproperty 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 loginand simply runnpm publish --access publicThe 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 publicUse 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