Skip to content

Commit 9304c9d

Browse files
enBonnetbenmccannRich-Harris
authored
Move compress inside kit (#5822)
* move tiny-glob dependency * add compress to builder * replace compress with builder.compress * add changeset * Clarify directory type description. Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> * Update .changeset/cyan-radios-attend.md Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> Co-authored-by: Rich Harris <hello@rich-harris.dev>
1 parent d74ef25 commit 9304c9d

File tree

9 files changed

+67
-113
lines changed

9 files changed

+67
-113
lines changed

.changeset/cyan-radios-attend.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@sveltejs/adapter-node': patch
3+
'@sveltejs/adapter-static': patch
4+
'@sveltejs/kit': patch
5+
---
6+
7+
Move `compress` logic to `Builder` API

packages/adapter-node/index.js

Lines changed: 3 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
1-
import { createReadStream, createWriteStream, existsSync, statSync, writeFileSync } from 'fs';
2-
import { pipeline } from 'stream';
3-
import glob from 'tiny-glob';
1+
import { writeFileSync } from 'fs';
42
import { fileURLToPath } from 'url';
5-
import { promisify } from 'util';
6-
import zlib from 'zlib';
7-
8-
const pipe = promisify(pipeline);
93

104
const files = fileURLToPath(new URL('./files', import.meta.url).href);
115

@@ -49,51 +43,9 @@ export default function (opts = {}) {
4943

5044
if (precompress) {
5145
builder.log.minor('Compressing assets');
52-
await compress(`${out}/client`);
53-
await compress(`${out}/prerendered`);
46+
await builder.compress(`${out}/client`);
47+
await builder.compress(`${out}/prerendered`);
5448
}
5549
}
5650
};
5751
}
58-
59-
/**
60-
* @param {string} directory
61-
*/
62-
async function compress(directory) {
63-
if (!existsSync(directory)) {
64-
return;
65-
}
66-
67-
const files = await glob('**/*.{html,js,json,css,svg,xml,wasm}', {
68-
cwd: directory,
69-
dot: true,
70-
absolute: true,
71-
filesOnly: true
72-
});
73-
74-
await Promise.all(
75-
files.map((file) => Promise.all([compress_file(file, 'gz'), compress_file(file, 'br')]))
76-
);
77-
}
78-
79-
/**
80-
* @param {string} file
81-
* @param {'gz' | 'br'} format
82-
*/
83-
async function compress_file(file, format = 'gz') {
84-
const compress =
85-
format == 'br'
86-
? zlib.createBrotliCompress({
87-
params: {
88-
[zlib.constants.BROTLI_PARAM_MODE]: zlib.constants.BROTLI_MODE_TEXT,
89-
[zlib.constants.BROTLI_PARAM_QUALITY]: zlib.constants.BROTLI_MAX_QUALITY,
90-
[zlib.constants.BROTLI_PARAM_SIZE_HINT]: statSync(file).size
91-
}
92-
})
93-
: zlib.createGzip({ level: zlib.constants.Z_BEST_COMPRESSION });
94-
95-
const source = createReadStream(file);
96-
const destination = createWriteStream(`${file}.${format}`);
97-
98-
await pipe(source, compress, destination);
99-
}

