Skip to content

Commit

Permalink
(feat) copy package.json functionality, update migration script
Browse files Browse the repository at this point in the history
closes #9114
  • Loading branch information
dummdidumm committed Feb 18, 2023
1 parent fa18dc5 commit d48bfee
Show file tree
Hide file tree
Showing 17 changed files with 126 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/odd-dragons-complain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/package': minor
---

feat: add copy package.json functionality
5 changes: 5 additions & 0 deletions .changeset/smart-actors-play.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte-migrate': patch
---

fix: use copy-package.json-functionality to fully resemble old way of doing things
15 changes: 5 additions & 10 deletions packages/migrate/migrations/package/migrate_pkg.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
}
}

Expand All @@ -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<string, string>} */
Expand All @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions packages/package/src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
};

Expand Down
4 changes: 4 additions & 0 deletions packages/package/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions packages/package/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface Options {
input: string;
output: string;
types: boolean;
copy_pkg: boolean;
config: {
extensions?: string[];
kit?: {
Expand Down
12 changes: 12 additions & 0 deletions packages/package/test/fixtures/copy-pkg-json/expected/Test.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<script>
import { createEventDispatcher } from 'svelte';
/**
* @type {string}
*/
export const astring = 'potato';
const dispatch = createEventDispatcher();
dispatch('event', true);
</script>

<slot {astring} />
Original file line number Diff line number Diff line change
@@ -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<any>;
} & {
[evt: string]: CustomEvent<any>;
},
{
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<any>;
} & {
[evt: string]: CustomEvent<any>;
};
slots: {
default: {
astring: string;
};
};
};
export {};
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 @@
export { default as Test } from './Test.svelte';
14 changes: 14 additions & 0 deletions packages/package/test/fixtures/copy-pkg-json/expected/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
1 change: 1 addition & 0 deletions packages/package/test/fixtures/copy-pkg-json/jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
14 changes: 14 additions & 0 deletions packages/package/test/fixtures/copy-pkg-json/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
12 changes: 12 additions & 0 deletions packages/package/test/fixtures/copy-pkg-json/src/lib/Test.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<script>
import { createEventDispatcher } from 'svelte';
/**
* @type {string}
*/
export const astring = 'potato';
const dispatch = createEventDispatcher();
dispatch('event', true);
</script>

<slot {astring} />
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as Test } from './Test.svelte';
Empty file.
10 changes: 8 additions & 2 deletions packages/package/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ async function test_make_package(path, options) {
output,
types: true,
config,
copy_pkg: false,
...options
});

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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');
});
Expand All @@ -154,7 +159,8 @@ if (!process.env.CI) {
input: 'src/lib',
output: 'package',
types: true,
config
config,
copy_pkg: false
});

/** @param {string} file */
Expand Down

0 comments on commit d48bfee

Please sign in to comment.