Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/eighty-maps-talk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/package': minor
---

Allow customization of metadata files copied during packaging with svelte-package
1 change: 1 addition & 0 deletions packages/kit/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ export interface Config {
emitTypes?: boolean;
exports?(filepath: string): boolean;
files?(filepath: string): boolean;
metadata?(filepath: string): boolean;
};
/** Preprocessor options, if any. Preprocessing can alternatively also be done through Vite's preprocessor capabilities. */
preprocess?: any;
Expand Down
3 changes: 2 additions & 1 deletion packages/package/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ function process_config(config, { cwd = process.cwd() } = {}) {
dir: config.package?.dir ?? 'package',
exports: config.package?.exports ?? ((filepath) => !/^_|\/_|\.d\.ts$/.test(filepath)),
files: config.package?.files ?? (() => true),
emitTypes: config.package?.emitTypes ?? true
emitTypes: config.package?.emitTypes ?? true,
metadata: config.package?.metadata ?? (() => true)
},
preprocess: config.preprocess
};
Expand Down
7 changes: 5 additions & 2 deletions packages/package/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ export async function build(config, cwd = process.cwd()) {
}
}

write(join(dir, 'package.json'), JSON.stringify(pkg, null, 2));
if (config.package.metadata('package.json')) {
write(join(dir, 'package.json'), JSON.stringify(pkg, null, 2));
}

for (const file of files) {
await process_file(config, file);
Expand All @@ -75,6 +77,7 @@ export async function build(config, cwd = process.cwd()) {
for (const pathname of whitelist) {
const full_path = join(cwd, pathname);
if (fs.lstatSync(full_path).isDirectory()) continue; // just to be sure
if (!config.package.metadata(pathname)) continue;

const package_path = join(dir, pathname);
if (!fs.existsSync(package_path)) fs.copyFileSync(full_path, package_path);
Expand Down Expand Up @@ -160,7 +163,7 @@ export async function watch(config, cwd = process.cwd()) {
}
}

if (should_update_pkg) {
if (should_update_pkg && config.package.metadata('package.json')) {
const pkg = generate_pkg(cwd, files);
write(join(dir, 'package.json'), JSON.stringify(pkg, null, 2));
console.log('Updated package.json');
Expand Down
1 change: 1 addition & 0 deletions packages/package/test/fixtures/metadata-false/ReadMe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
declare const _default: {};
export default _default;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default {};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
7 changes: 7 additions & 0 deletions packages/package/test/fixtures/metadata-false/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "metadata-false",
"private": true,
"version": "1.0.0",
"description": "metadata false",
"type": "module"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default {};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const config = {
package: {
metadata: () => false
}
};

export default config;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
declare const _default: {};
export default _default;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default {};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "metadata-readme-only",
"private": true,
"version": "1.0.0",
"description": "metadata copy readme only",
"type": "module"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default {};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const config = {
package: {
metadata: (filepath) => filepath === 'ReadMe.md'
}
};

export default config;
8 changes: 8 additions & 0 deletions packages/package/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,14 @@ test('create package with emitTypes settings disabled', async () => {
await test_make_package('emitTypes-false');
});

test('create package with no metadata', async () => {
await test_make_package('metadata-false');
});

test('create package with filtered metadata', async () => {
await test_make_package('metadata-readmeOnly');
});

test('create package and properly merge exports map', async () => {
await test_make_package('exports-merge');
});
Expand Down
4 changes: 4 additions & 0 deletions packages/package/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ export interface PackageConfig {
* Function that determines if the given file is part of the output.
*/
files?(filepath: string): boolean;
/**
* Function that determines if the given metadata file is part of the output.
*/
metadata?(filepath: string): boolean;
}

export interface Config {
Expand Down