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: add precompress option to adapter-static #3079

Merged
merged 2 commits into from
Dec 22, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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/healthy-pandas-pull.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-static': minor
---

add precompress option to adapter-static
1 change: 1 addition & 0 deletions packages/adapter-static/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ interface AdapterOptions {
pages?: string;
assets?: string;
fallback?: string;
precompress?: boolean;
}

declare function plugin(options?: AdapterOptions): Adapter;
Expand Down
61 changes: 60 additions & 1 deletion packages/adapter-static/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { createReadStream, createWriteStream, statSync } from 'fs';
import { pipeline } from 'stream';
import glob from 'tiny-glob';
import { promisify } from 'util';
import zlib from 'zlib';

const pipe = promisify(pipeline);

/** @type {import('.')} */
export default function ({ pages = 'build', assets = pages, fallback } = {}) {
export default function ({ pages = 'build', assets = pages, fallback, precompress = true } = {}) {
Rich-Harris marked this conversation as resolved.
Show resolved Hide resolved
return {
name: '@sveltejs/adapter-static',

Expand All @@ -15,6 +23,57 @@ export default function ({ pages = 'build', assets = pages, fallback } = {}) {
all: !fallback,
dest: pages
});

if (precompress) {
if (pages === assets) {
utils.log.minor('Compressing assets and pages');
await compress(assets);
} else {
utils.log.minor('Compressing assets');
await compress(assets);

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

/**
* @param {string} directory
*/
async function compress(directory) {
const files = await glob('**/*.{html,js,json,css,svg,xml}', {
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: 3 additions & 0 deletions packages/adapter-static/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
"check-format": "prettier --check . --config ../../.prettierrc --ignore-path .gitignore",
"test": "uvu test test.js"
},
"dependencies": {
"tiny-glob": "^0.2.9"
},
"devDependencies": {
"@sveltejs/kit": "workspace:*",
"playwright-chromium": "^1.17.0",
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

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