Skip to content
Merged
5 changes: 5 additions & 0 deletions .changeset/mean-ladybugs-sparkle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@sveltejs/package": minor
---

feat: add `--preserve-output` flag to prevent deletion of the output directory before packaging
1 change: 1 addition & 0 deletions documentation/docs/30-advanced/70-packaging.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ You can create so-called declaration maps (`d.ts.map` files) by setting `"declar
- `-w`/`--watch` — watch files in `src/lib` for changes and rebuild the package
- `-i`/`--input` — the input directory which contains all the files of the package. Defaults to `src/lib`
- `-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`
- `-p`/`--preserve-output` — prevent deletion of the output directory before packaging. Defaults to `false`, which means that the output directory will be emptied first
- `-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`
- `--tsconfig` - the path to a tsconfig or jsconfig. When not provided, searches for the next upper tsconfig/jsconfig in the workspace path.

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
.describe('Create a package')
.option('-i, --input', 'Input directory')
.option('-o, --output', 'Output directory', 'dist')
.option('-p, --preserve-output', 'Do not delete the output directory before packaging', false)
.option('-t, --types', 'Emit type declarations', true)
.option('-w, --watch', 'Rerun when files change', false)
.option(
Expand All @@ -47,6 +48,7 @@ prog
cwd: process.cwd(),
input: args.input ?? config.kit?.files?.lib ?? 'src/lib',
output: args.output,
preserve_output: args['preserve-output'],
tsconfig: args.tsconfig,
types: args.types,
config
Expand Down
7 changes: 6 additions & 1 deletion packages/package/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ async function do_build(options, analyse_code) {
await process_file(input, temp, file, options.config.preprocess, alias, tsconfig, analyse_code);
}

rimraf(output);
if (!options.preserve_output) {
rimraf(output);
}

mkdirp(output);
copy(temp, output);

Expand Down Expand Up @@ -173,6 +176,7 @@ export async function watch(options) {
function normalize_options(options) {
const input = path.resolve(options.cwd, options.input);
const output = path.resolve(options.cwd, options.output);
const preserve_output = options.preserve_output;
const temp = path.resolve(
options.cwd,
options.config.kit?.outDir ?? '.svelte-kit',
Expand All @@ -189,6 +193,7 @@ function normalize_options(options) {
return {
input,
output,
preserve_output,
temp,
extensions,
alias,
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 @@ -4,6 +4,7 @@ export interface Options {
cwd: string;
input: string;
output: string;
preserve_output: boolean;
types: boolean;
tsconfig?: string;
config: {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:root { color: red }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export declare const foo = 'bar';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const foo = 'bar';
16 changes: 16 additions & 0 deletions packages/package/test/fixtures/preserve-output/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "preserve-output",
"private": true,
"type": "module",
"description": "with additional things running before svelte-package",
"peerDependencies": {
"svelte": "^4.0.0"
},
"exports": {
".": {
"types": "./dist/index.d.ts",
"svelte": "./dist/index.js"
},
"./theme.css": "./dist/assets/theme.css"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const foo = 'bar';
5 changes: 5 additions & 0 deletions packages/package/test/fixtures/preserve-output/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"target": "ESNext"
}
}
16 changes: 15 additions & 1 deletion packages/package/test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ async function test_make_package(path, options) {
cwd,
input,
output,
preserve_output: false,
types: true,
config,
...options
Expand Down Expand Up @@ -93,7 +94,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, preserve_output: false });
throw new Error('Must not pass build');
} catch (/** @type {any} */ error) {
expect(error).toBeInstanceOf(Error);
Expand Down Expand Up @@ -183,6 +184,7 @@ if (!process.env.CI) {
cwd,
input: 'src/lib',
output: 'package',
preserve_output: false,
types: true,
config
});
Expand Down Expand Up @@ -271,6 +273,7 @@ test('validates package (1)', () => {
cwd: '',
input: '',
output: '',
preserve_output: false,
types: true
});
analyse_code('src/lib/index.js', 'export const a = 1;import.meta.env;');
Expand All @@ -290,6 +293,7 @@ test('validates package (2)', () => {
cwd: '',
input: '',
output: '',
preserve_output: false,
types: true
});
analyse_code('src/lib/C.svelte', '');
Expand All @@ -309,6 +313,7 @@ test('validates package (all ok 1)', () => {
cwd: '',
input: '',
output: '',
preserve_output: false,
types: true
});
analyse_code('src/lib/C.svelte', '');
Expand All @@ -326,6 +331,7 @@ test('validates package (all ok 2)', () => {
cwd: '',
input: '',
output: '',
preserve_output: false,
types: true
});
analyse_code('src/lib/C.svelte', '');
Expand All @@ -337,3 +343,11 @@ test('validates package (all ok 2)', () => {

expect(warnings.length).toEqual(0);
});

test('create package with preserved output', async () => {
const output = join(__dirname, 'fixtures', 'preserve-output', 'dist');
rimraf(output);
fs.mkdirSync(join(output, 'assets'), { recursive: true });
fs.writeFileSync(join(output, 'assets', 'theme.css'), ':root { color: red }');
await test_make_package('preserve-output', { preserve_output: true });
});
Loading