packages/adapter-node/package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@
3131
"format": "npm run lint -- --write",
3232
"prepublishOnly": "npm run build"
3333
},
34-
"dependencies": {
35-
"tiny-glob": "^0.2.9"
36-
},
3734
"devDependencies": {
3835
"@rollup/plugin-json": "^4.1.0",
3936
"@sveltejs/kit": "workspace:*",

packages/adapter-static/index.js

Lines changed: 3 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
1-
import { createReadStream, createWriteStream, statSync } from 'fs';
2-
import { pipeline } from 'stream';
3-
import glob from 'tiny-glob';
4-
import { promisify } from 'util';
5-
import zlib from 'zlib';
61
import { platforms } from './platforms.js';
72

8-
const pipe = promisify(pipeline);
9-
103
/** @type {import('.').default} */
114
export default function (options) {
125
return {
@@ -49,13 +42,13 @@ export default function (options) {
4942
if (precompress) {
5043
if (pages === assets) {
5144
builder.log.minor('Compressing assets and pages');
52-
await compress(assets);
45+
await builder.compress(assets);
5346
} else {
5447
builder.log.minor('Compressing assets');
55-
await compress(assets);
48+
await builder.compress(assets);
5649

5750
builder.log.minor('Compressing pages');
58-
await compress(pages);
51+
await builder.compress(pages);
5952
}
6053
}
6154

@@ -69,41 +62,3 @@ export default function (options) {
6962
}
7063
};
7164
}
72-
73-
/**
74-
* @param {string} directory
75-
*/
76-
async function compress(directory) {
77-
const files = await glob('**/*.{html,js,json,css,svg,xml,wasm}', {
78-
cwd: directory,
79-
dot: true,
80-
absolute: true,
81-
filesOnly: true
82-
});
83-
84-
await Promise.all(
85-
files.map((file) => Promise.all([compress_file(file, 'gz'), compress_file(file, 'br')]))
86-
);
87-
}
88-
89-
/**
90-
* @param {string} file
91-
* @param {'gz' | 'br'} format
92-
*/
93-
async function compress_file(file, format = 'gz') {
94-
const compress =
95-
format == 'br'
96-
? zlib.createBrotliCompress({
97-
params: {
98-
[zlib.constants.BROTLI_PARAM_MODE]: zlib.constants.BROTLI_MODE_TEXT,
99-
[zlib.constants.BROTLI_PARAM_QUALITY]: zlib.constants.BROTLI_MAX_QUALITY,
100-
[zlib.constants.BROTLI_PARAM_SIZE_HINT]: statSync(file).size
101-
}
102-
})
103-
: zlib.createGzip({ level: zlib.constants.Z_BEST_COMPRESSION });
104-
105-
const source = createReadStream(file);
106-
const destination = createWriteStream(`${file}.${format}`);
107-
108-
await pipe(source, compress, destination);
109-
}

packages/adapter-static/package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@
2424
"format": "npm run lint -- --write",
2525
"test": "uvu test test.js"
2626
},
27-
"dependencies": {
28-
"tiny-glob": "^0.2.9"
29-
},
3027
"devDependencies": {
3128
"@sveltejs/kit": "workspace:*",
3229
"@types/node": "^16.11.36",

packages/kit/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"dependencies": {
1313
"@sveltejs/vite-plugin-svelte": "^1.0.1",
1414
"chokidar": "^3.5.3",
15-
"sade": "^1.8.1"
15+
"sade": "^1.8.1",
16+
"tiny-glob": "^0.2.9"
1617
},
1718
"devDependencies": {
1819
"@playwright/test": "^1.23.4",

packages/kit/src/core/adapt/builder.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
import glob from 'tiny-glob';
2+
import zlib from 'zlib';
3+
import { existsSync, statSync, createReadStream, createWriteStream } from 'fs';
4+
import { pipeline } from 'stream';
5+
import { promisify } from 'util';
16
import { copy, rimraf, mkdirp } from '../../utils/filesystem.js';
27
import { generate_manifest } from '../generate_manifest/index.js';
38

@@ -25,6 +30,30 @@ export function create_builder({ config, build_data, prerendered, log }) {
2530
return true;
2631
}
2732

33+
const pipe = promisify(pipeline);
34+
35+
/**
36+
* @param {string} file
37+
* @param {'gz' | 'br'} format
38+
*/
39+
async function compress_file(file, format = 'gz') {
40+
const compress =
41+
format == 'br'
42+
? zlib.createBrotliCompress({
43+
params: {
44+
[zlib.constants.BROTLI_PARAM_MODE]: zlib.constants.BROTLI_MODE_TEXT,
45+
[zlib.constants.BROTLI_PARAM_QUALITY]: zlib.constants.BROTLI_MAX_QUALITY,
46+
[zlib.constants.BROTLI_PARAM_SIZE_HINT]: statSync(file).size
47+
}
48+
})
49+
: zlib.createGzip({ level: zlib.constants.Z_BEST_COMPRESSION });
50+
51+
const source = createReadStream(file);
52+
const destination = createWriteStream(`${file}.${format}`);
53+
54+
await pipe(source, compress, destination);
55+
}
56+
2857
return {
2958
log,
3059
rimraf,
@@ -151,6 +180,23 @@ export function create_builder({ config, build_data, prerendered, log }) {
151180
);
152181
},
153182

183+
async compress(directory) {
184+
if (!existsSync(directory)) {
185+
return;
186+
}
187+
188+
const files = await glob('**/*.{html,js,json,css,svg,xml,wasm}', {
189+
cwd: directory,
190+
dot: true,
191+
absolute: true,
192+
filesOnly: true
193+
});
194+
195+
await Promise.all(
196+
files.map((file) => Promise.all([compress_file(file, 'gz'), compress_file(file, 'br')]))
197+
);
198+
},
199+
154200
async prerender() {
155201
throw new Error(
156202
'builder.prerender() has been removed. Prerendering now takes place in the build phase — see builder.prerender and builder.writePrerendered'

packages/kit/types/index.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ export interface Builder {
8080
replace?: Record<string, string>;
8181
}
8282
): string[];
83+
84+
/**
85+
* @param {string} directory Path to the directory containing the files to be compressed
86+
*/
87+
compress(directory: string): void;
8388
}
8489

8590
export interface Config {

pnpm-lock.yaml

Lines changed: 1 addition & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)