Skip to content

Commit 46efb63

Browse files
GauBenRich-Harris
andauthored
feat(package): add --preserve-output flag (#13055)
* feat(package): added --preserve-output flag * Create mean-ladybugs-sparkle.md * Update 70-packaging.md * Update .changeset/mean-ladybugs-sparkle.md * Update .changeset/mean-ladybugs-sparkle.md * Update .changeset/mean-ladybugs-sparkle.md * oops * goddammit --------- Co-authored-by: Rich Harris <[email protected]> Co-authored-by: Rich Harris <[email protected]>
1 parent 82a0e96 commit 46efb63

File tree

12 files changed

+55
-2
lines changed

12 files changed

+55
-2
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@sveltejs/package": minor
3+
---
4+
5+
feat: add `--preserve-output` flag to prevent deletion of the output directory before packaging

documentation/docs/30-advanced/70-packaging.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ You can create so-called declaration maps (`d.ts.map` files) by setting `"declar
242242
- `-w`/`--watch` — watch files in `src/lib` for changes and rebuild the package
243243
- `-i`/`--input` — the input directory which contains all the files of the package. Defaults to `src/lib`
244244
- `-o`/`--output` — the output directory where the processed files are written to. Your `package.json`'s `exports` should point to files inside there, and the `files` array should include that folder. Defaults to `dist`
245+
- `-p`/`--preserve-output` — prevent deletion of the output directory before packaging. Defaults to `false`, which means that the output directory will be emptied first
245246
- `-t`/`--types` — whether or not to create type definitions (`d.ts` files). We strongly recommend doing this as it fosters ecosystem library quality. Defaults to `true`
246247
- `--tsconfig` - the path to a tsconfig or jsconfig. When not provided, searches for the next upper tsconfig/jsconfig in the workspace path.
247248

packages/package/src/cli.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ prog
2323
.describe('Create a package')
2424
.option('-i, --input', 'Input directory')
2525
.option('-o, --output', 'Output directory', 'dist')
26+
.option('-p, --preserve-output', 'Do not delete the output directory before packaging', false)
2627
.option('-t, --types', 'Emit type declarations', true)
2728
.option('-w, --watch', 'Rerun when files change', false)
2829
.option(
@@ -47,6 +48,7 @@ prog
4748
cwd: process.cwd(),
4849
input: args.input ?? config.kit?.files?.lib ?? 'src/lib',
4950
output: args.output,
51+
preserve_output: args['preserve-output'],
5052
tsconfig: args.tsconfig,
5153
types: args.types,
5254
config

packages/package/src/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ async function do_build(options, analyse_code) {
4141
await process_file(input, temp, file, options.config.preprocess, alias, tsconfig, analyse_code);
4242
}
4343

44-
rimraf(output);
44+
if (!options.preserve_output) {
45+
rimraf(output);
46+
}
47+
4548
mkdirp(output);
4649
copy(temp, output);
4750

@@ -173,6 +176,7 @@ export async function watch(options) {
173176
function normalize_options(options) {
174177
const input = path.resolve(options.cwd, options.input);
175178
const output = path.resolve(options.cwd, options.output);
179+
const preserve_output = options.preserve_output;
176180
const temp = path.resolve(
177181
options.cwd,
178182
options.config.kit?.outDir ?? '.svelte-kit',
@@ -189,6 +193,7 @@ function normalize_options(options) {
189193
return {
190194
input,
191195
output,
196+
preserve_output,
192197
temp,
193198
extensions,
194199
alias,

packages/package/src/types.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export interface Options {
44
cwd: string;
55
input: string;
66
output: string;
7+
preserve_output: boolean;
78
types: boolean;
89
tsconfig?: string;
910
config: {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:root { color: red }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export declare const foo = 'bar';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const foo = 'bar';
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "preserve-output",
3+
"private": true,
4+
"type": "module",
5+
"description": "with additional things running before svelte-package",
6+
"peerDependencies": {
7+
"svelte": "^4.0.0"
8+
},
9+
"exports": {
10+
".": {
11+
"types": "./dist/index.d.ts",
12+
"svelte": "./dist/index.js"
13+
},
14+
"./theme.css": "./dist/assets/theme.css"
15+
}
16+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const foo = 'bar';

0 commit comments

Comments
 (0)