Skip to content

Commit

Permalink
feat(compartment-mapper): Export all parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
kriskowal committed Oct 29, 2024
1 parent 057fd83 commit 7bbbeeb
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
21 changes: 21 additions & 0 deletions packages/compartment-mapper/NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,27 @@ User-visible changes to `@endo/compartment-mapper`:
- Omits unused module descriptors from `compartment-map.json` in archived
applications, potentially reducing file sizes.

Experimental:

- The module `@endo/compartment-mapper/import-archive-parsers.js` does not
support modules in archives in their original ESM (`mjs`) or CommonJS (`cjs`)
formats because they entrain Babel and a full JavaScript lexer that are
not suitable for use in all environments, specifically XS.
This version introduces an elective
`@endo/compartment-mapper/import-archive-all-parsers.js` that has all of the
precompiled module parsers (`pre-cjs-json` and `pre-mjs-json`) that Endo's
bundler currently produces by default and additionally parsers for original
sources (`mjs`, `cjs`).
Also, provided the `xs` package condition,
`@endo/compartment-mapper/import-archive-parsers.js` now falls through to the
native `ModuleSource` and safely includes `mjs` and `cjs` without entraining
Babel, but is only supported in conjunction with the `__native__` option
for `Compartment`, `importArchive`, `parseArchive`, and `importBundle`.
With the `node` package condition (present by default when running ESM on
`node`), `@endo/compartment-mapper/import-archive-parsers.js` also now
includes `mjs` and `cjs` by entraining Babel, which performs adequately on
that platform.

# v1.3.0 (2024-10-10)

- Adds support for dynamic requires in CommonJS modules. This requires specific
Expand Down
1 change: 1 addition & 0 deletions packages/compartment-mapper/import-archive-all-parsers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { defaultParserForLanguage } from './src/import-archive-all-parsers.js';
7 changes: 6 additions & 1 deletion packages/compartment-mapper/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@
"./capture-lite.js": "./capture-lite.js",
"./import-archive.js": "./import-archive.js",
"./import-archive-lite.js": "./import-archive-lite.js",
"./import-archive-parsers.js": "./import-archive-parsers.js",
"./import-archive-parsers.js": {
"xs": "./import-archive-all-parsers.js",
"node": "./import-archive-all-parsers.js",
"default": "./import-archive-parsers.js"
},
"./import-archive-all-parsers.js": "./import-archive-all-parsers.js",
"./bundle.js": "./bundle.js",
"./node-powers.js": "./node-powers.js",
"./node-modules.js": "./node-modules.js",
Expand Down
29 changes: 29 additions & 0 deletions packages/compartment-mapper/src/import-archive-all-parsers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* Provides a set of default language behaviors (parsers) suitable for
* evaluating archives (zip files with a `compartment-map.json` and a file for
* each module) with pre-compiled _or_ original ESM and CommonJS.
*
* This module does not entrain a dependency on Babel on XS, but does on other
* platforms like Node.js.
*/
/** @import {ParserForLanguage} from './types.js' */

import parserPreCjs from './parse-pre-cjs.js';
import parserJson from './parse-json.js';
import parserText from './parse-text.js';
import parserBytes from './parse-bytes.js';
import parserPreMjs from './parse-pre-mjs.js';
import parserMjs from './parse-mjs.js';
import parserCjs from './parse-cjs.js';

/** @satisfies {Readonly<ParserForLanguage>} */
export const defaultParserForLanguage = Object.freeze(
/** @type {const} */ ({
'pre-cjs-json': parserPreCjs,
'pre-mjs-json': parserPreMjs,
cjs: parserCjs,
mjs: parserMjs,
json: parserJson,
text: parserText,
bytes: parserBytes,
}),
);

0 comments on commit 7bbbeeb

Please sign in to comment.