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

Use Closure compiler for smallest possible bundle. #1

Merged
merged 2 commits into from
Jul 24, 2021
Merged
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
node_modules
*.swp
package-lock.json
sharedb-client-browser*

34 changes: 0 additions & 34 deletions build.js

This file was deleted.

61 changes: 61 additions & 0 deletions build.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { rollup } from 'rollup';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import nodePolyfills from 'rollup-plugin-node-polyfills';
import { terser } from "rollup-plugin-terser";
import compiler from '@ampproject/rollup-plugin-closure-compiler';

// Inspired by
// https://rollupjs.org/guide/en/#rolluprollup
const buildBundle = async ({ inputOptions, outputOptions }) => {
const bundle = await rollup(inputOptions);
const { output } = await bundle.generate(outputOptions);
await bundle.write(outputOptions);
await bundle.close();
};

// Build the client bundle, non-optimized.
const buildClient = async () => {
await buildBundle({
inputOptions: {
input: 'client.js',
plugins: [
commonjs(),
nodePolyfills(),
nodeResolve(),
],
},
outputOptions: {
file: 'sharedb-client-browser.js',
format: 'iife',
},
});
};

// Optimize the size of the bundle.
const buildOptimized = async () => {
await buildBundle({
inputOptions: {
input: 'client.js',
// Results:
// - Terser alone --> 86.2 kB
// - Closure Compiler alone --> 68.0 kB
// - Both --> 67.2 kB
// Went with both.
plugins: [
commonjs(),
nodePolyfills(),
nodeResolve(),
compiler({ compilation_level: 'ADVANCED' }),
terser(),
],
},
outputOptions: {
file: 'sharedb-client-browser.min.js',
format: 'iife'
},
});
};

await buildClient();
await buildOptimized();
Loading