Skip to content

Commit d48bfee

Browse files
committed
(feat) copy package.json functionality, update migration script
closes #9114
1 parent fa18dc5 commit d48bfee

File tree

17 files changed

+126
-12
lines changed

17 files changed

+126
-12
lines changed

.changeset/odd-dragons-complain.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/package': minor
3+
---
4+
5+
feat: add copy package.json functionality

.changeset/smart-actors-play.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte-migrate': patch
3+
---
4+
5+
fix: use copy-package.json-functionality to fully resemble old way of doing things

packages/migrate/migrations/package/migrate_pkg.js

+5-10
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export function update_pkg_json(config, pkg, files) {
7171
for (const key in pkg.scripts || []) {
7272
const script = pkg.scripts[key];
7373
if (script.includes('svelte-package')) {
74-
pkg.scripts[key] = script.replace('svelte-package', `svelte-package -o ${out_dir}`);
74+
pkg.scripts[key] = script.replace('svelte-package', `svelte-package -o ${out_dir} -c`);
7575
}
7676
}
7777

@@ -81,13 +81,8 @@ export function update_pkg_json(config, pkg, files) {
8181
...pkg.exports
8282
};
8383

84-
pkg.files = pkg.files || [];
85-
if (!pkg.files.includes(out_dir)) {
86-
pkg.files.push(out_dir);
87-
}
88-
8984
if (pkg.devDependencies?.['@sveltejs/package']) {
90-
pkg.devDependencies['@sveltejs/package'] = '^2.0.0';
85+
pkg.devDependencies['@sveltejs/package'] = '^2.1.0';
9186
}
9287

9388
/** @type {Record<string, string>} */
@@ -112,12 +107,12 @@ export function update_pkg_json(config, pkg, files) {
112107
// JSON.stringify will remove the undefined entries
113108
pkg.exports[key] = {
114109
types: has_type
115-
? `./${out_dir}/${
110+
? `./${
116111
file.is_svelte ? `${file.dest}.d.ts` : file.dest.slice(0, -'.js'.length) + '.d.ts'
117112
}`
118113
: undefined,
119-
svelte: needs_svelte_condition ? `./${out_dir}/${file.dest}` : undefined,
120-
default: `./${out_dir}/${file.dest}`
114+
svelte: needs_svelte_condition ? `./${file.dest}` : undefined,
115+
default: `./${file.dest}`
121116
};
122117

123118
if (Object.values(pkg.exports[key]).filter(Boolean).length === 1) {

packages/package/src/cli.js

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ prog
2323
.option('-i, --input', 'Input directory')
2424
.option('-o, --output', 'Output directory', 'dist')
2525
.option('-t, --types', 'Emit type declarations', true)
26+
.option('-c, --copy-pkg-json', 'Copy package.json as is into output folder', false)
2627
.option('-w, --watch', 'Rerun when files change', false)
2728
.action(async (args) => {
2829
try {
@@ -43,6 +44,7 @@ prog
4344
input: args.input ?? config.kit?.files?.lib ?? 'src/lib',
4445
output: args.output,
4546
types: args.types,
47+
copy_pkg: args.copyPkgJson,
4648
config
4749
};
4850

packages/package/src/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ async function do_build(options, analyse_code) {
4141
await process_file(input, output, file, options.config.preprocess, alias, analyse_code);
4242
}
4343

44+
if (options.copy_pkg) {
45+
fs.copyFileSync(path.join(options.cwd, 'package.json'), path.join(output, 'package.json'));
46+
}
47+
4448
console.log(
4549
colors
4650
.bold()

packages/package/src/types.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export interface Options {
55
input: string;
66
output: string;
77
types: boolean;
8+
copy_pkg: boolean;
89
config: {
910
extensions?: string[];
1011
kit?: {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<script>
2+
import { createEventDispatcher } from 'svelte';
3+
/**
4+
* @type {string}
5+
*/
6+
export const astring = 'potato';
7+
8+
const dispatch = createEventDispatcher();
9+
dispatch('event', true);
10+
</script>
11+
12+
<slot {astring} />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/** @typedef {typeof __propDef.props} TestProps */
2+
/** @typedef {typeof __propDef.events} TestEvents */
3+
/** @typedef {typeof __propDef.slots} TestSlots */
4+
export default class Test extends SvelteComponentTyped<
5+
{
6+
astring?: string;
7+
},
8+
{
9+
event: CustomEvent<any>;
10+
} & {
11+
[evt: string]: CustomEvent<any>;
12+
},
13+
{
14+
default: {
15+
astring: string;
16+
};
17+
}
18+
> {
19+
get astring(): string;
20+
}
21+
export type TestProps = typeof __propDef.props;
22+
export type TestEvents = typeof __propDef.events;
23+
export type TestSlots = typeof __propDef.slots;
24+
import { SvelteComponentTyped } from 'svelte';
25+
declare const __propDef: {
26+
props: {
27+
astring?: string;
28+
};
29+
events: {
30+
event: CustomEvent<any>;
31+
} & {
32+
[evt: string]: CustomEvent<any>;
33+
};
34+
slots: {
35+
default: {
36+
astring: string;
37+
};
38+
};
39+
};
40+
export {};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default as Test } from './Test.svelte';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default as Test } from './Test.svelte';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "copy-pkg-json",
3+
"private": true,
4+
"version": "1.0.0",
5+
"description": "test copy-pkg-json option",
6+
"peerDependencies": {
7+
"svelte": "^3.55.0"
8+
},
9+
"exports": {
10+
".": {
11+
"svelte": "./index.js"
12+
}
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "copy-pkg-json",
3+
"private": true,
4+
"version": "1.0.0",
5+
"description": "test copy-pkg-json option",
6+
"peerDependencies": {
7+
"svelte": "^3.55.0"
8+
},
9+
"exports": {
10+
".": {
11+
"svelte": "./index.js"
12+
}
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<script>
2+
import { createEventDispatcher } from 'svelte';
3+
/**
4+
* @type {string}
5+
*/
6+
export const astring = 'potato';
7+
8+
const dispatch = createEventDispatcher();
9+
dispatch('event', true);
10+
</script>
11+
12+
<slot {astring} />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default as Test } from './Test.svelte';

packages/package/test/fixtures/copy-pkg-json/svelte.config.js

Whitespace-only changes.

packages/package/test/index.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ async function test_make_package(path, options) {
3232
output,
3333
types: true,
3434
config,
35+
copy_pkg: false,
3536
...options
3637
});
3738

@@ -88,7 +89,7 @@ for (const dir of fs.readdirSync(join(__dirname, 'errors'))) {
8889
const input = resolve(cwd, config.kit?.files?.lib ?? 'src/lib');
8990

9091
try {
91-
await build({ cwd, input, output, types: true, config });
92+
await build({ cwd, input, output, types: true, config, copy_pkg: false });
9293
assert.unreachable('Must not pass build');
9394
} catch (/** @type {any} */ error) {
9495
assert.instance(error, Error);
@@ -138,6 +139,10 @@ test('create package and resolves $lib alias', async () => {
138139
await test_make_package('resolve-alias');
139140
});
140141

142+
test.only('create package and copy over package.json', async () => {
143+
await test_make_package('copy-pkg-json', { copy_pkg: true });
144+
});
145+
141146
test('SvelteKit interop', async () => {
142147
await test_make_package('svelte-kit');
143148
});
@@ -154,7 +159,8 @@ if (!process.env.CI) {
154159
input: 'src/lib',
155160
output: 'package',
156161
types: true,
157-
config
162+
config,
163+
copy_pkg: false
158164
});
159165

160166
/** @param {string} file */

0 commit comments

Comments
 (0)