diff --git a/.changeset/happy-actors-camp.md b/.changeset/happy-actors-camp.md new file mode 100644 index 000000000000..ccecb6e51de4 --- /dev/null +++ b/.changeset/happy-actors-camp.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +add config.kit.package.emitTypes diff --git a/documentation/docs/12-packaging.md b/documentation/docs/12-packaging.md index e2644a0dfce7..0cdd288d02ef 100644 --- a/documentation/docs/12-packaging.md +++ b/documentation/docs/12-packaging.md @@ -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. @@ -30,7 +30,7 @@ import Foo from 'your-library/Foo.svelte'; To publish the generated package: -``` +```sh npm publish package ``` diff --git a/documentation/docs/14-configuration.md b/documentation/docs/14-configuration.md index effc9ffb796e..1623136623b1 100644 --- a/documentation/docs/14-configuration.md +++ b/documentation/docs/14-configuration.md @@ -55,7 +55,8 @@ const config = { files: { include: ['**'], exclude: [] - } + }, + emitTypes: true }, vite: () => ({}) }, @@ -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 diff --git a/packages/kit/src/core/config/index.spec.js b/packages/kit/src/core/config/index.spec.js index 4472b27f2f9f..2906bd4eafba 100644 --- a/packages/kit/src/core/config/index.spec.js +++ b/packages/kit/src/core/config/index.spec.js @@ -36,7 +36,8 @@ test('fills in defaults', () => { files: { include: ['**'], exclude: [] - } + }, + emitTypes: true }, serviceWorker: { exclude: [] @@ -134,7 +135,8 @@ test('fills in partial blanks', () => { files: { include: ['**'], exclude: [] - } + }, + emitTypes: true }, serviceWorker: { exclude: [] diff --git a/packages/kit/src/core/config/options.js b/packages/kit/src/core/config/options.js index 567a8c2b906b..c7d4563d7544 100644 --- a/packages/kit/src/core/config/options.js +++ b/packages/kit/src/core/config/options.js @@ -103,7 +103,8 @@ const options = { include: expect_array_of_strings(['**']), exclude: expect_array_of_strings([]) } - } + }, + emitTypes: expect_boolean(true) } }, diff --git a/packages/kit/src/core/config/test/index.js b/packages/kit/src/core/config/test/index.js index 8a0d8e0779fd..4e8eea7dc3c1 100644 --- a/packages/kit/src/core/config/test/index.js +++ b/packages/kit/src/core/config/test/index.js @@ -46,7 +46,8 @@ async function testLoadDefaultConfig(path) { files: { include: ['**'], exclude: [] - } + }, + emitTypes: true }, serviceWorker: { exclude: [] diff --git a/packages/kit/src/core/make_package/index.js b/packages/kit/src/core/make_package/index.js index a99afa1a4a60..55f77ff7624d 100644 --- a/packages/kit/src/core/make_package/index.js +++ b/packages/kit/src/core/make_package/index.js @@ -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({ diff --git a/packages/kit/src/core/make_package/test/fixtures/javascript_no_types/expected/Test.svelte b/packages/kit/src/core/make_package/test/fixtures/javascript_no_types/expected/Test.svelte new file mode 100644 index 000000000000..31d118397f9d --- /dev/null +++ b/packages/kit/src/core/make_package/test/fixtures/javascript_no_types/expected/Test.svelte @@ -0,0 +1,12 @@ + + + diff --git a/packages/kit/src/core/make_package/test/fixtures/javascript_no_types/expected/Test2.svelte b/packages/kit/src/core/make_package/test/fixtures/javascript_no_types/expected/Test2.svelte new file mode 100644 index 000000000000..06a5a3408e95 --- /dev/null +++ b/packages/kit/src/core/make_package/test/fixtures/javascript_no_types/expected/Test2.svelte @@ -0,0 +1,6 @@ + diff --git a/packages/kit/src/core/make_package/test/fixtures/javascript_no_types/expected/foo.d.ts b/packages/kit/src/core/make_package/test/fixtures/javascript_no_types/expected/foo.d.ts new file mode 100644 index 000000000000..c941ad7b1ce6 --- /dev/null +++ b/packages/kit/src/core/make_package/test/fixtures/javascript_no_types/expected/foo.d.ts @@ -0,0 +1 @@ +export type Foo = boolean; diff --git a/packages/kit/src/core/make_package/test/fixtures/javascript_no_types/expected/index.js b/packages/kit/src/core/make_package/test/fixtures/javascript_no_types/expected/index.js new file mode 100644 index 000000000000..4c44188c3648 --- /dev/null +++ b/packages/kit/src/core/make_package/test/fixtures/javascript_no_types/expected/index.js @@ -0,0 +1 @@ +export { default as Test } from './Test.svelte'; diff --git a/packages/kit/src/core/make_package/test/fixtures/javascript_no_types/expected/package.json b/packages/kit/src/core/make_package/test/fixtures/javascript_no_types/expected/package.json new file mode 100644 index 000000000000..3738ea1a2e3f --- /dev/null +++ b/packages/kit/src/core/make_package/test/fixtures/javascript_no_types/expected/package.json @@ -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" + } +} diff --git a/packages/kit/src/core/make_package/test/fixtures/javascript_no_types/package.json b/packages/kit/src/core/make_package/test/fixtures/javascript_no_types/package.json new file mode 100644 index 000000000000..9f03308d46c5 --- /dev/null +++ b/packages/kit/src/core/make_package/test/fixtures/javascript_no_types/package.json @@ -0,0 +1,5 @@ +{ + "name": "javascript-no-types", + "version": "1.0.0", + "description": "package-javascript-no-types-test" +} diff --git a/packages/kit/src/core/make_package/test/fixtures/javascript_no_types/src/app.html b/packages/kit/src/core/make_package/test/fixtures/javascript_no_types/src/app.html new file mode 100644 index 000000000000..245305c4ef5b --- /dev/null +++ b/packages/kit/src/core/make_package/test/fixtures/javascript_no_types/src/app.html @@ -0,0 +1,12 @@ + + + + + + + %svelte.head% + + +
%svelte.body%
+ + diff --git a/packages/kit/src/core/make_package/test/fixtures/javascript_no_types/src/lib/Test.svelte b/packages/kit/src/core/make_package/test/fixtures/javascript_no_types/src/lib/Test.svelte new file mode 100644 index 000000000000..31d118397f9d --- /dev/null +++ b/packages/kit/src/core/make_package/test/fixtures/javascript_no_types/src/lib/Test.svelte @@ -0,0 +1,12 @@ + + + diff --git a/packages/kit/src/core/make_package/test/fixtures/javascript_no_types/src/lib/Test2.svelte b/packages/kit/src/core/make_package/test/fixtures/javascript_no_types/src/lib/Test2.svelte new file mode 100644 index 000000000000..06a5a3408e95 --- /dev/null +++ b/packages/kit/src/core/make_package/test/fixtures/javascript_no_types/src/lib/Test2.svelte @@ -0,0 +1,6 @@ + diff --git a/packages/kit/src/core/make_package/test/fixtures/javascript_no_types/src/lib/foo.d.ts b/packages/kit/src/core/make_package/test/fixtures/javascript_no_types/src/lib/foo.d.ts new file mode 100644 index 000000000000..c941ad7b1ce6 --- /dev/null +++ b/packages/kit/src/core/make_package/test/fixtures/javascript_no_types/src/lib/foo.d.ts @@ -0,0 +1 @@ +export type Foo = boolean; diff --git a/packages/kit/src/core/make_package/test/fixtures/javascript_no_types/src/lib/index.js b/packages/kit/src/core/make_package/test/fixtures/javascript_no_types/src/lib/index.js new file mode 100644 index 000000000000..4c44188c3648 --- /dev/null +++ b/packages/kit/src/core/make_package/test/fixtures/javascript_no_types/src/lib/index.js @@ -0,0 +1 @@ +export { default as Test } from './Test.svelte'; diff --git a/packages/kit/src/core/make_package/test/fixtures/javascript_no_types/svelte.config.cjs b/packages/kit/src/core/make_package/test/fixtures/javascript_no_types/svelte.config.cjs new file mode 100644 index 000000000000..aa2147412aaf --- /dev/null +++ b/packages/kit/src/core/make_package/test/fixtures/javascript_no_types/svelte.config.cjs @@ -0,0 +1,7 @@ +module.exports = { + kit: { + package: { + emitTypes: false + } + } +}; diff --git a/packages/kit/src/core/make_package/test/fixtures/typescript/expected/package.json b/packages/kit/src/core/make_package/test/fixtures/typescript/expected/package.json index a3a48fce0b0e..28cce45244fb 100644 --- a/packages/kit/src/core/make_package/test/fixtures/typescript/expected/package.json +++ b/packages/kit/src/core/make_package/test/fixtures/typescript/expected/package.json @@ -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", diff --git a/packages/kit/src/core/make_package/test/fixtures/typescript/package.json b/packages/kit/src/core/make_package/test/fixtures/typescript/package.json index e29e4689c6e0..ef3b7e7d81e4 100644 --- a/packages/kit/src/core/make_package/test/fixtures/typescript/package.json +++ b/packages/kit/src/core/make_package/test/fixtures/typescript/package.json @@ -1,5 +1,5 @@ { - "name": "javascript", + "name": "typescript", "version": "1.0.0", - "description": "package-javascript-test" + "description": "package-typescript-test" } diff --git a/packages/kit/src/core/make_package/test/index.js b/packages/kit/src/core/make_package/test/index.js index 107563424844..942eff4241e9 100644 --- a/packages/kit/src/core/make_package/test/index.js +++ b/packages/kit/src/core/make_package/test/index.js @@ -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(); diff --git a/packages/kit/types/config.d.ts b/packages/kit/types/config.d.ts index 48c95bf93425..365cf1f741b1 100644 --- a/packages/kit/types/config.d.ts +++ b/packages/kit/types/config.d.ts @@ -57,6 +57,7 @@ export type Config = { include?: string[]; exclude?: string[]; }; + emitTypes?: boolean; }; paths?: { base?: string; @@ -110,6 +111,7 @@ export type ValidatedConfig = { include: string[]; exclude: string[]; }; + emitTypes: boolean; }; paths: { base: string;