From d48bfee89e4153eeb4f9eadf424a0ac494cf65a1 Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Sat, 18 Feb 2023 21:22:23 +0100 Subject: [PATCH] (feat) copy package.json functionality, update migration script closes #9114 --- .changeset/odd-dragons-complain.md | 5 +++ .changeset/smart-actors-play.md | 5 +++ .../migrate/migrations/package/migrate_pkg.js | 15 +++---- packages/package/src/cli.js | 2 + packages/package/src/index.js | 4 ++ packages/package/src/types.d.ts | 1 + .../copy-pkg-json/expected/Test.svelte | 12 ++++++ .../copy-pkg-json/expected/Test.svelte.d.ts | 40 +++++++++++++++++++ .../copy-pkg-json/expected/index.d.ts | 1 + .../fixtures/copy-pkg-json/expected/index.js | 1 + .../copy-pkg-json/expected/package.json | 14 +++++++ .../test/fixtures/copy-pkg-json/jsconfig.json | 1 + .../test/fixtures/copy-pkg-json/package.json | 14 +++++++ .../copy-pkg-json/src/lib/Test.svelte | 12 ++++++ .../fixtures/copy-pkg-json/src/lib/index.js | 1 + .../fixtures/copy-pkg-json/svelte.config.js | 0 packages/package/test/index.js | 10 ++++- 17 files changed, 126 insertions(+), 12 deletions(-) create mode 100644 .changeset/odd-dragons-complain.md create mode 100644 .changeset/smart-actors-play.md create mode 100644 packages/package/test/fixtures/copy-pkg-json/expected/Test.svelte create mode 100644 packages/package/test/fixtures/copy-pkg-json/expected/Test.svelte.d.ts create mode 100644 packages/package/test/fixtures/copy-pkg-json/expected/index.d.ts create mode 100644 packages/package/test/fixtures/copy-pkg-json/expected/index.js create mode 100644 packages/package/test/fixtures/copy-pkg-json/expected/package.json create mode 100644 packages/package/test/fixtures/copy-pkg-json/jsconfig.json create mode 100644 packages/package/test/fixtures/copy-pkg-json/package.json create mode 100644 packages/package/test/fixtures/copy-pkg-json/src/lib/Test.svelte create mode 100644 packages/package/test/fixtures/copy-pkg-json/src/lib/index.js create mode 100644 packages/package/test/fixtures/copy-pkg-json/svelte.config.js diff --git a/.changeset/odd-dragons-complain.md b/.changeset/odd-dragons-complain.md new file mode 100644 index 000000000000..029557ff8af3 --- /dev/null +++ b/.changeset/odd-dragons-complain.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/package': minor +--- + +feat: add copy package.json functionality diff --git a/.changeset/smart-actors-play.md b/.changeset/smart-actors-play.md new file mode 100644 index 000000000000..1ba9b24d620f --- /dev/null +++ b/.changeset/smart-actors-play.md @@ -0,0 +1,5 @@ +--- +'svelte-migrate': patch +--- + +fix: use copy-package.json-functionality to fully resemble old way of doing things diff --git a/packages/migrate/migrations/package/migrate_pkg.js b/packages/migrate/migrations/package/migrate_pkg.js index 044bb71dcf24..1eabdf5d6f1a 100644 --- a/packages/migrate/migrations/package/migrate_pkg.js +++ b/packages/migrate/migrations/package/migrate_pkg.js @@ -71,7 +71,7 @@ export function update_pkg_json(config, pkg, files) { for (const key in pkg.scripts || []) { const script = pkg.scripts[key]; if (script.includes('svelte-package')) { - pkg.scripts[key] = script.replace('svelte-package', `svelte-package -o ${out_dir}`); + pkg.scripts[key] = script.replace('svelte-package', `svelte-package -o ${out_dir} -c`); } } @@ -81,13 +81,8 @@ export function update_pkg_json(config, pkg, files) { ...pkg.exports }; - pkg.files = pkg.files || []; - if (!pkg.files.includes(out_dir)) { - pkg.files.push(out_dir); - } - if (pkg.devDependencies?.['@sveltejs/package']) { - pkg.devDependencies['@sveltejs/package'] = '^2.0.0'; + pkg.devDependencies['@sveltejs/package'] = '^2.1.0'; } /** @type {Record} */ @@ -112,12 +107,12 @@ export function update_pkg_json(config, pkg, files) { // JSON.stringify will remove the undefined entries pkg.exports[key] = { types: has_type - ? `./${out_dir}/${ + ? `./${ file.is_svelte ? `${file.dest}.d.ts` : file.dest.slice(0, -'.js'.length) + '.d.ts' }` : undefined, - svelte: needs_svelte_condition ? `./${out_dir}/${file.dest}` : undefined, - default: `./${out_dir}/${file.dest}` + svelte: needs_svelte_condition ? `./${file.dest}` : undefined, + default: `./${file.dest}` }; if (Object.values(pkg.exports[key]).filter(Boolean).length === 1) { diff --git a/packages/package/src/cli.js b/packages/package/src/cli.js index a57c8d065875..419638863b37 100644 --- a/packages/package/src/cli.js +++ b/packages/package/src/cli.js @@ -23,6 +23,7 @@ prog .option('-i, --input', 'Input directory') .option('-o, --output', 'Output directory', 'dist') .option('-t, --types', 'Emit type declarations', true) + .option('-c, --copy-pkg-json', 'Copy package.json as is into output folder', false) .option('-w, --watch', 'Rerun when files change', false) .action(async (args) => { try { @@ -43,6 +44,7 @@ prog input: args.input ?? config.kit?.files?.lib ?? 'src/lib', output: args.output, types: args.types, + copy_pkg: args.copyPkgJson, config }; diff --git a/packages/package/src/index.js b/packages/package/src/index.js index fb8690934f7a..f8d05aff4a86 100644 --- a/packages/package/src/index.js +++ b/packages/package/src/index.js @@ -41,6 +41,10 @@ async function do_build(options, analyse_code) { await process_file(input, output, file, options.config.preprocess, alias, analyse_code); } + if (options.copy_pkg) { + fs.copyFileSync(path.join(options.cwd, 'package.json'), path.join(output, 'package.json')); + } + console.log( colors .bold() diff --git a/packages/package/src/types.d.ts b/packages/package/src/types.d.ts index 3e481ed2bef9..4e87256b4b69 100644 --- a/packages/package/src/types.d.ts +++ b/packages/package/src/types.d.ts @@ -5,6 +5,7 @@ export interface Options { input: string; output: string; types: boolean; + copy_pkg: boolean; config: { extensions?: string[]; kit?: { diff --git a/packages/package/test/fixtures/copy-pkg-json/expected/Test.svelte b/packages/package/test/fixtures/copy-pkg-json/expected/Test.svelte new file mode 100644 index 000000000000..e60789af6a74 --- /dev/null +++ b/packages/package/test/fixtures/copy-pkg-json/expected/Test.svelte @@ -0,0 +1,12 @@ + + + diff --git a/packages/package/test/fixtures/copy-pkg-json/expected/Test.svelte.d.ts b/packages/package/test/fixtures/copy-pkg-json/expected/Test.svelte.d.ts new file mode 100644 index 000000000000..71bb3d88958e --- /dev/null +++ b/packages/package/test/fixtures/copy-pkg-json/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/package/test/fixtures/copy-pkg-json/expected/index.d.ts b/packages/package/test/fixtures/copy-pkg-json/expected/index.d.ts new file mode 100644 index 000000000000..4c44188c3648 --- /dev/null +++ b/packages/package/test/fixtures/copy-pkg-json/expected/index.d.ts @@ -0,0 +1 @@ +export { default as Test } from './Test.svelte'; diff --git a/packages/package/test/fixtures/copy-pkg-json/expected/index.js b/packages/package/test/fixtures/copy-pkg-json/expected/index.js new file mode 100644 index 000000000000..4c44188c3648 --- /dev/null +++ b/packages/package/test/fixtures/copy-pkg-json/expected/index.js @@ -0,0 +1 @@ +export { default as Test } from './Test.svelte'; diff --git a/packages/package/test/fixtures/copy-pkg-json/expected/package.json b/packages/package/test/fixtures/copy-pkg-json/expected/package.json new file mode 100644 index 000000000000..94d2a64ebb0a --- /dev/null +++ b/packages/package/test/fixtures/copy-pkg-json/expected/package.json @@ -0,0 +1,14 @@ +{ + "name": "copy-pkg-json", + "private": true, + "version": "1.0.0", + "description": "test copy-pkg-json option", + "peerDependencies": { + "svelte": "^3.55.0" + }, + "exports": { + ".": { + "svelte": "./index.js" + } + } +} diff --git a/packages/package/test/fixtures/copy-pkg-json/jsconfig.json b/packages/package/test/fixtures/copy-pkg-json/jsconfig.json new file mode 100644 index 000000000000..0967ef424bce --- /dev/null +++ b/packages/package/test/fixtures/copy-pkg-json/jsconfig.json @@ -0,0 +1 @@ +{} diff --git a/packages/package/test/fixtures/copy-pkg-json/package.json b/packages/package/test/fixtures/copy-pkg-json/package.json new file mode 100644 index 000000000000..94d2a64ebb0a --- /dev/null +++ b/packages/package/test/fixtures/copy-pkg-json/package.json @@ -0,0 +1,14 @@ +{ + "name": "copy-pkg-json", + "private": true, + "version": "1.0.0", + "description": "test copy-pkg-json option", + "peerDependencies": { + "svelte": "^3.55.0" + }, + "exports": { + ".": { + "svelte": "./index.js" + } + } +} diff --git a/packages/package/test/fixtures/copy-pkg-json/src/lib/Test.svelte b/packages/package/test/fixtures/copy-pkg-json/src/lib/Test.svelte new file mode 100644 index 000000000000..e60789af6a74 --- /dev/null +++ b/packages/package/test/fixtures/copy-pkg-json/src/lib/Test.svelte @@ -0,0 +1,12 @@ + + + diff --git a/packages/package/test/fixtures/copy-pkg-json/src/lib/index.js b/packages/package/test/fixtures/copy-pkg-json/src/lib/index.js new file mode 100644 index 000000000000..4c44188c3648 --- /dev/null +++ b/packages/package/test/fixtures/copy-pkg-json/src/lib/index.js @@ -0,0 +1 @@ +export { default as Test } from './Test.svelte'; diff --git a/packages/package/test/fixtures/copy-pkg-json/svelte.config.js b/packages/package/test/fixtures/copy-pkg-json/svelte.config.js new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/packages/package/test/index.js b/packages/package/test/index.js index a0ab8b4b379c..55824490355d 100644 --- a/packages/package/test/index.js +++ b/packages/package/test/index.js @@ -32,6 +32,7 @@ async function test_make_package(path, options) { output, types: true, config, + copy_pkg: false, ...options }); @@ -88,7 +89,7 @@ for (const dir of fs.readdirSync(join(__dirname, 'errors'))) { const input = resolve(cwd, config.kit?.files?.lib ?? 'src/lib'); try { - await build({ cwd, input, output, types: true, config }); + await build({ cwd, input, output, types: true, config, copy_pkg: false }); assert.unreachable('Must not pass build'); } catch (/** @type {any} */ error) { assert.instance(error, Error); @@ -138,6 +139,10 @@ test('create package and resolves $lib alias', async () => { await test_make_package('resolve-alias'); }); +test.only('create package and copy over package.json', async () => { + await test_make_package('copy-pkg-json', { copy_pkg: true }); +}); + test('SvelteKit interop', async () => { await test_make_package('svelte-kit'); }); @@ -154,7 +159,8 @@ if (!process.env.CI) { input: 'src/lib', output: 'package', types: true, - config + config, + copy_pkg: false }); /** @param {string} file */