From dda7c59b04da82f9be4faedf0d937d8ef6a96e7c Mon Sep 17 00:00:00 2001 From: LeoTM <1881059+leotm@users.noreply.github.com> Date: Mon, 30 Sep 2024 17:19:50 +0100 Subject: [PATCH] feat(ses): bundle and export shim compatible with Hermes compiler --- packages/ses/package.json | 8 +++-- packages/ses/scripts/bundle.js | 65 ++++++++++++++++++++++++---------- 2 files changed, 53 insertions(+), 20 deletions(-) diff --git a/packages/ses/package.json b/packages/ses/package.json index 6e5bf418c6..e3eb14efa8 100644 --- a/packages/ses/package.json +++ b/packages/ses/package.json @@ -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": { @@ -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", diff --git a/packages/ses/scripts/bundle.js b/packages/ses/scripts/bundle.js index 9b330299f1..c275fdd286 100644 --- a/packages/ses/scripts/bundle.js +++ b/packages/ses/scripts/bundle.js @@ -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(); @@ -16,7 +20,11 @@ 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', @@ -24,32 +32,48 @@ const main = async () => { 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)), @@ -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;