diff --git a/.changeset/unlucky-planets-battle.md b/.changeset/unlucky-planets-battle.md new file mode 100644 index 000000000000..974612bf6146 --- /dev/null +++ b/.changeset/unlucky-planets-battle.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +Preserve README casing and package.json contents on svelte-kit package diff --git a/documentation/docs/12-packaging.md b/documentation/docs/12-packaging.md index 86acf5d64bb0..a0bc69492301 100644 --- a/documentation/docs/12-packaging.md +++ b/documentation/docs/12-packaging.md @@ -12,7 +12,7 @@ Running `svelte-kit package` will take the contents of `src/lib` and generate a - 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. 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`, `files` and `publishConfig` fields from the root of the project, and adds a `"type": "module"` and an `"exports"` field +- A `package.json` copied from the project root without the `"scripts"` field and adds a `"type": "module"`. An `"exports"` field will also be added if it's not defined in the original file. 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. diff --git a/packages/kit/src/core/make_package/index.js b/packages/kit/src/core/make_package/index.js index 0980533e8f7e..772cb5236d57 100644 --- a/packages/kit/src/core/make_package/index.js +++ b/packages/kit/src/core/make_package/index.js @@ -29,28 +29,15 @@ export async function make_package(config, cwd = process.cwd()) { 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, - files: pkg.files, - publishConfig: pkg.publishConfig, - type: 'module', - /** @type {Record} */ - exports: { - './package.json': './package.json' - } - }; + delete pkg.scripts; + pkg.type = 'module'; // type must be 'module' + + const user_defined_exports = 'exports' in pkg; + + // We still want to always predefine some exports + // like package.json that is used by other packages + if (!pkg.exports) pkg.exports = {}; + pkg.exports['./package.json'] = './package.json'; for (const file of files) { if (!files_filter(file)) continue; @@ -94,22 +81,19 @@ export async function make_package(config, cwd = process.cwd()) { 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; + if (!user_defined_exports && exports_filter(file)) { + const entry = `./${out_file.replace(/\\/g, '/')}`; + pkg.exports[entry] = entry; } } - const main = package_pkg.exports['./index.js'] || package_pkg.exports['./index.svelte']; + const main = pkg.exports['./index.js'] || pkg.exports['./index.svelte']; - if (main) { - package_pkg.exports['.'] = main; + if (!user_defined_exports && main) { + pkg.exports['.'] = main; } - write( - path.join(cwd, config.kit.package.dir, 'package.json'), - JSON.stringify(package_pkg, null, ' ') - ); + write(path.join(cwd, config.kit.package.dir, 'package.json'), JSON.stringify(pkg, null, ' ')); const whitelist = fs.readdirSync(cwd).filter((file) => { const lowercased = file.toLowerCase(); @@ -120,8 +104,7 @@ export async function make_package(config, cwd = process.cwd()) { if (fs.lstatSync(full_path).isDirectory()) continue; // just to be sure const package_path = path.join(cwd, config.kit.package.dir, pathname); - if (fs.existsSync(package_path)) continue; - fs.copyFileSync(full_path, package_path); + if (!fs.existsSync(package_path)) fs.copyFileSync(full_path, package_path); } } diff --git a/packages/kit/src/core/make_package/test/fixtures/casing/ReadMe.md b/packages/kit/src/core/make_package/test/fixtures/casing/ReadMe.md new file mode 100644 index 000000000000..e965047ad7c5 --- /dev/null +++ b/packages/kit/src/core/make_package/test/fixtures/casing/ReadMe.md @@ -0,0 +1 @@ +Hello diff --git a/packages/kit/src/core/make_package/test/fixtures/casing/expected/ReadMe.md b/packages/kit/src/core/make_package/test/fixtures/casing/expected/ReadMe.md new file mode 100644 index 000000000000..e965047ad7c5 --- /dev/null +++ b/packages/kit/src/core/make_package/test/fixtures/casing/expected/ReadMe.md @@ -0,0 +1 @@ +Hello diff --git a/packages/kit/src/core/make_package/test/fixtures/casing/expected/Test.svelte b/packages/kit/src/core/make_package/test/fixtures/casing/expected/Test.svelte new file mode 100644 index 000000000000..31d118397f9d --- /dev/null +++ b/packages/kit/src/core/make_package/test/fixtures/casing/expected/Test.svelte @@ -0,0 +1,12 @@ + + + diff --git a/packages/kit/src/core/make_package/test/fixtures/casing/expected/Test.svelte.d.ts b/packages/kit/src/core/make_package/test/fixtures/casing/expected/Test.svelte.d.ts new file mode 100644 index 000000000000..8cfa75e62de7 --- /dev/null +++ b/packages/kit/src/core/make_package/test/fixtures/casing/expected/Test.svelte.d.ts @@ -0,0 +1,40 @@ +/** @typedef {typeof __propDef.props} TestProps */ +/** @typedef {typeof __propDef.events} TestEvents */ +/** @typedef {typeof __propDef.slots} TestSlots */ +export default class Test extends SvelteComponentTyped< + { + astring: string; + }, + { + event: CustomEvent; + } & { + [evt: string]: CustomEvent; + }, + { + default: { + astring: string; + }; + } +> { + get astring(): string; +} +export type TestProps = typeof __propDef.props; +export type TestEvents = typeof __propDef.events; +export type TestSlots = typeof __propDef.slots; +import { SvelteComponentTyped } from 'svelte'; +declare const __propDef: { + props: { + astring: string; + }; + events: { + event: CustomEvent; + } & { + [evt: string]: CustomEvent; + }; + slots: { + default: { + astring: string; + }; + }; +}; +export {}; diff --git a/packages/kit/src/core/make_package/test/fixtures/casing/expected/index.d.ts b/packages/kit/src/core/make_package/test/fixtures/casing/expected/index.d.ts new file mode 100644 index 000000000000..4c44188c3648 --- /dev/null +++ b/packages/kit/src/core/make_package/test/fixtures/casing/expected/index.d.ts @@ -0,0 +1 @@ +export { default as Test } from './Test.svelte'; diff --git a/packages/kit/src/core/make_package/test/fixtures/casing/expected/index.js b/packages/kit/src/core/make_package/test/fixtures/casing/expected/index.js new file mode 100644 index 000000000000..4c44188c3648 --- /dev/null +++ b/packages/kit/src/core/make_package/test/fixtures/casing/expected/index.js @@ -0,0 +1 @@ +export { default as Test } from './Test.svelte'; diff --git a/packages/kit/src/core/make_package/test/fixtures/casing/expected/package.json b/packages/kit/src/core/make_package/test/fixtures/casing/expected/package.json new file mode 100644 index 000000000000..d637970e1d6f --- /dev/null +++ b/packages/kit/src/core/make_package/test/fixtures/casing/expected/package.json @@ -0,0 +1,12 @@ +{ + "name": "casing", + "version": "1.0.0", + "description": "package-casing-test", + "type": "module", + "exports": { + "./package.json": "./package.json", + "./index.js": "./index.js", + "./Test.svelte": "./Test.svelte", + ".": "./index.js" + } +} diff --git a/packages/kit/src/core/make_package/test/fixtures/casing/jsconfig.json b/packages/kit/src/core/make_package/test/fixtures/casing/jsconfig.json new file mode 100644 index 000000000000..5417394ebe85 --- /dev/null +++ b/packages/kit/src/core/make_package/test/fixtures/casing/jsconfig.json @@ -0,0 +1,9 @@ +{ + "compilerOptions": { + "checkJs": true, + "paths": { + "$lib/*": ["src/lib/*"] + } + }, + "include": ["src/**/*.d.ts", "src/**/*.js", "src/**/*.svelte"] +} diff --git a/packages/kit/src/core/make_package/test/fixtures/casing/package.json b/packages/kit/src/core/make_package/test/fixtures/casing/package.json new file mode 100644 index 000000000000..858292474b29 --- /dev/null +++ b/packages/kit/src/core/make_package/test/fixtures/casing/package.json @@ -0,0 +1,5 @@ +{ + "name": "casing", + "version": "1.0.0", + "description": "package-casing-test" +} diff --git a/packages/kit/src/core/make_package/test/fixtures/casing/src/app.html b/packages/kit/src/core/make_package/test/fixtures/casing/src/app.html new file mode 100644 index 000000000000..245305c4ef5b --- /dev/null +++ b/packages/kit/src/core/make_package/test/fixtures/casing/src/app.html @@ -0,0 +1,12 @@ + + + + + + + %svelte.head% + + +
%svelte.body%
+ + diff --git a/packages/kit/src/core/make_package/test/fixtures/casing/src/lib/Test.svelte b/packages/kit/src/core/make_package/test/fixtures/casing/src/lib/Test.svelte new file mode 100644 index 000000000000..31d118397f9d --- /dev/null +++ b/packages/kit/src/core/make_package/test/fixtures/casing/src/lib/Test.svelte @@ -0,0 +1,12 @@ + + + diff --git a/packages/kit/src/core/make_package/test/fixtures/casing/src/lib/index.js b/packages/kit/src/core/make_package/test/fixtures/casing/src/lib/index.js new file mode 100644 index 000000000000..4c44188c3648 --- /dev/null +++ b/packages/kit/src/core/make_package/test/fixtures/casing/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/casing/svelte.config.js b/packages/kit/src/core/make_package/test/fixtures/casing/svelte.config.js new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/packages/kit/src/core/make_package/test/fixtures/exports/expected/Test.svelte b/packages/kit/src/core/make_package/test/fixtures/exports/expected/Test.svelte new file mode 100644 index 000000000000..31d118397f9d --- /dev/null +++ b/packages/kit/src/core/make_package/test/fixtures/exports/expected/Test.svelte @@ -0,0 +1,12 @@ + + + diff --git a/packages/kit/src/core/make_package/test/fixtures/exports/expected/Test.svelte.d.ts b/packages/kit/src/core/make_package/test/fixtures/exports/expected/Test.svelte.d.ts new file mode 100644 index 000000000000..8cfa75e62de7 --- /dev/null +++ b/packages/kit/src/core/make_package/test/fixtures/exports/expected/Test.svelte.d.ts @@ -0,0 +1,40 @@ +/** @typedef {typeof __propDef.props} TestProps */ +/** @typedef {typeof __propDef.events} TestEvents */ +/** @typedef {typeof __propDef.slots} TestSlots */ +export default class Test extends SvelteComponentTyped< + { + astring: string; + }, + { + event: CustomEvent; + } & { + [evt: string]: CustomEvent; + }, + { + default: { + astring: string; + }; + } +> { + get astring(): string; +} +export type TestProps = typeof __propDef.props; +export type TestEvents = typeof __propDef.events; +export type TestSlots = typeof __propDef.slots; +import { SvelteComponentTyped } from 'svelte'; +declare const __propDef: { + props: { + astring: string; + }; + events: { + event: CustomEvent; + } & { + [evt: string]: CustomEvent; + }; + slots: { + default: { + astring: string; + }; + }; +}; +export {}; diff --git a/packages/kit/src/core/make_package/test/fixtures/exports/expected/index.d.ts b/packages/kit/src/core/make_package/test/fixtures/exports/expected/index.d.ts new file mode 100644 index 000000000000..4c44188c3648 --- /dev/null +++ b/packages/kit/src/core/make_package/test/fixtures/exports/expected/index.d.ts @@ -0,0 +1 @@ +export { default as Test } from './Test.svelte'; diff --git a/packages/kit/src/core/make_package/test/fixtures/exports/expected/index.js b/packages/kit/src/core/make_package/test/fixtures/exports/expected/index.js new file mode 100644 index 000000000000..4c44188c3648 --- /dev/null +++ b/packages/kit/src/core/make_package/test/fixtures/exports/expected/index.js @@ -0,0 +1 @@ +export { default as Test } from './Test.svelte'; diff --git a/packages/kit/src/core/make_package/test/fixtures/exports/expected/internal/Test.svelte b/packages/kit/src/core/make_package/test/fixtures/exports/expected/internal/Test.svelte new file mode 100644 index 000000000000..06a5a3408e95 --- /dev/null +++ b/packages/kit/src/core/make_package/test/fixtures/exports/expected/internal/Test.svelte @@ -0,0 +1,6 @@ + diff --git a/packages/kit/src/core/make_package/test/fixtures/exports/expected/internal/Test.svelte.d.ts b/packages/kit/src/core/make_package/test/fixtures/exports/expected/internal/Test.svelte.d.ts new file mode 100644 index 000000000000..c787a209ac12 --- /dev/null +++ b/packages/kit/src/core/make_package/test/fixtures/exports/expected/internal/Test.svelte.d.ts @@ -0,0 +1,26 @@ +/** @typedef {typeof __propDef.props} Test2Props */ +/** @typedef {typeof __propDef.events} Test2Events */ +/** @typedef {typeof __propDef.slots} Test2Slots */ +export default class Test2 extends SvelteComponentTyped< + { + foo: boolean; + }, + { + [evt: string]: CustomEvent; + }, + {} +> {} +export type Test2Props = typeof __propDef.props; +export type Test2Events = typeof __propDef.events; +export type Test2Slots = typeof __propDef.slots; +import { SvelteComponentTyped } from 'svelte'; +declare const __propDef: { + props: { + foo: import('./foo').Foo; + }; + events: { + [evt: string]: CustomEvent; + }; + slots: {}; +}; +export {}; diff --git a/packages/kit/src/core/make_package/test/fixtures/exports/expected/internal/foo.d.ts b/packages/kit/src/core/make_package/test/fixtures/exports/expected/internal/foo.d.ts new file mode 100644 index 000000000000..c941ad7b1ce6 --- /dev/null +++ b/packages/kit/src/core/make_package/test/fixtures/exports/expected/internal/foo.d.ts @@ -0,0 +1 @@ +export type Foo = boolean; diff --git a/packages/kit/src/core/make_package/test/fixtures/exports/expected/package.json b/packages/kit/src/core/make_package/test/fixtures/exports/expected/package.json new file mode 100644 index 000000000000..f561f9dbe440 --- /dev/null +++ b/packages/kit/src/core/make_package/test/fixtures/exports/expected/package.json @@ -0,0 +1,12 @@ +{ + "name": "exports", + "version": "1.0.0", + "description": "package-exports-test", + "exports": { + ".": { + "import": "./index.js" + }, + "./package.json": "./package.json" + }, + "type": "module" +} diff --git a/packages/kit/src/core/make_package/test/fixtures/exports/jsconfig.json b/packages/kit/src/core/make_package/test/fixtures/exports/jsconfig.json new file mode 100644 index 000000000000..5417394ebe85 --- /dev/null +++ b/packages/kit/src/core/make_package/test/fixtures/exports/jsconfig.json @@ -0,0 +1,9 @@ +{ + "compilerOptions": { + "checkJs": true, + "paths": { + "$lib/*": ["src/lib/*"] + } + }, + "include": ["src/**/*.d.ts", "src/**/*.js", "src/**/*.svelte"] +} diff --git a/packages/kit/src/core/make_package/test/fixtures/exports/package.json b/packages/kit/src/core/make_package/test/fixtures/exports/package.json new file mode 100644 index 000000000000..7b6755bb7ce3 --- /dev/null +++ b/packages/kit/src/core/make_package/test/fixtures/exports/package.json @@ -0,0 +1,10 @@ +{ + "name": "exports", + "version": "1.0.0", + "description": "package-exports-test", + "exports": { + ".": { + "import": "./index.js" + } + } +} diff --git a/packages/kit/src/core/make_package/test/fixtures/exports/src/app.html b/packages/kit/src/core/make_package/test/fixtures/exports/src/app.html new file mode 100644 index 000000000000..245305c4ef5b --- /dev/null +++ b/packages/kit/src/core/make_package/test/fixtures/exports/src/app.html @@ -0,0 +1,12 @@ + + + + + + + %svelte.head% + + +
%svelte.body%
+ + diff --git a/packages/kit/src/core/make_package/test/fixtures/exports/src/lib/Test.svelte b/packages/kit/src/core/make_package/test/fixtures/exports/src/lib/Test.svelte new file mode 100644 index 000000000000..31d118397f9d --- /dev/null +++ b/packages/kit/src/core/make_package/test/fixtures/exports/src/lib/Test.svelte @@ -0,0 +1,12 @@ + + + diff --git a/packages/kit/src/core/make_package/test/fixtures/exports/src/lib/index.js b/packages/kit/src/core/make_package/test/fixtures/exports/src/lib/index.js new file mode 100644 index 000000000000..4c44188c3648 --- /dev/null +++ b/packages/kit/src/core/make_package/test/fixtures/exports/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/exports/src/lib/internal/Test.svelte b/packages/kit/src/core/make_package/test/fixtures/exports/src/lib/internal/Test.svelte new file mode 100644 index 000000000000..06a5a3408e95 --- /dev/null +++ b/packages/kit/src/core/make_package/test/fixtures/exports/src/lib/internal/Test.svelte @@ -0,0 +1,6 @@ + diff --git a/packages/kit/src/core/make_package/test/fixtures/exports/src/lib/internal/foo.d.ts b/packages/kit/src/core/make_package/test/fixtures/exports/src/lib/internal/foo.d.ts new file mode 100644 index 000000000000..c941ad7b1ce6 --- /dev/null +++ b/packages/kit/src/core/make_package/test/fixtures/exports/src/lib/internal/foo.d.ts @@ -0,0 +1 @@ +export type Foo = boolean; diff --git a/packages/kit/src/core/make_package/test/fixtures/exports/svelte.config.js b/packages/kit/src/core/make_package/test/fixtures/exports/svelte.config.js new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/packages/kit/src/core/make_package/test/fixtures/nested/expected/Test.svelte b/packages/kit/src/core/make_package/test/fixtures/nested/expected/Test.svelte new file mode 100644 index 000000000000..31d118397f9d --- /dev/null +++ b/packages/kit/src/core/make_package/test/fixtures/nested/expected/Test.svelte @@ -0,0 +1,12 @@ + + + diff --git a/packages/kit/src/core/make_package/test/fixtures/nested/expected/Test.svelte.d.ts b/packages/kit/src/core/make_package/test/fixtures/nested/expected/Test.svelte.d.ts new file mode 100644 index 000000000000..8cfa75e62de7 --- /dev/null +++ b/packages/kit/src/core/make_package/test/fixtures/nested/expected/Test.svelte.d.ts @@ -0,0 +1,40 @@ +/** @typedef {typeof __propDef.props} TestProps */ +/** @typedef {typeof __propDef.events} TestEvents */ +/** @typedef {typeof __propDef.slots} TestSlots */ +export default class Test extends SvelteComponentTyped< + { + astring: string; + }, + { + event: CustomEvent; + } & { + [evt: string]: CustomEvent; + }, + { + default: { + astring: string; + }; + } +> { + get astring(): string; +} +export type TestProps = typeof __propDef.props; +export type TestEvents = typeof __propDef.events; +export type TestSlots = typeof __propDef.slots; +import { SvelteComponentTyped } from 'svelte'; +declare const __propDef: { + props: { + astring: string; + }; + events: { + event: CustomEvent; + } & { + [evt: string]: CustomEvent; + }; + slots: { + default: { + astring: string; + }; + }; +}; +export {}; diff --git a/packages/kit/src/core/make_package/test/fixtures/nested/expected/index.d.ts b/packages/kit/src/core/make_package/test/fixtures/nested/expected/index.d.ts new file mode 100644 index 000000000000..ed60fe04da25 --- /dev/null +++ b/packages/kit/src/core/make_package/test/fixtures/nested/expected/index.d.ts @@ -0,0 +1,2 @@ +export { default as Test } from './Test.svelte'; +export { default as Internal } from './internal/Test.svelte'; diff --git a/packages/kit/src/core/make_package/test/fixtures/nested/expected/index.js b/packages/kit/src/core/make_package/test/fixtures/nested/expected/index.js new file mode 100644 index 000000000000..ed60fe04da25 --- /dev/null +++ b/packages/kit/src/core/make_package/test/fixtures/nested/expected/index.js @@ -0,0 +1,2 @@ +export { default as Test } from './Test.svelte'; +export { default as Internal } from './internal/Test.svelte'; diff --git a/packages/kit/src/core/make_package/test/fixtures/nested/expected/internal/Test.svelte b/packages/kit/src/core/make_package/test/fixtures/nested/expected/internal/Test.svelte new file mode 100644 index 000000000000..06a5a3408e95 --- /dev/null +++ b/packages/kit/src/core/make_package/test/fixtures/nested/expected/internal/Test.svelte @@ -0,0 +1,6 @@ + diff --git a/packages/kit/src/core/make_package/test/fixtures/nested/expected/internal/Test.svelte.d.ts b/packages/kit/src/core/make_package/test/fixtures/nested/expected/internal/Test.svelte.d.ts new file mode 100644 index 000000000000..c787a209ac12 --- /dev/null +++ b/packages/kit/src/core/make_package/test/fixtures/nested/expected/internal/Test.svelte.d.ts @@ -0,0 +1,26 @@ +/** @typedef {typeof __propDef.props} Test2Props */ +/** @typedef {typeof __propDef.events} Test2Events */ +/** @typedef {typeof __propDef.slots} Test2Slots */ +export default class Test2 extends SvelteComponentTyped< + { + foo: boolean; + }, + { + [evt: string]: CustomEvent; + }, + {} +> {} +export type Test2Props = typeof __propDef.props; +export type Test2Events = typeof __propDef.events; +export type Test2Slots = typeof __propDef.slots; +import { SvelteComponentTyped } from 'svelte'; +declare const __propDef: { + props: { + foo: import('./foo').Foo; + }; + events: { + [evt: string]: CustomEvent; + }; + slots: {}; +}; +export {}; diff --git a/packages/kit/src/core/make_package/test/fixtures/nested/expected/internal/foo.d.ts b/packages/kit/src/core/make_package/test/fixtures/nested/expected/internal/foo.d.ts new file mode 100644 index 000000000000..c941ad7b1ce6 --- /dev/null +++ b/packages/kit/src/core/make_package/test/fixtures/nested/expected/internal/foo.d.ts @@ -0,0 +1 @@ +export type Foo = boolean; diff --git a/packages/kit/src/core/make_package/test/fixtures/nested/expected/package.json b/packages/kit/src/core/make_package/test/fixtures/nested/expected/package.json new file mode 100644 index 000000000000..e42f6745dbed --- /dev/null +++ b/packages/kit/src/core/make_package/test/fixtures/nested/expected/package.json @@ -0,0 +1,13 @@ +{ + "name": "nested", + "version": "1.0.0", + "description": "package-nested-test", + "type": "module", + "exports": { + "./package.json": "./package.json", + "./index.js": "./index.js", + "./internal/Test.svelte": "./internal/Test.svelte", + "./Test.svelte": "./Test.svelte", + ".": "./index.js" + } +} diff --git a/packages/kit/src/core/make_package/test/fixtures/nested/jsconfig.json b/packages/kit/src/core/make_package/test/fixtures/nested/jsconfig.json new file mode 100644 index 000000000000..5417394ebe85 --- /dev/null +++ b/packages/kit/src/core/make_package/test/fixtures/nested/jsconfig.json @@ -0,0 +1,9 @@ +{ + "compilerOptions": { + "checkJs": true, + "paths": { + "$lib/*": ["src/lib/*"] + } + }, + "include": ["src/**/*.d.ts", "src/**/*.js", "src/**/*.svelte"] +} diff --git a/packages/kit/src/core/make_package/test/fixtures/nested/package.json b/packages/kit/src/core/make_package/test/fixtures/nested/package.json new file mode 100644 index 000000000000..d575f11b5bef --- /dev/null +++ b/packages/kit/src/core/make_package/test/fixtures/nested/package.json @@ -0,0 +1,5 @@ +{ + "name": "nested", + "version": "1.0.0", + "description": "package-nested-test" +} diff --git a/packages/kit/src/core/make_package/test/fixtures/nested/src/app.html b/packages/kit/src/core/make_package/test/fixtures/nested/src/app.html new file mode 100644 index 000000000000..245305c4ef5b --- /dev/null +++ b/packages/kit/src/core/make_package/test/fixtures/nested/src/app.html @@ -0,0 +1,12 @@ + + + + + + + %svelte.head% + + +
%svelte.body%
+ + diff --git a/packages/kit/src/core/make_package/test/fixtures/nested/src/lib/Test.svelte b/packages/kit/src/core/make_package/test/fixtures/nested/src/lib/Test.svelte new file mode 100644 index 000000000000..31d118397f9d --- /dev/null +++ b/packages/kit/src/core/make_package/test/fixtures/nested/src/lib/Test.svelte @@ -0,0 +1,12 @@ + + + diff --git a/packages/kit/src/core/make_package/test/fixtures/nested/src/lib/index.js b/packages/kit/src/core/make_package/test/fixtures/nested/src/lib/index.js new file mode 100644 index 000000000000..ed60fe04da25 --- /dev/null +++ b/packages/kit/src/core/make_package/test/fixtures/nested/src/lib/index.js @@ -0,0 +1,2 @@ +export { default as Test } from './Test.svelte'; +export { default as Internal } from './internal/Test.svelte'; diff --git a/packages/kit/src/core/make_package/test/fixtures/nested/src/lib/internal/Test.svelte b/packages/kit/src/core/make_package/test/fixtures/nested/src/lib/internal/Test.svelte new file mode 100644 index 000000000000..06a5a3408e95 --- /dev/null +++ b/packages/kit/src/core/make_package/test/fixtures/nested/src/lib/internal/Test.svelte @@ -0,0 +1,6 @@ + diff --git a/packages/kit/src/core/make_package/test/fixtures/nested/src/lib/internal/foo.d.ts b/packages/kit/src/core/make_package/test/fixtures/nested/src/lib/internal/foo.d.ts new file mode 100644 index 000000000000..c941ad7b1ce6 --- /dev/null +++ b/packages/kit/src/core/make_package/test/fixtures/nested/src/lib/internal/foo.d.ts @@ -0,0 +1 @@ +export type Foo = boolean; diff --git a/packages/kit/src/core/make_package/test/fixtures/nested/svelte.config.js b/packages/kit/src/core/make_package/test/fixtures/nested/svelte.config.js new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/packages/kit/src/core/make_package/test/index.js b/packages/kit/src/core/make_package/test/index.js index 942eff4241e9..63ce96b9d52c 100644 --- a/packages/kit/src/core/make_package/test/index.js +++ b/packages/kit/src/core/make_package/test/index.js @@ -5,7 +5,7 @@ import { fileURLToPath } from 'url'; import { load_config } from '../../config/index.js'; import { make_package } from '../index.js'; import { rimraf } from '../../filesystem/index.js'; -import { readdirSync, readFileSync } from 'fs'; +import { lstatSync, readdirSync, readFileSync } from 'fs'; import prettier from 'prettier'; const __filename = fileURLToPath(import.meta.url); @@ -16,12 +16,30 @@ const __dirname = join(__filename, '..'); */ async function test_make_package(path) { const cwd = join(__dirname, 'fixtures', path); + const ewd = join(cwd, 'expected'); + const pwd = join(cwd, 'package'); + + /** + * @param {string} start starting pathname + * @param {string} dir absolute directory + * @returns {string[]} list of complete paths + */ + const get_complete_list = (start, dir) => { + const files = []; + for (const name of readdirSync(dir)) { + const relative = join(start, name); + const current = join(dir, name); + if (!lstatSync(current).isDirectory()) files.push(relative); + else files.concat(get_complete_list(relative, current)); + } + return files; + }; try { const config = await load_config({ cwd }); await make_package(config, cwd); - const expected_files = readdirSync(join(cwd, 'expected')); - const actual_files = readdirSync(join(cwd, 'package')); + const expected_files = get_complete_list('', ewd); + const actual_files = get_complete_list('', pwd); assert.equal( actual_files.length, expected_files.length, @@ -31,18 +49,32 @@ async function test_make_package(path) { actual_files.join(',') ); - for (const file of actual_files) { - assert.equal(expected_files.includes(file), true, `Did not expect ${file}`); - const expected_content = readFileSync(join(cwd, 'expected', file), 'utf-8'); - const actual_content = readFileSync(join(cwd, 'package', file), 'utf-8'); - assert.equal( - format(file, actual_content), - format(file, expected_content), - 'Expected equal file contents' - ); - } + /** + * @param {string[]} files relative names + * @param {string} path absolute pathnames + */ + const traverse = (files, path) => { + for (const file of files) { + const current = join(path, file); + if (lstatSync(current).isDirectory()) { + traverse(readdirSync(current), current); + } else { + assert.equal(expected_files.includes(file), true, `Did not expect ${file}`); + const expected_path = current.replace(/([/\\]test[/\\].+)package([/\\])/, '$1expected$2'); + const expected_content = readFileSync(expected_path, 'utf-8'); + const actual_content = readFileSync(current, 'utf-8'); + assert.equal( + format(file, actual_content), + format(file, expected_content), + 'Expected equal file contents' + ); + } + } + }; + + traverse(actual_files, pwd); } finally { - rimraf(join(cwd, 'package')); + rimraf(pwd); } } @@ -74,6 +106,18 @@ test('create package (typescript)', async () => { await test_make_package('typescript'); }); +test('create package (nested)', async () => { + await test_make_package('nested'); +}); + +test('create package with user defined settings (exports)', async () => { + await test_make_package('exports'); +}); + +test('create package with preserved files', async () => { + await test_make_package('casing'); +}); + test('create package (javascript without types)', async () => { await test_make_package('javascript_no_types'); });