From 7fe99467894c847958e9cfa0247776e525815e52 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Wed, 19 May 2021 18:18:03 -0400 Subject: [PATCH 01/13] first stab at svelte-kit package command --- packages/kit/package.json | 3 +- packages/kit/src/cli.js | 16 +++ .../kit/src/core/load_config/index.spec.js | 14 ++ packages/kit/src/core/load_config/options.js | 30 +++++ .../kit/src/core/load_config/test/index.js | 7 + packages/kit/src/core/make_package/index.js | 120 ++++++++++++++++++ packages/kit/types/config.d.ts | 14 ++ pnpm-lock.yaml | 10 +- 8 files changed, 211 insertions(+), 3 deletions(-) create mode 100644 packages/kit/src/core/make_package/index.js diff --git a/packages/kit/package.json b/packages/kit/package.json index 0b7fa339d957..19f34eb8c062 100644 --- a/packages/kit/package.json +++ b/packages/kit/package.json @@ -10,9 +10,9 @@ }, "devDependencies": { "@rollup/plugin-replace": "^2.4.2", - "@sveltejs/kit": "workspace:*", "@types/amphtml-validator": "^1.0.1", "@types/cookie": "^0.4.0", + "@types/globrex": "^0.1.0", "@types/marked": "^2.0.2", "@types/mime": "^2.0.3", "@types/node": "^14.14.43", @@ -22,6 +22,7 @@ "cookie": "^0.4.1", "devalue": "^2.0.1", "eslint": "^7.25.0", + "globrex": "^0.1.2", "kleur": "^4.1.4", "marked": "^2.0.3", "node-fetch": "^3.0.0-beta.9", diff --git a/packages/kit/src/cli.js b/packages/kit/src/cli.js index 97b44bac0836..f80e01a461b2 100644 --- a/packages/kit/src/cli.js +++ b/packages/kit/src/cli.js @@ -178,6 +178,22 @@ prog ); }); +prog + .command('package') + .describe('Create a package') + .option('-d, --dir', 'Destination directory', 'package') + .action(async () => { + const config = await get_config(); + + const { make_package } = await import('./core/make_package/index.js'); + + try { + await make_package(config); + } catch (error) { + handle_error(error); + } + }); + prog.parse(process.argv, { unknown: (arg) => `Unknown option: ${arg}` }); /** @param {number} port */ diff --git a/packages/kit/src/core/load_config/index.spec.js b/packages/kit/src/core/load_config/index.spec.js index 4678a7300126..dd992daa06b4 100644 --- a/packages/kit/src/core/load_config/index.spec.js +++ b/packages/kit/src/core/load_config/index.spec.js @@ -27,6 +27,13 @@ test('fills in defaults', () => { host: null, hostHeader: null, hydrate: true, + package: { + dir: 'package', + entries: { + include: ['**'], + exclude: [] + } + }, paths: { base: '', assets: '/.' @@ -111,6 +118,13 @@ test('fills in partial blanks', () => { host: null, hostHeader: null, hydrate: true, + package: { + dir: 'package', + entries: { + include: ['**'], + exclude: [] + } + }, paths: { base: '', assets: '/.' diff --git a/packages/kit/src/core/load_config/options.js b/packages/kit/src/core/load_config/options.js index 8c63e9748885..d8d94d59ec34 100644 --- a/packages/kit/src/core/load_config/options.js +++ b/packages/kit/src/core/load_config/options.js @@ -80,6 +80,36 @@ const options = { hydrate: expect_boolean(true), + package: { + type: 'branch', + children: { + dir: expect_string('package'), + entries: { + type: 'branch', + children: { + include: { + type: 'leaf', + default: ['**'], + validate: (option, keypath) => { + if (!Array.isArray(option) || !option.every((glob) => typeof glob === 'string')) { + throw new Error(`${keypath} must be an array of strings`); + } + } + }, + exclude: { + type: 'leaf', + default: [], + validate: (option, keypath) => { + if (!Array.isArray(option) || !option.every((glob) => typeof glob === 'string')) { + throw new Error(`${keypath} must be an array of strings`); + } + } + } + } + } + } + }, + paths: { type: 'branch', children: { diff --git a/packages/kit/src/core/load_config/test/index.js b/packages/kit/src/core/load_config/test/index.js index c86f90e55307..eaa2d0a33683 100644 --- a/packages/kit/src/core/load_config/test/index.js +++ b/packages/kit/src/core/load_config/test/index.js @@ -37,6 +37,13 @@ async function testLoadDefaultConfig(path) { host: null, hostHeader: null, hydrate: true, + package: { + dir: 'package', + entries: { + include: ['**'], + exclude: [] + } + }, paths: { base: '', assets: '/.' }, prerender: { crawl: true, enabled: true, force: false, pages: ['*'] }, router: true, diff --git a/packages/kit/src/core/make_package/index.js b/packages/kit/src/core/make_package/index.js new file mode 100644 index 000000000000..32513cea50e9 --- /dev/null +++ b/packages/kit/src/core/make_package/index.js @@ -0,0 +1,120 @@ +import * as fs from 'fs'; +import * as path from 'path'; +import { preprocess } from 'svelte/compiler'; +import globrex from 'globrex'; +import { mkdirp } from '../filesystem'; + +/** + * @param {import('types/config').ValidatedConfig} config + * @param {string} cwd + */ +export async function make_package(config, cwd = process.cwd()) { + const include = config.kit.package.entries.include.map((str) => globrex(str)); + const exclude = config.kit.package.entries.exclude.map((str) => globrex(str)); + + 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} */ + exports: { + './package.json': './package.json' + } + }; + + for (const file of files) { + 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 = (await preprocess(source, config.preprocess, { filename })).code; + } else if (ext === '.ts' && !file.endsWith('.d.ts')) { + // TODO transpile TS file and emit types + throw new Error('TODO transpile TS and emit types'); + } else { + out_file = file; + out_contents = source; + } + + write(path.join(config.kit.package.dir, out_file), out_contents); + + const is_entry = + include.some((glob) => glob.regex.test(file)) && + !exclude.some((glob) => glob.regex.test(file)); + + if (is_entry) { + 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(config.kit.package.dir, 'package.json'), JSON.stringify(package_pkg, null, ' ')); +} + +/** @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); +} diff --git a/packages/kit/types/config.d.ts b/packages/kit/types/config.d.ts index cd3d7e54ade4..35f340a9c397 100644 --- a/packages/kit/types/config.d.ts +++ b/packages/kit/types/config.d.ts @@ -44,6 +44,13 @@ export type Config = { host?: string; hostHeader?: string; hydrate?: boolean; + package?: { + dir?: string; + entries?: { + include?: string[]; + exclude?: string[]; + }; + }; paths?: { base?: string; assets?: string; @@ -83,6 +90,13 @@ export type ValidatedConfig = { host: string; hostHeader: string; hydrate: boolean; + package: { + dir: string; + entries: { + include: string[]; + exclude: string[]; + }; + }; paths: { base: string; assets: string; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index d1295d54d0a0..25170aea0359 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -199,10 +199,10 @@ importers: packages/kit: specifiers: '@rollup/plugin-replace': ^2.4.2 - '@sveltejs/kit': workspace:* '@sveltejs/vite-plugin-svelte': ^1.0.0-next.10 '@types/amphtml-validator': ^1.0.1 '@types/cookie': ^0.4.0 + '@types/globrex': ^0.1.0 '@types/marked': ^2.0.2 '@types/mime': ^2.0.3 '@types/node': ^14.14.43 @@ -213,6 +213,7 @@ importers: cookie: ^0.4.1 devalue: ^2.0.1 eslint: ^7.25.0 + globrex: ^0.1.2 kleur: ^4.1.4 marked: ^2.0.3 node-fetch: ^3.0.0-beta.9 @@ -235,9 +236,9 @@ importers: vite: 2.3.1 devDependencies: '@rollup/plugin-replace': 2.4.2_rollup@2.47.0 - '@sveltejs/kit': 'link:' '@types/amphtml-validator': 1.0.1 '@types/cookie': 0.4.0 + '@types/globrex': 0.1.0 '@types/marked': 2.0.2 '@types/mime': 2.0.3 '@types/node': 14.14.43 @@ -247,6 +248,7 @@ importers: cookie: 0.4.1 devalue: 2.0.1 eslint: 7.25.0 + globrex: 0.1.2 kleur: 4.1.4 marked: 2.0.3 node-fetch: 3.0.0-beta.9 @@ -682,6 +684,10 @@ packages: '@types/node': 14.14.43 dev: true + /@types/globrex/0.1.0: + resolution: {integrity: sha512-aBkxDgp/UbnluE+CIT3V3PoNewwOlLCzXSF3ipD86Slv8xVjwxrDAfSGbsfGgMzPo/fEMPXc+gNUJbtiugwfoA==} + dev: true + /@types/istanbul-lib-coverage/2.0.3: resolution: {integrity: sha512-sz7iLqvVUg1gIedBOvlkxPlc8/uVzyS5OwGz1cKjXzkl3FpL3al0crU8YGU1WoHkxn0Wxbw5tyi6hvzJKNzFsw==} dev: true From e6fd416fb19f52c16a488b4c06b87b55511d0a43 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Wed, 19 May 2021 23:46:47 -0400 Subject: [PATCH 02/13] include/exclude files as well --- .../kit/src/core/load_config/index.spec.js | 12 ++++- packages/kit/src/core/load_config/options.js | 46 +++++++++++-------- .../kit/src/core/load_config/test/index.js | 6 ++- packages/kit/src/core/make_package/index.js | 41 +++++++++++++---- packages/kit/types/config.d.ts | 12 ++++- 5 files changed, 83 insertions(+), 34 deletions(-) diff --git a/packages/kit/src/core/load_config/index.spec.js b/packages/kit/src/core/load_config/index.spec.js index dd992daa06b4..a0b2fb527edc 100644 --- a/packages/kit/src/core/load_config/index.spec.js +++ b/packages/kit/src/core/load_config/index.spec.js @@ -29,7 +29,11 @@ test('fills in defaults', () => { hydrate: true, package: { dir: 'package', - entries: { + exports: { + include: ['**'], + exclude: [] + }, + files: { include: ['**'], exclude: [] } @@ -120,7 +124,11 @@ test('fills in partial blanks', () => { hydrate: true, package: { dir: 'package', - entries: { + exports: { + include: ['**'], + exclude: [] + }, + files: { include: ['**'], exclude: [] } diff --git a/packages/kit/src/core/load_config/options.js b/packages/kit/src/core/load_config/options.js index d8d94d59ec34..5775d2219e5f 100644 --- a/packages/kit/src/core/load_config/options.js +++ b/packages/kit/src/core/load_config/options.js @@ -84,27 +84,18 @@ const options = { type: 'branch', children: { dir: expect_string('package'), - entries: { + exports: { type: 'branch', children: { - include: { - type: 'leaf', - default: ['**'], - validate: (option, keypath) => { - if (!Array.isArray(option) || !option.every((glob) => typeof glob === 'string')) { - throw new Error(`${keypath} must be an array of strings`); - } - } - }, - exclude: { - type: 'leaf', - default: [], - validate: (option, keypath) => { - if (!Array.isArray(option) || !option.every((glob) => typeof glob === 'string')) { - throw new Error(`${keypath} must be an array of strings`); - } - } - } + include: expect_array_of_strings(['**']), + exclude: expect_array_of_strings([]) + } + }, + files: { + type: 'branch', + children: { + include: expect_array_of_strings(['**']), + exclude: expect_array_of_strings([]) } } } @@ -201,6 +192,23 @@ function expect_string(string, allow_empty = true) { }; } +/** + * @param {string[]} array + * @returns {ConfigDefinition} + */ +function expect_array_of_strings(array) { + return { + type: 'leaf', + default: array, + validate: (option, keypath) => { + if (!Array.isArray(option) || !option.every((glob) => typeof glob === 'string')) { + throw new Error(`${keypath} must be an array of strings`); + } + return option; + } + }; +} + /** * @param {boolean} boolean * @returns {ConfigDefinition} diff --git a/packages/kit/src/core/load_config/test/index.js b/packages/kit/src/core/load_config/test/index.js index eaa2d0a33683..7b4e8f257240 100644 --- a/packages/kit/src/core/load_config/test/index.js +++ b/packages/kit/src/core/load_config/test/index.js @@ -39,7 +39,11 @@ async function testLoadDefaultConfig(path) { hydrate: true, package: { dir: 'package', - entries: { + exports: { + include: ['**'], + exclude: [] + }, + files: { include: ['**'], exclude: [] } diff --git a/packages/kit/src/core/make_package/index.js b/packages/kit/src/core/make_package/index.js index 32513cea50e9..11ad3de548a6 100644 --- a/packages/kit/src/core/make_package/index.js +++ b/packages/kit/src/core/make_package/index.js @@ -2,15 +2,17 @@ import * as fs from 'fs'; import * as path from 'path'; import { preprocess } from 'svelte/compiler'; import globrex from 'globrex'; -import { mkdirp } from '../filesystem'; +import { mkdirp, rimraf } from '../filesystem'; /** * @param {import('types/config').ValidatedConfig} config * @param {string} cwd */ export async function make_package(config, cwd = process.cwd()) { - const include = config.kit.package.entries.include.map((str) => globrex(str)); - const exclude = config.kit.package.entries.exclude.map((str) => globrex(str)); + 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); @@ -39,6 +41,9 @@ export async function make_package(config, cwd = process.cwd()) { }; for (const file of files) { + console.log(file, files_filter(file)); + if (!files_filter(file)) continue; + const filename = path.join(config.kit.files.lib, file); const source = fs.readFileSync(filename, 'utf8'); @@ -64,13 +69,9 @@ export async function make_package(config, cwd = process.cwd()) { out_contents = source; } - write(path.join(config.kit.package.dir, out_file), out_contents); - - const is_entry = - include.some((glob) => glob.regex.test(file)) && - !exclude.some((glob) => glob.regex.test(file)); + write(path.join(cwd, config.kit.package.dir, out_file), out_contents); - if (is_entry) { + if (exports_filter(file)) { const entry = `./${out_file}`; package_pkg.exports[entry] = entry; } @@ -82,7 +83,27 @@ export async function make_package(config, cwd = process.cwd()) { package_pkg.exports['.'] = main; } - write(path.join(config.kit.package.dir, 'package.json'), JSON.stringify(package_pkg, null, ' ')); + write( + path.join(cwd, config.kit.package.dir, 'package.json'), + JSON.stringify(package_pkg, null, ' ') + ); +} + +/** + * @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 */ diff --git a/packages/kit/types/config.d.ts b/packages/kit/types/config.d.ts index 35f340a9c397..375b8258aa39 100644 --- a/packages/kit/types/config.d.ts +++ b/packages/kit/types/config.d.ts @@ -46,7 +46,11 @@ export type Config = { hydrate?: boolean; package?: { dir?: string; - entries?: { + exports?: { + include?: string[]; + exclude?: string[]; + }; + files?: { include?: string[]; exclude?: string[]; }; @@ -92,7 +96,11 @@ export type ValidatedConfig = { hydrate: boolean; package: { dir: string; - entries: { + exports: { + include: string[]; + exclude: string[]; + }; + files: { include: string[]; exclude: string[]; }; From a70968a39dfb597d867b4fce6c43d1e50bf385dd Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Thu, 20 May 2021 00:06:29 -0400 Subject: [PATCH 03/13] make typescript happy --- packages/kit/package.json | 1 + packages/kit/test/types.d.ts | 2 +- packages/kit/tsconfig.json | 2 +- pnpm-lock.yaml | 6 ++++++ 4 files changed, 9 insertions(+), 2 deletions(-) diff --git a/packages/kit/package.json b/packages/kit/package.json index 19f34eb8c062..2883a4827ec4 100644 --- a/packages/kit/package.json +++ b/packages/kit/package.json @@ -24,6 +24,7 @@ "eslint": "^7.25.0", "globrex": "^0.1.2", "kleur": "^4.1.4", + "locate-character": "^2.0.5", "marked": "^2.0.3", "node-fetch": "^3.0.0-beta.9", "port-authority": "^1.1.2", diff --git a/packages/kit/test/types.d.ts b/packages/kit/test/types.d.ts index 5214190d8dac..f612487e9d2e 100644 --- a/packages/kit/test/types.d.ts +++ b/packages/kit/test/types.d.ts @@ -1,4 +1,4 @@ -/// +/// import { Page, Response as PlaywrightResponse } from 'playwright-chromium'; import { RequestInfo, RequestInit, Response as NodeFetchResponse } from 'node-fetch'; diff --git a/packages/kit/tsconfig.json b/packages/kit/tsconfig.json index 0d8a30793399..e8904ff2aaea 100644 --- a/packages/kit/tsconfig.json +++ b/packages/kit/tsconfig.json @@ -11,7 +11,7 @@ "paths": { "test": ["./test/types"], "types/*": ["./types/*"], - "@sveltejs/kit": ["../types"] + "@sveltejs/kit": ["./types/index"] } }, "include": ["src/**/*", "test/**/*", "types/**/*", "test/types.d.ts", "test/ambient.d.ts"] diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 25170aea0359..7d6167e54c5d 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -215,6 +215,7 @@ importers: eslint: ^7.25.0 globrex: ^0.1.2 kleur: ^4.1.4 + locate-character: ^2.0.5 marked: ^2.0.3 node-fetch: ^3.0.0-beta.9 port-authority: ^1.1.2 @@ -250,6 +251,7 @@ importers: eslint: 7.25.0 globrex: 0.1.2 kleur: 4.1.4 + locate-character: 2.0.5 marked: 2.0.3 node-fetch: 3.0.0-beta.9 port-authority: 1.1.2 @@ -2345,6 +2347,10 @@ packages: strip-bom: 3.0.0 dev: true + /locate-character/2.0.5: + resolution: {integrity: sha512-n2GmejDXtOPBAZdIiEFy5dJ5N38xBCXLNOtw2WpB9kGh6pnrEuKlwYI+Tkpofc4wDtVXHtoAOJaMRlYG/oYaxg==} + dev: true + /locate-path/2.0.0: resolution: {integrity: sha1-K1aLJl7slExtnA3pw9u7ygNUzY4=} engines: {node: '>=4'} From ac45fac305feff11e024424eb6c532332ca966cb Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Thu, 20 May 2021 00:21:01 -0400 Subject: [PATCH 04/13] tidy up --- packages/kit/src/core/make_package/index.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/kit/src/core/make_package/index.js b/packages/kit/src/core/make_package/index.js index 11ad3de548a6..868478c8e73d 100644 --- a/packages/kit/src/core/make_package/index.js +++ b/packages/kit/src/core/make_package/index.js @@ -41,7 +41,6 @@ export async function make_package(config, cwd = process.cwd()) { }; for (const file of files) { - console.log(file, files_filter(file)); if (!files_filter(file)) continue; const filename = path.join(config.kit.files.lib, file); @@ -60,7 +59,9 @@ export async function make_package(config, cwd = process.cwd()) { // it's a Svelte component // TODO how to emit types? out_file = file.slice(0, -svelte_ext.length) + '.svelte'; - out_contents = (await preprocess(source, config.preprocess, { filename })).code; + 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 throw new Error('TODO transpile TS and emit types'); From 3e66d9d74ab26cdc8e5d25ba5363fe5567751e87 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Fri, 21 May 2021 13:10:46 -0400 Subject: [PATCH 05/13] improve error message --- packages/kit/src/core/make_package/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/kit/src/core/make_package/index.js b/packages/kit/src/core/make_package/index.js index 868478c8e73d..48bf6d8386be 100644 --- a/packages/kit/src/core/make_package/index.js +++ b/packages/kit/src/core/make_package/index.js @@ -64,7 +64,8 @@ export async function make_package(config, cwd = process.cwd()) { : source; } else if (ext === '.ts' && !file.endsWith('.d.ts')) { // TODO transpile TS file and emit types - throw new Error('TODO transpile TS 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; From 2a5bb3a8d27600986045616b1774576e8a3595fd Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Fri, 21 May 2021 13:20:03 -0400 Subject: [PATCH 06/13] exclude underscore-prefixed files from exports by default --- packages/kit/src/core/load_config/options.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/kit/src/core/load_config/options.js b/packages/kit/src/core/load_config/options.js index 5775d2219e5f..c763465039e9 100644 --- a/packages/kit/src/core/load_config/options.js +++ b/packages/kit/src/core/load_config/options.js @@ -88,7 +88,7 @@ const options = { type: 'branch', children: { include: expect_array_of_strings(['**']), - exclude: expect_array_of_strings([]) + exclude: expect_array_of_strings(['_*', '**/_*']) } }, files: { From 5f16809b359c56aff0a6243d723ac91cf4eecec9 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Fri, 21 May 2021 14:45:44 -0400 Subject: [PATCH 07/13] move files --- documentation/docs/{12-cli.md => 13-cli.md} | 0 documentation/docs/{13-configuration.md => 14-configuration.md} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename documentation/docs/{12-cli.md => 13-cli.md} (100%) rename documentation/docs/{13-configuration.md => 14-configuration.md} (100%) diff --git a/documentation/docs/12-cli.md b/documentation/docs/13-cli.md similarity index 100% rename from documentation/docs/12-cli.md rename to documentation/docs/13-cli.md diff --git a/documentation/docs/13-configuration.md b/documentation/docs/14-configuration.md similarity index 100% rename from documentation/docs/13-configuration.md rename to documentation/docs/14-configuration.md From aa8a509b590ff07f5bfcd579effc2907ac401426 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Fri, 21 May 2021 15:31:13 -0400 Subject: [PATCH 08/13] add docs --- documentation/docs/12-packaging.md | 44 ++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 documentation/docs/12-packaging.md diff --git a/documentation/docs/12-packaging.md b/documentation/docs/12-packaging.md new file mode 100644 index 000000000000..d9e33ecd5528 --- /dev/null +++ b/documentation/docs/12-packaging.md @@ -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 From 5f21dc675a55bede3b86b5ebafa058a46c7cf0e0 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Fri, 21 May 2021 15:31:47 -0400 Subject: [PATCH 09/13] changeset --- .changeset/four-pillows-give.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/four-pillows-give.md diff --git a/.changeset/four-pillows-give.md b/.changeset/four-pillows-give.md new file mode 100644 index 000000000000..6043eaaa70cb --- /dev/null +++ b/.changeset/four-pillows-give.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +Add svelte-kit package command From d3853d0e1029a1edf65f2f24c69e93be1ead7634 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Fri, 21 May 2021 15:32:19 -0400 Subject: [PATCH 10/13] gitignore package directory --- .changeset/twelve-feet-deny.md | 5 +++++ packages/create-svelte/templates/default/.gitignore | 3 +-- packages/create-svelte/templates/skeleton/.gitignore | 3 +-- 3 files changed, 7 insertions(+), 4 deletions(-) create mode 100644 .changeset/twelve-feet-deny.md diff --git a/.changeset/twelve-feet-deny.md b/.changeset/twelve-feet-deny.md new file mode 100644 index 000000000000..25e40588a3c6 --- /dev/null +++ b/.changeset/twelve-feet-deny.md @@ -0,0 +1,5 @@ +--- +'create-svelte': patch +--- + +gitignore package directory diff --git a/packages/create-svelte/templates/default/.gitignore b/packages/create-svelte/templates/default/.gitignore index 0a4623184983..2d66ec056330 100644 --- a/packages/create-svelte/templates/default/.gitignore +++ b/packages/create-svelte/templates/default/.gitignore @@ -1,5 +1,4 @@ .DS_Store node_modules /.svelte-kit -/build -/functions +/package diff --git a/packages/create-svelte/templates/skeleton/.gitignore b/packages/create-svelte/templates/skeleton/.gitignore index 0a4623184983..2d66ec056330 100644 --- a/packages/create-svelte/templates/skeleton/.gitignore +++ b/packages/create-svelte/templates/skeleton/.gitignore @@ -1,5 +1,4 @@ .DS_Store node_modules /.svelte-kit -/build -/functions +/package From 21b00edaef5250267d9f105026bfc7e5b862bdce Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Fri, 21 May 2021 15:37:13 -0400 Subject: [PATCH 11/13] update lockfile, who knows why --- pnpm-lock.yaml | 96 ++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 86 insertions(+), 10 deletions(-) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 7d6167e54c5d..90326d06c5b2 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -187,10 +187,10 @@ importers: '@lukeed/uuid': 2.0.0 cookie: 0.4.1 devDependencies: - '@sveltejs/adapter-cloudflare-workers': link:../../../adapter-cloudflare-workers - '@sveltejs/adapter-netlify': link:../../../adapter-netlify - '@sveltejs/adapter-vercel': link:../../../adapter-vercel - '@sveltejs/kit': link:../../../kit + '@sveltejs/adapter-cloudflare-workers': 1.0.0-next.9_@sveltejs+kit@1.0.0-next.109 + '@sveltejs/adapter-netlify': 1.0.0-next.14_@sveltejs+kit@1.0.0-next.109 + '@sveltejs/adapter-vercel': 1.0.0-next.19_@sveltejs+kit@1.0.0-next.109 + '@sveltejs/kit': 1.0.0-next.109_svelte@3.38.2 svelte: 3.38.2 svelte-preprocess: 4.7.3_svelte@3.38.2+typescript@4.2.4 typescript: 4.2.4 @@ -501,6 +501,10 @@ packages: resolution: {integrity: sha512-t2WRThg+eLkQNQCtPG2sCCq40lz3xeb7nsL7P8l4+wfSRbdLQXAY5IebMftI2YEZR4MRRhdgrg0p5fi/2yXypA==} dev: false + /@iarna/toml/2.2.5: + resolution: {integrity: sha512-trnsAYxU3xnS1gPHPyU961coFyLkh4gAD/0zQ5mymY4yOZ+CYvsPqUbOFSw0aDM4y0tV7tiFxL/1XfXPNC6IPg==} + dev: true + /@istanbuljs/schema/0.1.3: resolution: {integrity: sha512-ZXRY4jNvVgSVQ8DL3LTcakaAtXwTVUxE81hslsyD2AtoXW/wVob10HkOJ1X/pAlcI7D+2YoZKg5do8G/w6RYgA==} engines: {node: '>=8'} @@ -624,6 +628,16 @@ packages: rollup: 2.47.0 dev: true + /@rollup/pluginutils/4.1.0: + resolution: {integrity: sha512-TrBhfJkFxA+ER+ew2U2/fHbebhLT/l/2pRk0hfj9KusXUuRXd2v0R58AfaZK9VXDQ4TogOSEmICVrQAA3zFnHQ==} + engines: {node: '>= 8.0.0'} + peerDependencies: + rollup: ^1.20.0||^2.0.0 + dependencies: + estree-walker: 2.0.2 + picomatch: 2.2.3 + dev: true + /@rollup/pluginutils/4.1.0_rollup@2.47.0: resolution: {integrity: sha512-TrBhfJkFxA+ER+ew2U2/fHbebhLT/l/2pRk0hfj9KusXUuRXd2v0R58AfaZK9VXDQ4TogOSEmICVrQAA3zFnHQ==} engines: {node: '>= 8.0.0'} @@ -635,6 +649,52 @@ packages: rollup: 2.47.0 dev: false + /@sveltejs/adapter-cloudflare-workers/1.0.0-next.9_@sveltejs+kit@1.0.0-next.109: + resolution: {integrity: sha512-jQtjGvX++XrgyaRVW7wKY9liIa5l/PW8r1rcM6V/ZYLrTmJ0UJON//fIOp8LH0zeOHLD1FqtNdo4j6PCOPknDw==} + peerDependencies: + '@sveltejs/kit': 1.0.0-next.109 + dependencies: + '@iarna/toml': 2.2.5 + '@sveltejs/kit': 1.0.0-next.109_svelte@3.38.2 + esbuild: 0.11.20 + dev: true + + /@sveltejs/adapter-netlify/1.0.0-next.14_@sveltejs+kit@1.0.0-next.109: + resolution: {integrity: sha512-TIyiFRYjEFYTKEQuht5Ln8HnzBRCVHUJwG5npigJjlVdwyuENXi9B/kO6gJ9I5/Hg49Fs7lc5crBlL6XHezAqw==} + peerDependencies: + '@sveltejs/kit': 1.0.0-next.109 + dependencies: + '@iarna/toml': 2.2.5 + '@sveltejs/kit': 1.0.0-next.109_svelte@3.38.2 + esbuild: 0.11.20 + dev: true + + /@sveltejs/adapter-vercel/1.0.0-next.19_@sveltejs+kit@1.0.0-next.109: + resolution: {integrity: sha512-A2fTQDTTARahncaW5qP5sOyyw3xrCFj4rORw9dh819AvpajzO5PLwmQiAm2Fj2D8hrG7cnU0c30g89RsPsQ+OA==} + peerDependencies: + '@sveltejs/kit': 1.0.0-next.109 + dependencies: + '@sveltejs/kit': 1.0.0-next.109_svelte@3.38.2 + esbuild: 0.11.20 + dev: true + + /@sveltejs/kit/1.0.0-next.109_svelte@3.38.2: + resolution: {integrity: sha512-72iHsgcZTj9WU2VMq/qzMdFidTrSlJ1+KE0Iiw43Gee8TkKi5tMOyeu/f8lWa4HzdHLFZ2CJdvmcL7w3F4SWjg==} + engines: {node: ^12.20 || ^14.13.1 || >= 16} + hasBin: true + peerDependencies: + svelte: ^3.38.2 + dependencies: + '@sveltejs/vite-plugin-svelte': 1.0.0-next.10_svelte@3.38.2+vite@2.3.1 + cheap-watch: 1.0.3 + sade: 1.7.4 + svelte: 3.38.2 + vite: 2.3.1 + transitivePeerDependencies: + - rollup + - supports-color + dev: true + /@sveltejs/vite-plugin-svelte/1.0.0-next.10_fa3f9f81b9199d842185ad66f92f1626: resolution: {integrity: sha512-ImvxbhPePm2hWNTKBSA3LHAYGwiEjHjvvgfPLXm4R87sfZ+BMXql9jBmDpzUC/URBLT4BB3Jxos/i523qkJBHg==} engines: {node: '>=12.0.0'} @@ -657,6 +717,28 @@ packages: - supports-color dev: false + /@sveltejs/vite-plugin-svelte/1.0.0-next.10_svelte@3.38.2+vite@2.3.1: + resolution: {integrity: sha512-ImvxbhPePm2hWNTKBSA3LHAYGwiEjHjvvgfPLXm4R87sfZ+BMXql9jBmDpzUC/URBLT4BB3Jxos/i523qkJBHg==} + engines: {node: '>=12.0.0'} + peerDependencies: + svelte: ^3.37.0 + vite: ^2.2.3 + dependencies: + '@rollup/pluginutils': 4.1.0 + chalk: 4.1.1 + debug: 4.3.2 + hash-sum: 2.0.0 + require-relative: 0.8.7 + slash: 4.0.0 + source-map: 0.7.3 + svelte: 3.38.2 + svelte-hmr: 0.14.3_svelte@3.38.2 + vite: 2.3.1 + transitivePeerDependencies: + - rollup + - supports-color + dev: true + /@types/amphtml-validator/1.0.1: resolution: {integrity: sha512-DWE7fy6KtC+Uw0KV/HAmjuH2GB/o8yskXlvmVWR7mOVsLDybp+XrwkzEeRFU9wGjWKeRMBNGsx+5DRq7sUsAwA==} dependencies: @@ -1187,7 +1269,6 @@ packages: /cheap-watch/1.0.3: resolution: {integrity: sha512-xC5CruMhLzjPwJ5ecUxGu1uGmwJQykUhqd2QrCrYbwvsFYdRyviu6jG9+pccwDXJR/OpmOTOJ9yLFunVgQu9wg==} engines: {node: '>=8'} - dev: false /chokidar/3.5.1: resolution: {integrity: sha512-9+s+Od+W0VJJzawDma/gvBNQqkTiqYTWLuZoyAsivsI4AaWTCzHG06/TMjsf1cYe9Cb97UCEhjz7HvnPk2p/tw==} @@ -1390,7 +1471,6 @@ packages: optional: true dependencies: ms: 2.1.2 - dev: false /decamelize-keys/1.1.0: resolution: {integrity: sha1-0XGoeTMlKAfrPLYdwcFEXQeN8tk=} @@ -2023,7 +2103,6 @@ packages: /hash-sum/2.0.0: resolution: {integrity: sha512-WdZTbAByD+pHfl/g9QSsBIIwy8IT+EsPiKDs0KNX+zSHhdDLFKdZu0BQHljvO+0QI/BasbMSUa8wYNCZTvhslg==} - dev: false /hosted-git-info/2.8.9: resolution: {integrity: sha512-mxIDAb9Lsm6DoOJ7xH+5+X4y1LU/4Hi50L9C5sIswK3JzULS4bwk1FvjdBgvYR4bzT4tuUQiC15FE2f5HbLvYw==} @@ -3058,7 +3137,6 @@ packages: /require-relative/0.8.7: resolution: {integrity: sha1-eZlTn8ngR6N5KPoZb44VY9q9Nt4=} - dev: false /resolve-from/4.0.0: resolution: {integrity: sha512-pb/MYmXstAkysRFx8piNI1tGFNQIFA3vkE3Gq4EuA1dF6gHp/+vgZqsCGJapvy8N3Q+4o7FwvquPJcnZ7RYy4g==} @@ -3197,7 +3275,6 @@ packages: /slash/4.0.0: resolution: {integrity: sha512-3dOsAHXXUkQTpOYcoAxLIorMTp4gIQr5IW3iVb7A7lFIp0VHhnynm9izx6TssdrIcVIESAlVjtnO2K8bg+Coew==} engines: {node: '>=12'} - dev: false /slice-ansi/4.0.0: resolution: {integrity: sha512-qMCMfhY040cVHT43K9BFygqYbUPFZKHOg7K73mtTWJRb8pyP3fzf4Ixd5SzdEJQ6MRUg/WBnOLxghZtKKurENQ==} @@ -3405,7 +3482,6 @@ packages: svelte: '>=3.19.0' dependencies: svelte: 3.38.2 - dev: false /svelte-preprocess/4.7.3_svelte@3.38.2+typescript@4.2.4: resolution: {integrity: sha512-Zx1/xLeGOIBlZMGPRCaXtlMe4ZA0faato5Dc3CosEqwu75MIEPuOstdkH6cy+RYTUYynoxzNaDxkPX4DbrPwRA==} From f7d1ad496ebc6e5afdd3463c728e72541f16a208 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Fri, 21 May 2021 15:37:19 -0400 Subject: [PATCH 12/13] update unit tests --- packages/kit/src/core/load_config/index.spec.js | 4 ++-- packages/kit/src/core/load_config/test/index.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/kit/src/core/load_config/index.spec.js b/packages/kit/src/core/load_config/index.spec.js index a0b2fb527edc..d1138752f849 100644 --- a/packages/kit/src/core/load_config/index.spec.js +++ b/packages/kit/src/core/load_config/index.spec.js @@ -31,7 +31,7 @@ test('fills in defaults', () => { dir: 'package', exports: { include: ['**'], - exclude: [] + exclude: ['_*', '**/_*'] }, files: { include: ['**'], @@ -126,7 +126,7 @@ test('fills in partial blanks', () => { dir: 'package', exports: { include: ['**'], - exclude: [] + exclude: ['_*', '**/_*'] }, files: { include: ['**'], diff --git a/packages/kit/src/core/load_config/test/index.js b/packages/kit/src/core/load_config/test/index.js index 7b4e8f257240..eb41d36ad697 100644 --- a/packages/kit/src/core/load_config/test/index.js +++ b/packages/kit/src/core/load_config/test/index.js @@ -41,7 +41,7 @@ async function testLoadDefaultConfig(path) { dir: 'package', exports: { include: ['**'], - exclude: [] + exclude: ['_*', '**/_*'] }, files: { include: ['**'], From a23a33ab43f6ce48f5e3a3bd4fd25322f4c30c6f Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Fri, 21 May 2021 19:05:23 -0400 Subject: [PATCH 13/13] copy over readme --- packages/kit/src/core/make_package/index.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/packages/kit/src/core/make_package/index.js b/packages/kit/src/core/make_package/index.js index 48bf6d8386be..c081bb5dd1f9 100644 --- a/packages/kit/src/core/make_package/index.js +++ b/packages/kit/src/core/make_package/index.js @@ -89,6 +89,13 @@ export async function make_package(config, cwd = process.cwd()) { 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); + } } /**