Skip to content

Commit 89ef569

Browse files
[breaking] move packaging functionality into its own package (#5730)
* [breaking] move package command into separate package * enhance create-svelte with libskeleton option * remove package from kit * update docs * move shared code into internal package * update workflow to build shared first * please? * remove cyclic dependency * fixes * fix * @internal/shared is a no-go since we no longer bundle, unless we publish it to npm which feels like overkill * remove bundling * fix workflow * goddammit vscode grow up. just add the extension. sheesh * update tests * pin vite version for now, to avoid distracting test failures * fix * add unused prepare script Co-authored-by: Rich Harris <[email protected]>
1 parent ad0ffa2 commit 89ef569

File tree

253 files changed

+845
-268
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

253 files changed

+845
-268
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ test-results/
55
package-lock.json
66
yarn.lock
77
/packages/create-svelte/template/CHANGELOG.md
8-
/packages/kit/src/packaging/test/watch/package
8+
/packages/package/test/**/package
99
/documentation/types.js
1010
.env
1111
.vercel_build_output

.prettierrc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
"files": [
2222
"**/CHANGELOG.md",
2323
"**/.svelte-kit/**",
24-
"packages/kit/src/packaging/test/fixtures/**/expected/**/*",
25-
"packages/kit/src/packaging/test/watch/expected/**/*",
26-
"packages/kit/src/packaging/test/watch/package/**/*",
24+
"packages/package/test/fixtures/**/expected/**/*",
25+
"packages/package/test/watch/expected/**/*",
26+
"packages/package/test/watch/package/**/*",
2727
"packages/kit/src/core/prerender/fixtures/**/*",
2828
"packages/migrate/migrations/routes/*/samples.md"
2929
],

documentation/docs/13-packaging.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22
title: Packaging
33
---
44

5-
> `svelte-kit package` is currently experimental and is not subject to Semantic Versioning rules. Non-backward compatible changes may occur in any future release.
5+
> `svelte-package` is currently experimental. Non-backward compatible changes may occur in any future release.
66
7-
You can use SvelteKit to build component libraries as well as apps.
7+
You can use SvelteKit to build apps as well as component libraries, using the `@sveltejs/package` package (`npm create svelte` has an option to set this up for you).
88

99
When you're creating an app, the contents of `src/routes` is the public-facing stuff; [`src/lib`](/docs/modules#$lib) contains your app's internal library.
1010

1111
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.
1212

13-
Running `svelte-kit package` will take the contents of `src/lib` and generate a `package` directory (which can be [configured](/docs/configuration#package)) containing the following:
13+
Running the `svelte-package` command from `@sveltejs/package` will take the contents of `src/lib` and generate a `package` directory (which can be [configured](/docs/configuration#package)) containing the following:
1414

1515
- All the files in `src/lib`, unless you [configure](/docs/configuration#package) custom `include`/`exclude` options. Svelte components will be preprocessed, TypeScript files will be transpiled to JavaScript.
16-
- Type definitions (`d.ts` files) which are generated for Svelte, JavaScript and TypeScript files. You need to install `typescript >= 4.0.0` and `svelte2tsx >= 0.4.1` for this. Type definitions are placed next to their implementation, hand-written `d.ts` files are copied over as is. You can [disable generation](/docs/configuration#package), but we strongly recommend against it.
16+
- Type definitions (`d.ts` files) which are generated for Svelte, JavaScript and TypeScript files. You need to install `typescript >= 4.0.0` for this. Type definitions are placed next to their implementation, hand-written `d.ts` files are copied over as is. You can [disable generation](/docs/configuration#package), but we strongly recommend against it.
1717
- A `package.json` copied from the project root with all fields except `"scripts"`, `"publishConfig.directory"` and `"publishConfig.linkDirectory"`. The `"dependencies"` field is included, which means you should add packages that you only need for your documentation or demo site to `"devDependencies"`. A `"type": "module"` and an `"exports"` field will be added if it's not defined in the original file.
1818

1919
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](/docs/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.
@@ -50,4 +50,6 @@ The `./package` above is referring to the directory name generated, change accor
5050

5151
### Caveats
5252

53+
All relative file imports need to be fully specified, adhering to Node's ESM algorithm. This means you cannot import the file `src/lib/something/index.js` like `import { something } from './something`, instead you need to import it like this: `import { something } from './something/index.js`. If you are using TypeScript, you need to import `.ts` files the same way, but using a `.js` file ending, _not_ a `.ts` file ending (this isn't under our control, the TypeScript team has made that decision). Setting `"moduleResolution": "NodeNext"` in your `tsconfig.json` or `jsconfig.json` will help you with this.
54+
5355
This is a relatively experimental feature and is not yet fully implemented. All files except Svelte files (preprocessed) and TypeScript files (transpiled to JavaScript) are copied across as-is.

documentation/docs/15-configuration.md

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,6 @@ const config = {
4848
},
4949
moduleExtensions: ['.js', '.ts'],
5050
outDir: '.svelte-kit',
51-
package: {
52-
dir: 'package',
53-
emitTypes: true,
54-
// excludes all .d.ts and files starting with _ as the name
55-
exports: (filepath) => !/^_|\/_|\.d\.ts$/.test(filepath),
56-
files: () => true
57-
},
5851
paths: {
5952
assets: '',
6053
base: ''
@@ -80,7 +73,17 @@ const config = {
8073
},
8174

8275
// options passed to svelte.preprocess (https://svelte.dev/docs#compile-time-svelte-preprocess)
83-
preprocess: null
76+
preprocess: null,
77+
78+
// options passed to @sveltejs/package
79+
package: {
80+
source: 'value of kit.files.lib, if available, else src/lib',
81+
dir: 'package',
82+
emitTypes: true,
83+
// excludes all .d.ts and files starting with _ as the name
84+
exports: (filepath) => !/^_|\/_|\.d\.ts$/.test(filepath),
85+
files: () => true
86+
}
8487
};
8588

8689
export default config;
@@ -208,8 +211,9 @@ The directory that SvelteKit writes files to during `dev` and `build`. You shoul
208211

209212
Options related to [creating a package](/docs/packaging).
210213

214+
- `source` - library directory
211215
- `dir` - output directory
212-
- `emitTypes` - by default, `svelte-kit package` will automatically generate types for your package in the form of `.d.ts` files. While generating types is configurable, we believe it is best for the ecosystem quality to generate types, always. Please make sure you have a good reason when setting it to `false` (for example when you want to provide handwritten type definitions instead)
216+
- `emitTypes` - by default, `svelte-package` will automatically generate types for your package in the form of `.d.ts` files. While generating types is configurable, we believe it is best for the ecosystem quality to generate types, always. Please make sure you have a good reason when setting it to `false` (for example when you want to provide handwritten type definitions instead)
213217
- `exports` - a function with the type of `(filepath: string) => boolean`. When `true`, the filepath will be included in the `exports` field of the `package.json`. Any existing values in the `package.json` source will be merged with values from the original `exports` field taking precedence
214218
- `files` - a function with the type of `(filepath: string) => boolean`. When `true`, the file will be processed and copied over to the final output folder, specified in `dir`
215219

@@ -226,14 +230,12 @@ import mm from 'micromatch';
226230

227231
/** @type {import('@sveltejs/kit').Config} */
228232
const config = {
229-
kit: {
230-
package: {
231-
exports: (filepath) => {
232-
if (filepath.endsWith('.d.ts')) return false;
233-
return mm.isMatch(filepath, ['!**/_*', '!**/internal/**']);
234-
},
235-
files: mm.matcher('!**/build.*')
236-
}
233+
package: {
234+
exports: (filepath) => {
235+
if (filepath.endsWith('.d.ts')) return false;
236+
return mm.isMatch(filepath, ['!**/_*', '!**/internal/**']);
237+
},
238+
files: mm.matcher('!**/build.*')
237239
}
238240
};
239241

packages/adapter-static/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@
3232
"svelte": "^3.48.0",
3333
"typescript": "^4.7.4",
3434
"uvu": "^0.5.3",
35-
"vite": "^3.0.0"
35+
"vite": "3.0.2"
3636
}
3737
}

packages/adapter-static/test/apps/prerendered/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"devDependencies": {
1111
"@sveltejs/kit": "workspace:*",
1212
"svelte": "^3.48.0",
13-
"vite": "^3.0.0"
13+
"vite": "3.0.2"
1414
},
1515
"type": "module"
1616
}

packages/adapter-static/test/apps/spa/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"@sveltejs/kit": "workspace:*",
1313
"sirv-cli": "^2.0.2",
1414
"svelte": "^3.48.0",
15-
"vite": "^3.0.0"
15+
"vite": "3.0.2"
1616
},
1717
"type": "module"
1818
}

packages/create-svelte/index.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ function write_common_files(cwd, options, name) {
5454
const pkg_file = path.join(cwd, 'package.json');
5555
const pkg = /** @type {any} */ (JSON.parse(fs.readFileSync(pkg_file, 'utf-8')));
5656

57-
files.forEach((file) => {
57+
sort_files(files).forEach((file) => {
5858
const include = file.include.every((condition) => matches_condition(condition, options));
5959
const exclude = file.exclude.some((condition) => matches_condition(condition, options));
6060

@@ -83,7 +83,7 @@ function write_common_files(cwd, options, name) {
8383
* @returns {boolean}
8484
*/
8585
function matches_condition(condition, options) {
86-
if (condition === 'default' || condition === 'skeleton') {
86+
if (condition === 'default' || condition === 'skeleton' || condition === 'libskeleton') {
8787
return options.template === condition;
8888
}
8989
if (condition === 'typescript' || condition === 'checkjs') {
@@ -135,6 +135,26 @@ function sort_keys(obj) {
135135
return sorted;
136136
}
137137

138+
/**
139+
* Sort files so that those which apply more generically come first so they
140+
* can be overwritten by files for more precise cases later.
141+
*
142+
* @param files {import('./types/internal').Common['files']}
143+
* */
144+
function sort_files(files) {
145+
return files.sort((f1, f2) => {
146+
const f1_more_generic =
147+
f1.include.every((include) => f2.include.includes(include)) &&
148+
f1.exclude.every((exclude) => f2.exclude.includes(exclude));
149+
const f2_more_generic =
150+
f2.include.every((include) => f1.include.includes(include)) &&
151+
f2.exclude.every((exclude) => f1.exclude.includes(exclude));
152+
const same = f1_more_generic && f2_more_generic;
153+
const different = !f1_more_generic && !f2_more_generic;
154+
return same || different ? 0 : f1_more_generic ? -1 : 1;
155+
});
156+
}
157+
138158
/** @param {string} name */
139159
function to_valid_package_name(name) {
140160
return name
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"extends": "./.svelte-kit/tsconfig.json",
3+
"compilerOptions": {
4+
"allowJs": true,
5+
"checkJs": true,
6+
"esModuleInterop": true,
7+
"forceConsistentCasingInFileNames": true,
8+
"resolveJsonModule": true,
9+
"skipLibCheck": true,
10+
"sourceMap": true,
11+
"strict": true,
12+
"moduleResolution": "NodeNext"
13+
}
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"extends": "./.svelte-kit/tsconfig.json",
3+
"compilerOptions": {
4+
"allowJs": true,
5+
"checkJs": true,
6+
"esModuleInterop": true,
7+
"forceConsistentCasingInFileNames": true,
8+
"resolveJsonModule": true,
9+
"skipLibCheck": true,
10+
"sourceMap": true,
11+
"strict": true,
12+
"moduleResolution": "NodeNext"
13+
}
14+
}

0 commit comments

Comments
 (0)