Skip to content

Commit

Permalink
Move compress inside kit (#5822)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
3 people committed Aug 6, 2022
1 parent d74ef25 commit 9304c9d
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 113 deletions.
7 changes: 7 additions & 0 deletions .changeset/cyan-radios-attend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@sveltejs/adapter-node': patch
'@sveltejs/adapter-static': patch
'@sveltejs/kit': patch
---

Move `compress` logic to `Builder` API
54 changes: 3 additions & 51 deletions packages/adapter-node/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import { createReadStream, createWriteStream, existsSync, statSync, writeFileSync } from 'fs';
import { pipeline } from 'stream';
import glob from 'tiny-glob';
import { writeFileSync } from 'fs';
import { fileURLToPath } from 'url';
import { promisify } from 'util';
import zlib from 'zlib';

const pipe = promisify(pipeline);

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

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

if (precompress) {
builder.log.minor('Compressing assets');
await compress(`${out}/client`);
await compress(`${out}/prerendered`);
await builder.compress(`${out}/client`);
await builder.compress(`${out}/prerendered`);
}
}
};
}

/**
* @param {string} directory
*/
async function compress(directory) {
if (!existsSync(directory)) {
return;
}

const files = await glob('**/*.{html,js,json,css,svg,xml,wasm}', {
cwd: directory,
dot: true,
absolute: true,
filesOnly: true
});

await Promise.all(
files.map((file) => Promise.all([compress_file(file, 'gz'), compress_file(file, 'br')]))
);
}

