Skip to content

Commit

Permalink
refactor(ses): Replace Rollup with Endo bundle (#715)
Browse files Browse the repository at this point in the history
  • Loading branch information
kriskowal authored May 11, 2021
2 parents 7209bd6 + 17406d6 commit df48e60
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import test from 'tape-promise/tape';
import path from 'path';

const runBrowserTests = async (t, indexFile) => {
const browser = await puppeteer.launch();
const browser = await puppeteer.launch({
// debug:
// { headless: false }
});

let numTests;
let numPass;
Expand Down Expand Up @@ -62,7 +65,7 @@ const testBundler = (bundlerName, indexFile) => {
t.notEqual(numTests, undefined);
t.equal(numTests, numPass);
})
.catch(e => t.isNot(e, e, 'unexpected exception'))
.catch(e => t.fail(`Unexpected exception ${e}`))
.finally(() => t.end());
});
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<html>
<head>
<title>Test</title>
<meta charset="utf8">
</head>
<body>
<script src="../../../ses/dist/ses.umd.js"></script>
Expand Down
7 changes: 7 additions & 0 deletions packages/ses/NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ User-visible changes in SES:
The constructor must perform the module analysis and transform the source,
and present this duck-type to the Compartment `importHook`.
This relieves SES of a dependency on Babel and simplifies its API.
- *BREAKING CHANGE* The UMD distribution of SES must have the UTF-8 charset.
The prior versions were accidentally ASCII, so SES would have worked
in any web page, regardless of the charset.
To remedy this, be sure to include `<head><meta charset="utf-8"></head>` in
the containing page (a general best-practice for web pages) or specifically
use `<script charset="utf-8" src="ses.umd.min.js">` to address the single
file.
- Relaxes the censorship of `import` and `eval` in programs evaluated
under SES to specifically allow the use of `something.import()` or
`something.eval()` methods.
Expand Down
7 changes: 7 additions & 0 deletions packages/ses/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ npm install ses

## Usage

SES is a shim that can be run in most engines, either as an ESM module `ses` or
as a `<script>` tag.
For a script tag, the content encoding charset must be UTF-8, either by virtue
of `<head><meta charset="utf-8"></head>` (a general best practice for all HTML
files) or specifically `<script src="node_modules/ses/dist/ses.umd.min.js"
charset="utf-8">`.

### Lockdown

SES introduces the `lockdown()` function.
Expand Down
6 changes: 4 additions & 2 deletions packages/ses/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,15 @@
"test": "yarn build && yarn ava",
"qt": "yarn ava",
"test:platform-compatability": "node test/package/test.cjs",
"build": "rollup --config rollup.config.js && (cd dist; cp ses.cjs lockdown.cjs; cp ses.mjs lockdown.mjs; cp ses.umd.js lockdown.umd.js; cp ses.umd.min.js lockdown.umd.min.js)",
"build": "mkdir -p dist && node scripts/bundle.js",
"demo": "http-server -o /demos"
},
"dependencies": {
"@agoric/make-hardener": "^0.1.2"
},
"devDependencies": {
"@agoric/babel-standalone": "^7.9.5",
"@agoric/compartment-mapper": "^0.2.4",
"@agoric/eslint-config": "^0.1.0",
"@agoric/test262-runner": "~0.1.0",
"@endo/static-module-record": "^0.1.0",
Expand All @@ -66,7 +67,8 @@
"http-server": "^0.12.1",
"prettier": "^1.19.1",
"rollup-plugin-terser": "^5.1.3",
"sinon": "8.0.4"
"sinon": "8.0.4",
"terser": "^4.8.0"
},
"ava": {
"files": [
Expand Down
38 changes: 0 additions & 38 deletions packages/ses/rollup.config.js

This file was deleted.

37 changes: 37 additions & 0 deletions packages/ses/scripts/bundle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import '../index.js';
import fs from 'fs';
import { makeBundle } from '@agoric/compartment-mapper';
import terser from 'terser';

const resolve = (rel, abs) => new URL(rel, abs).toString();
const root = resolve('..', import.meta.url);

const read = async location => fs.promises.readFile(new URL(location).pathname);
const write = async (target, content) => {
const location = resolve(target, root);
await fs.promises.writeFile(new URL(location).pathname, content);
};

(async () => {
const bundle = await makeBundle(
read,
resolve('../index.js', import.meta.url),
);
const { code: terse } = terser.minify(bundle, {
mangle: false,
keep_classnames: true,
});

console.log(`Bundle size: ${bundle.length} bytes`);
console.log(`Minified bundle size: ${terse.length} bytes`);

await write('dist/ses.cjs', bundle);
await write('dist/ses.mjs', bundle);
await write('dist/ses.umd.js', bundle);
await write('dist/ses.umd.min.js', terse);

await write('dist/lockdown.cjs', bundle);
await write('dist/lockdown.mjs', bundle);
await write('dist/lockdown.umd.js', bundle);
await write('dist/lockdown.umd.min.js', terse);
})();

0 comments on commit df48e60

Please sign in to comment.