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

Move compress inside kit #5822

Merged
merged 6 commits into from
Aug 6, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
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 inside kit
Rich-Harris marked this conversation as resolved.
Show resolved Hide resolved
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.