Skip to content

Commit

Permalink
add config.kit.package.emitTypes option (#1852)
Browse files Browse the repository at this point in the history
Closes #1851
  • Loading branch information
janosh committed Jul 9, 2021
1 parent 4c7ccfd commit c826016
Show file tree
Hide file tree
Showing 23 changed files with 109 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/happy-actors-camp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

add config.kit.package.emitTypes
4 changes: 2 additions & 2 deletions documentation/docs/12-packaging.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ A SvelteKit component library has the exact same structure as a SvelteKit app, e
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, TypeScript files will be transpiled to JavaScript.
- 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.
- 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](#configuration-package), but we strongly recommend against it.
- 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.
Expand All @@ -30,7 +30,7 @@ import Foo from 'your-library/Foo.svelte';

To publish the generated package:

```
```sh
npm publish package
```

Expand Down
4 changes: 3 additions & 1 deletion documentation/docs/14-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ const config = {
files: {
include: ['**'],
exclude: []
}
},
emitTypes: true
},
vite: () => ({})
},
Expand Down Expand Up @@ -176,6 +177,7 @@ Options related to [creating a package](#packaging).
- `dir` - output directory
- `exports` - contains a `includes` and a `excludes` array which specifies which files to mark as exported from the `exports` field of the `package.json`
- `files` - contains a `includes` and a `excludes` array which specifies which files to process and copy over when packaging
- `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).

### vite

Expand Down
6 changes: 4 additions & 2 deletions packages/kit/src/core/config/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ test('fills in defaults', () => {
files: {
include: ['**'],
exclude: []
}
},
emitTypes: true
},
serviceWorker: {
exclude: []
Expand Down Expand Up @@ -134,7 +135,8 @@ test('fills in partial blanks', () => {
files: {
include: ['**'],
exclude: []
}
},
emitTypes: true
},
serviceWorker: {
exclude: []
Expand Down
3 changes: 2 additions & 1 deletion packages/kit/src/core/config/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ const options = {
include: expect_array_of_strings(['**']),
exclude: expect_array_of_strings([])
}
}
},
emitTypes: expect_boolean(true)
}
},

Expand Down
3 changes: 2 additions & 1 deletion packages/kit/src/core/config/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ async function testLoadDefaultConfig(path) {
files: {
include: ['**'],
exclude: []
}
},
emitTypes: true
},
serviceWorker: {
exclude: []
Expand Down
6 changes: 4 additions & 2 deletions packages/kit/src/core/make_package/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import { mkdirp, rimraf } from '../filesystem/index.js';
export async function make_package(config, cwd = process.cwd()) {
rimraf(path.join(cwd, config.kit.package.dir));

// Generate type definitions first so hand-written types can overwrite generated ones
await emit_dts(config);
if (config.kit.package.emitTypes) {
// Generate type definitions first so hand-written types can overwrite generated ones
await emit_dts(config);
}

const files_filter = create_filter(config.kit.package.files);
const exports_filter = create_filter({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<script>
import { createEventDispatcher } from 'svelte';
/**
* @type {string}
*/
export const astring;
const dispatch = createEventDispatcher();
dispatch('event', true);
</script>

<slot {astring} />
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<script>
/**
* @type {import('./foo').Foo}
*/
export let foo;
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type Foo = boolean;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as Test } from './Test.svelte';
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "javascript-no-types",
"version": "1.0.0",
"description": "package-javascript-no-types-test",
"type": "module",
"exports": {
"./package.json": "./package.json",
"./index.js": "./index.js",
"./Test.svelte": "./Test.svelte",
"./Test2.svelte": "./Test2.svelte",
".": "./index.js"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "javascript-no-types",
"version": "1.0.0",
"description": "package-javascript-no-types-test"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
%svelte.head%
</head>
<body>
<div id="svelte">%svelte.body%</div>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<script>
import { createEventDispatcher } from 'svelte';
/**
* @type {string}
*/
export const astring;
const dispatch = createEventDispatcher();
dispatch('event', true);
</script>

<slot {astring} />
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<script>
/**
* @type {import('./foo').Foo}
*/
export let foo;
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type Foo = boolean;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as Test } from './Test.svelte';
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
kit: {
package: {
emitTypes: false
}
}
};
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "javascript",
"name": "typescript",
"version": "1.0.0",
"description": "package-javascript-test",
"description": "package-typescript-test",
"type": "module",
"exports": {
"./package.json": "./package.json",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "javascript",
"name": "typescript",
"version": "1.0.0",
"description": "package-javascript-test"
"description": "package-typescript-test"
}
4 changes: 4 additions & 0 deletions packages/kit/src/core/make_package/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,8 @@ test('create package (typescript)', async () => {
await test_make_package('typescript');
});

test('create package (javascript without types)', async () => {
await test_make_package('javascript_no_types');
});

test.run();
2 changes: 2 additions & 0 deletions packages/kit/types/config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export type Config = {
include?: string[];
exclude?: string[];
};
emitTypes?: boolean;
};
paths?: {
base?: string;
Expand Down Expand Up @@ -110,6 +111,7 @@ export type ValidatedConfig = {
include: string[];
exclude: string[];
};
emitTypes: boolean;
};
paths: {
base: string;
Expand Down

0 comments on commit c826016

Please sign in to comment.