Skip to content

Commit

Permalink
feat(ses): bundle and export shim compatible with Hermes compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
leotm committed Oct 31, 2024
1 parent 586144c commit dda7c59
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 20 deletions.
8 changes: 6 additions & 2 deletions packages/ses/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@
},
"require": {
"types": "./dist/types.d.cts",
"hermes": "./dist/ses-hermes.cjs",
"default": "./dist/ses.cjs"
}
},
"default": "./src/index.js"
},
"./lockdown": {
"import": {
Expand All @@ -63,7 +65,9 @@
"./package.json": "./package.json"
},
"scripts": {
"build": "node scripts/bundle.js",
"build:vanilla": "node scripts/bundle.js",
"build:hermes": "SES_BUILD_TYPE=hermes node scripts/bundle.js",
"build": "yarn build:vanilla && yarn build:hermes",
"clean": "rm -rf dist",
"cover": "c8 ava",
"demo": "python3 -m http.server",
Expand Down
65 changes: 47 additions & 18 deletions packages/ses/scripts/bundle.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
/** @import {ModuleTransforms} from '../../compartment-mapper/types.js' */

/* global process */
import '../index.js';
import fs from 'fs';
import { makeBundle } from '@endo/compartment-mapper/bundle.js';
import { minify } from 'terser';
import { fileURLToPath, pathToFileURL } from 'url';
import { getEnvironmentOption as getenv } from '@endo/env-options';
import { hermesTransforms } from './hermes-transforms.js';

lockdown();

Expand All @@ -16,40 +20,60 @@ const write = async (target, content) => {
await fs.promises.writeFile(location, content);
};

const main = async () => {
/**
* @param {object} [options]
* @param {string} [options.buildType] Suffix used to build special bundles (e.g. 'hermes')
*/
const writeBundle = async ({ buildType } = {}) => {
const text = await fs.promises.readFile(
fileURLToPath(`${root}/package.json`),
'utf8',
);
const packageJson = JSON.parse(text);
const version = packageJson.version;

/** @type ModuleTransforms */
const moduleTransforms = {};

let bundleFilePaths = [
'dist/ses.cjs',
'dist/ses.mjs',
'dist/ses.umd.js',
'dist/lockdown.cjs',
'dist/lockdown.mjs',
'dist/lockdown.umd.js',
];
let terseFilePaths = ['dist/ses.umd.min.js', 'dist/lockdown.umd.min.js'];

if (buildType === 'hermes') {
bundleFilePaths = ['dist/ses-hermes.cjs'];
terseFilePaths = [];
Object.assign(moduleTransforms, hermesTransforms);
}

const bundle = await makeBundle(
read,
pathToFileURL(resolve('../index.js', import.meta.url)).toString(),
{ moduleTransforms },
);
const versionedBundle = `// ses@${version}\n${bundle}`;

const { code: terse } = await minify(versionedBundle, {
mangle: false,
keep_classnames: true,
});
assert.string(terse);

console.log(`-- Building '${buildType}' version of SES --`);
console.log(`Bundle size: ${versionedBundle.length} bytes`);
console.log(`Minified bundle size: ${terse.length} bytes`);

await fs.promises.mkdir('dist', { recursive: true });
/** @type {string|undefined} */
let terse;
if (terseFilePaths.length) {
const { code } = await minify(versionedBundle, {
mangle: false,
keep_classnames: true,
});
terse = code;
assert.string(terse);
console.log(`Minified bundle size: ${terse.length} bytes`);
}

const bundleFilePaths = [
'dist/ses.cjs',
'dist/ses.mjs',
'dist/ses.umd.js',
'dist/lockdown.cjs',
'dist/lockdown.mjs',
'dist/lockdown.umd.js',
];
const terseFilePaths = ['dist/ses.umd.min.js', 'dist/lockdown.umd.min.js'];
await fs.promises.mkdir('dist', { recursive: true });

await Promise.all([
...bundleFilePaths.map(dest => write(dest, versionedBundle)),
Expand Down Expand Up @@ -82,6 +106,11 @@ const main = async () => {
console.log(`Copied ${sourceDTS} to ${destDTS}`);
};

const main = async () => {
const buildType = getenv('SES_BUILD_TYPE', 'vanilla');
await writeBundle({ buildType });
};

main().catch(err => {
console.error('Error running main:', err);
process.exitCode = 1;
Expand Down

0 comments on commit dda7c59

Please sign in to comment.