Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(package): added --preserve-output flag #13055

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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/mean-ladybugs-sparkle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@sveltejs/package": minor
---

Added `--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 @@ -225,6 +225,7 @@ You should think carefully about whether or not the changes you make to your pac
- `-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 @@ -172,6 +175,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 @@ -188,6 +192,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.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ async function test_make_package(path, options) {
cwd,
input,
output,
preserve_output: false,
types: true,
config,
...options
Expand Down Expand Up @@ -90,7 +91,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 });
assert.unreachable('Must not pass build');
} catch (/** @type {any} */ error) {
assert.instance(error, Error);
Expand Down Expand Up @@ -167,6 +168,7 @@ if (!process.env.CI) {
cwd,
input: 'src/lib',
output: 'package',
preserve_output: false,
types: true,
config
});
Expand Down Expand Up @@ -256,6 +258,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 @@ -275,6 +278,7 @@ test('validates package (2)', () => {
cwd: '',
input: '',
output: '',
preserve_output: false,
types: true
});
analyse_code('src/lib/C.svelte', '');
Expand All @@ -294,6 +298,7 @@ test('validates package (all ok 1)', () => {
cwd: '',
input: '',
output: '',
preserve_output: false,
types: true
});
analyse_code('src/lib/C.svelte', '');
Expand All @@ -311,6 +316,7 @@ test('validates package (all ok 2)', () => {
cwd: '',
input: '',
output: '',
preserve_output: false,
types: true
});
analyse_code('src/lib/C.svelte', '');
Expand All @@ -323,4 +329,12 @@ test('validates package (all ok 2)', () => {
assert.equal(warnings.length, 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 });
});

test.run();
Loading