-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
svelte-kit package #1499
Merged
Merged
svelte-kit package #1499
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
7fe9946
first stab at svelte-kit package command
Rich-Harris e6fd416
include/exclude files as well
Rich-Harris a70968a
make typescript happy
Rich-Harris ac45fac
tidy up
Rich-Harris 3e66d9d
improve error message
Rich-Harris 2a5bb3a
exclude underscore-prefixed files from exports by default
Rich-Harris 5f16809
move files
Rich-Harris aa8a509
add docs
Rich-Harris 5f21dc6
changeset
Rich-Harris d3853d0
gitignore package directory
Rich-Harris 21b00ed
update lockfile, who knows why
Rich-Harris f7d1ad4
update unit tests
Rich-Harris 3e6f5f4
merge master -> svelte-kit-package
Rich-Harris a23a33a
copy over readme
Rich-Harris File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@sveltejs/kit': patch | ||
--- | ||
|
||
Add svelte-kit package command |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'create-svelte': patch | ||
--- | ||
|
||
gitignore package directory |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
--- | ||
title: Packaging | ||
--- | ||
|
||
You can use SvelteKit to build component libraries as well as apps. | ||
|
||
When you're creating an app, the contents of `src/routes` is the public-facing stuff; [`src/lib`](#modules-lib) contains your app's internal library. | ||
|
||
A SvelteKit component library has the exact same structure as a SvelteKit app, except that `src/lib` is the public-facing bit. `src/routes` might be a documentation or demo site that accompanies the library, or it might just be a sandbox you use during development. | ||
|
||
Running `svelte-kit package` will take the contents of `src/lib` and generate a `package` directory (which can be [configured](#configuration-package)) containing the following: | ||
|
||
- All the files in `src/lib`, unless you [configure](#configuration-package) custom `include`/`exclude` options. Svelte components will be preprocessed (but note the [caveats](#packaging-caveats) below) | ||
- A `package.json` that copies the `name`, `version`, `description`, `keywords`, `homepage`, `bugs`, `license`, `author`, `contributors`, `funding`, `repository`, `dependencies`, `private` and `publishConfig` fields from the root of the project, and adds a `"type": "module"` and an `"exports"` field | ||
|
||
The `"exports"` field contains the package's entry points. By default, all files in `src/lib` will be treated as an entry point unless they start with (or live in a directory that starts with) an underscore, but you can [configure](#configuration-package) this behaviour. If you have a `src/lib/index.js` or `src/lib/index.svelte` file, it will be treated as the package root. | ||
|
||
For example, if you had a `src/lib/Foo.svelte` component and a `src/lib/index.js` module that re-exported it, a consumer of your library could do either of the following: | ||
|
||
```js | ||
import { Foo } from 'your-library'; | ||
``` | ||
|
||
```js | ||
import Foo from 'your-library/Foo.svelte'; | ||
``` | ||
|
||
## Publishing | ||
|
||
To publish the generated package: | ||
|
||
``` | ||
npm publish package | ||
``` | ||
|
||
If you configure a custom [`package.dir`](#configuration-package), change `package` accordingly. | ||
|
||
## Caveats | ||
|
||
This is a relatively experimental feature and is not yet fully implemented: | ||
|
||
- if a preprocessor is specified, `.svelte` files are transformed (meaning they can contain TypeScript, for example), but `.d.ts` files are not generated | ||
- `.ts` files are not currently transformed, and will cause the process to fail | ||
- all other files are copied across as-is |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
.DS_Store | ||
node_modules | ||
/.svelte-kit | ||
/build | ||
/functions | ||
/package |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
.DS_Store | ||
node_modules | ||
/.svelte-kit | ||
/build | ||
/functions | ||
/package |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
import { preprocess } from 'svelte/compiler'; | ||
import globrex from 'globrex'; | ||
import { mkdirp, rimraf } from '../filesystem'; | ||
|
||
/** | ||
* @param {import('types/config').ValidatedConfig} config | ||
* @param {string} cwd | ||
*/ | ||
export async function make_package(config, cwd = process.cwd()) { | ||
rimraf(path.join(cwd, config.kit.package.dir)); | ||
|
||
const files_filter = create_filter(config.kit.package.files); | ||
const exports_filter = create_filter(config.kit.package.exports); | ||
|
||
const files = walk(config.kit.files.lib); | ||
|
||
const pkg = JSON.parse(fs.readFileSync(path.join(cwd, 'package.json'), 'utf8')); | ||
|
||
const package_pkg = { | ||
name: pkg.name, | ||
version: pkg.version, | ||
description: pkg.description, | ||
keywords: pkg.keywords, | ||
homepage: pkg.homepage, | ||
bugs: pkg.bugs, | ||
license: pkg.license, | ||
author: pkg.author, | ||
contributors: pkg.contributors, | ||
funding: pkg.funding, | ||
repository: pkg.repository, | ||
dependencies: pkg.dependencies, | ||
private: pkg.private, | ||
publishConfig: pkg.publishConfig, | ||
type: 'module', | ||
/** @type {Record<string, string>} */ | ||
exports: { | ||
'./package.json': './package.json' | ||
} | ||
}; | ||
|
||
for (const file of files) { | ||
if (!files_filter(file)) continue; | ||
|
||
const filename = path.join(config.kit.files.lib, file); | ||
const source = fs.readFileSync(filename, 'utf8'); | ||
|
||
const ext = path.extname(file); | ||
const svelte_ext = config.extensions.find((ext) => file.endsWith(ext)); // unlike `ext`, could be e.g. `.svelte.md` | ||
|
||
/** @type {string} */ | ||
let out_file; | ||
|
||
/** @type {string} */ | ||
let out_contents; | ||
|
||
if (svelte_ext) { | ||
// it's a Svelte component | ||
// TODO how to emit types? | ||
out_file = file.slice(0, -svelte_ext.length) + '.svelte'; | ||
out_contents = config.preprocess | ||
? (await preprocess(source, config.preprocess, { filename })).code | ||
: source; | ||
} else if (ext === '.ts' && !file.endsWith('.d.ts')) { | ||
// TODO transpile TS file and emit types | ||
// also, we want to emit types from JSDoc annotations in .js files | ||
throw new Error('svelte-kit package does not yet support TypeScript'); | ||
} else { | ||
out_file = file; | ||
out_contents = source; | ||
} | ||
|
||
write(path.join(cwd, config.kit.package.dir, out_file), out_contents); | ||
|
||
if (exports_filter(file)) { | ||
const entry = `./${out_file}`; | ||
package_pkg.exports[entry] = entry; | ||
} | ||
} | ||
|
||
const main = package_pkg.exports['./index.js'] || package_pkg.exports['./index.svelte']; | ||
|
||
if (main) { | ||
package_pkg.exports['.'] = main; | ||
} | ||
|
||
write( | ||
path.join(cwd, config.kit.package.dir, 'package.json'), | ||
JSON.stringify(package_pkg, null, ' ') | ||
); | ||
|
||
const project_readme = path.join(cwd, 'README.md'); | ||
const package_readme = path.join(cwd, config.kit.package.dir, 'README.md'); | ||
|
||
if (fs.existsSync(project_readme) && !fs.existsSync(package_readme)) { | ||
fs.copyFileSync(project_readme, package_readme); | ||
} | ||
} | ||
|
||
/** | ||
* @param {{ | ||
* include: string[]; | ||
* exclude: string[]; | ||
* }} options | ||
*/ | ||
function create_filter(options) { | ||
const include = options.include.map((str) => str && globrex(str)); | ||
const exclude = options.exclude.map((str) => str && globrex(str)); | ||
|
||
/** @param {string} str */ | ||
const filter = (str) => | ||
include.some((glob) => glob.regex.test(str)) && !exclude.some((glob) => glob.regex.test(str)); | ||
|
||
return filter; | ||
} | ||
|
||
/** @param {string} cwd */ | ||
function walk(cwd) { | ||
/** @type {string[]} */ | ||
const all_files = []; | ||
|
||
/** @param {string} dir */ | ||
function walk_dir(dir) { | ||
const files = fs.readdirSync(path.join(cwd, dir)); | ||
|
||
for (const file of files) { | ||
const joined = path.join(dir, file); | ||
const stats = fs.statSync(path.join(cwd, joined)); | ||
|
||
if (stats.isDirectory()) { | ||
walk_dir(joined); | ||
} else { | ||
all_files.push(joined); | ||
} | ||
} | ||
} | ||
|
||
walk_dir(''); | ||
return all_files; | ||
} | ||
|
||
/** | ||
* @param {string} file | ||
* @param {string} contents | ||
*/ | ||
function write(file, contents) { | ||
mkdirp(path.dirname(file)); | ||
fs.writeFileSync(file, contents); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Rich-Harris did you mean to remove both
/build
and/functions
here?