/**
* @param {string} file
* @param {'gz' | 'br'} format
*/
async function compress_file(file, format = 'gz') {
const compress =
format == 'br'
? zlib.createBrotliCompress({
params: {
[zlib.constants.BROTLI_PARAM_MODE]: zlib.constants.BROTLI_MODE_TEXT,
[zlib.constants.BROTLI_PARAM_QUALITY]: zlib.constants.BROTLI_MAX_QUALITY,
[zlib.constants.BROTLI_PARAM_SIZE_HINT]: statSync(file).size
}
})
: zlib.createGzip({ level: zlib.constants.Z_BEST_COMPRESSION });

const source = createReadStream(file);
const destination = createWriteStream(`${file}.${format}`);

await pipe(source, compress, destination);
}
3 changes: 0 additions & 3 deletions packages/adapter-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@
"format": "npm run lint -- --write",
"prepublishOnly": "npm run build"
},
"dependencies": {
"tiny-glob": "^0.2.9"
},
"devDependencies": {
"@rollup/plugin-json": "^4.1.0",
"@sveltejs/kit": "workspace:*",
Expand Down
51 changes: 3 additions & 48 deletions packages/adapter-static/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import { createReadStream, createWriteStream, statSync } from 'fs';
import { pipeline } from 'stream';
import glob from 'tiny-glob';
import { promisify } from 'util';
import zlib from 'zlib';
import { platforms } from './platforms.js';

const pipe = promisify(pipeline);

/** @type {import('.').default} */
export default function (options) {
return {
Expand Down Expand Up @@ -49,13 +42,13 @@ export default function (options) {
if (precompress) {
if (pages === assets) {
builder.log.minor('Compressing assets and pages');
await compress(assets);
await builder.compress(assets);
} else {
builder.log.minor('Compressing assets');
await compress(assets);
await builder.compress(assets);

builder.log.minor('Compressing pages');
await compress(pages);
await builder.compress(pages);
}
}

Expand All @@ -69,41 +62,3 @@ export default function (options) {
}
};
}

/**
* @param {string} directory
*/
async function compress(directory) {
const files = await glob('**/*.{html,js,json,css,svg,xml,wasm}', {
cwd: directory,
dot: true,
absolute: true,
filesOnly: true
});

await Promise.all(
files.map((file) => Promise.all([compress_file(file, 'gz'), compress_file(file, 'br')]))
);
}

/**
* @param {string} file
* @param {'gz' | 'br'} format
*/
async function compress_file(file, format = 'gz') {
const compress =
format == 'br'
? zlib.createBrotliCompress({
params: {
[zlib.constants.BROTLI_PARAM_MODE]: zlib.constants.BROTLI_MODE_TEXT,
[zlib.constants.BROTLI_PARAM_QUALITY]: zlib.constants.BROTLI_MAX_QUALITY,
[zlib.constants.BROTLI_PARAM_SIZE_HINT]: statSync(file).size
}
})
: zlib.createGzip({ level: zlib.constants.Z_BEST_COMPRESSION });

const source = createReadStream(file);
const destination = createWriteStream(`${file}.${format}`);

await pipe(source, compress, destination);
}
3 changes: 0 additions & 3 deletions packages/adapter-static/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@
"format": "npm run lint -- --write",
"test": "uvu test test.js"
},
"dependencies": {
"tiny-glob": "^0.2.9"
},
"devDependencies": {
"@sveltejs/kit": "workspace:*",
"@types/node": "^16.11.36",
Expand Down
3 changes: 2 additions & 1 deletion packages/kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"dependencies": {
"@sveltejs/vite-plugin-svelte": "^1.0.1",
"chokidar": "^3.5.3",
"sade": "^1.8.1"
"sade": "^1.8.1",
"tiny-glob": "^0.2.9"
},
"devDependencies": {
"@playwright/test": "^1.23.4",
Expand Down
46 changes: 46 additions & 0 deletions packages/kit/src/core/adapt/builder.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import glob from 'tiny-glob';
import zlib from 'zlib';
import { existsSync, statSync, createReadStream, createWriteStream } from 'fs';
import { pipeline } from 'stream';
import { promisify } from 'util';
import { copy, rimraf, mkdirp } from '../../utils/filesystem.js';
import { generate_manifest } from '../generate_manifest/index.js';

Expand Down Expand Up @@ -25,6 +30,30 @@ export function create_builder({ config, build_data, prerendered, log }) {
return true;
}

const pipe = promisify(pipeline);

/**
* @param {string} file
* @param {'gz' | 'br'} format
*/
async function compress_file(file, format = 'gz') {
const compress =
format == 'br'
? zlib.createBrotliCompress({
params: {
[zlib.constants.BROTLI_PARAM_MODE]: zlib.constants.BROTLI_MODE_TEXT,
[zlib.constants.BROTLI_PARAM_QUALITY]: zlib.constants.BROTLI_MAX_QUALITY,
[zlib.constants.BROTLI_PARAM_SIZE_HINT]: statSync(file).size
}
})
: zlib.createGzip({ level: zlib.constants.Z_BEST_COMPRESSION });

const source = createReadStream(file);
const destination = createWriteStream(`${file}.${format}`);

await pipe(source, compress, destination);
}

return {
log,
rimraf,
Expand Down Expand Up @@ -151,6 +180,23 @@ export function create_builder({ config, build_data, prerendered, log }) {
);
},

async compress(directory) {
if (!existsSync(directory)) {
return;
}

const files = await glob('**/*.{html,js,json,css,svg,xml,wasm}', {
cwd: directory,
dot: true,
absolute: true,
filesOnly: true
});

await Promise.all(
files.map((file) => Promise.all([compress_file(file, 'gz'), compress_file(file, 'br')]))
);
},

async prerender() {
throw new Error(
'builder.prerender() has been removed. Prerendering now takes place in the build phase — see builder.prerender and builder.writePrerendered'
Expand Down
5 changes: 5 additions & 0 deletions packages/kit/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ export interface Builder {
replace?: Record<string, string>;
}
): string[];

/**
* @param {string} directory Path to the directory containing the files to be compressed
*/
compress(directory: string): void;
}

export interface Config {
Expand Down
8 changes: 1 addition & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9304c9d

Please sign in to comment.