Skip to content

Commit

Permalink
feat(compartment-mapper): expose extraParsers option
Browse files Browse the repository at this point in the history
The `extraParsers` option is an optional mapping of file extension to parser name.  Consumers can use it to associate a file extension with a specific parser, e.g., `{"node": "bytes"}` or override the default mappings.

The specific use-case is enabling creation of compartment map descriptors for Node.js native modules (e.g., `index.node`) _without_ needing a `parsers: {node: 'bytes'}` property in the `package.json` of the package containing the native module.
  • Loading branch information
boneskull committed Mar 28, 2024
1 parent 8f632a2 commit a4d6b59
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 1 deletion.
2 changes: 2 additions & 0 deletions packages/compartment-mapper/src/archive.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ export const loadCompartmentForArchive = async ({
importHook: exitModuleImportHook = undefined,
policy = undefined,
sourceMapHook = undefined,
extraParsers = {},
}) => {
const { read, computeSha512 } = unpackReadPowers(readPowers);
const {
Expand Down Expand Up @@ -357,6 +358,7 @@ export const loadCompartmentForArchive = async ({
makeImportHook,
moduleTransforms,
parserForLanguage,
extraParsers,
archiveOnly: true,
});
await compartment.load(entryModuleSpecifier);
Expand Down
3 changes: 3 additions & 0 deletions packages/compartment-mapper/src/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ function getBundlerKitForModule(module) {
* @param {object} [options.commonDependencies]
* @param {Array<string>} [options.searchSuffixes]
* @param {import('./types.js').SourceMapHook} [options.sourceMapHook]
* @param {Record<string, import('./types.js').Language>} [options.extraParsers]
* @returns {Promise<string>}
*/
export const makeBundle = async (read, moduleLocation, options) => {
Expand All @@ -178,6 +179,7 @@ export const makeBundle = async (read, moduleLocation, options) => {
searchSuffixes,
commonDependencies,
sourceMapHook = undefined,
extraParsers = {},
} = options || {};
const tags = new Set(tagsOption);

Expand Down Expand Up @@ -223,6 +225,7 @@ export const makeBundle = async (read, moduleLocation, options) => {
makeImportHook,
moduleTransforms,
parserForLanguage,
extraParsers,
});
await compartment.load(entryModuleSpecifier);

Expand Down
5 changes: 5 additions & 0 deletions packages/compartment-mapper/src/import-archive.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ const makeFauxModuleExportsNamespace = Compartment => {
* @param {CompartmentConstructor} [options.Compartment]
* @param {import('./types.js').ComputeSourceLocationHook} [options.computeSourceLocation]
* @param {import('./types.js').ComputeSourceMapLocationHook} [options.computeSourceMapLocation]
* @param {Record<string, import('./types.js').Language>} [options.extraParsers]
* @returns {Promise<import('./types.js').Application>}
*/
export const parseArchive = async (
Expand All @@ -265,6 +266,7 @@ export const parseArchive = async (
Compartment = DefaultCompartment,
modules = undefined,
importHook: exitModuleImportHook = undefined,
extraParsers = {},
} = options;

const compartmentExitModuleImportHook = exitModuleImportHookMaker({
Expand Down Expand Up @@ -345,6 +347,7 @@ export const parseArchive = async (
}),
),
Compartment,
extraParsers,
});

await pendingJobsPromise;
Expand All @@ -365,6 +368,7 @@ export const parseArchive = async (
__shimTransforms__,
Compartment,
importHook: exitModuleImportHook,
extraParsers = {},
} = options || {};

const compartmentExitModuleImportHook = exitModuleImportHookMaker({
Expand All @@ -388,6 +392,7 @@ export const parseArchive = async (
transforms,
__shimTransforms__,
Compartment,
extraParsers,
});

await pendingJobsPromise;
Expand Down
2 changes: 2 additions & 0 deletions packages/compartment-mapper/src/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export const loadLocation = async (readPowers, moduleLocation, options) => {
__shimTransforms__,
Compartment,
importHook: exitModuleImportHook,
extraParsers = {},
} = options;
const compartmentExitModuleImportHook = exitModuleImportHookMaker({
modules,
Expand All @@ -101,6 +102,7 @@ export const loadLocation = async (readPowers, moduleLocation, options) => {
moduleTransforms,
__shimTransforms__,
Compartment,
extraParsers,
});

await pendingJobsPromise;
Expand Down
4 changes: 3 additions & 1 deletion packages/compartment-mapper/src/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ export const mapParsers = (
parserForLanguage,
moduleTransforms = {},
) => {
/** @type {[string, string][]} */
const languageForExtensionEntries = [];
const problems = [];
for (const [extension, language] of entries(languageForExtension)) {
Expand Down Expand Up @@ -340,6 +341,7 @@ export const link = (
__shimTransforms__ = [],
archiveOnly = false,
Compartment = defaultCompartment,
extraParsers = {},
},
) => {
const { compartment: entryCompartmentName } = entry;
Expand Down Expand Up @@ -374,7 +376,7 @@ export const link = (
compartmentDescriptor.modules = modules;

const parse = mapParsers(
languageForExtension,
{ ...languageForExtension, ...extraParsers },
languageForModuleSpecifier,
parserForLanguage,
moduleTransforms,
Expand Down
2 changes: 2 additions & 0 deletions packages/compartment-mapper/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ export {};
* @property {ExitModuleImportHook} [importHook]
* @property {Record<string, object>} [attenuations]
* @property {typeof Compartment} [Compartment]
* @property {Record<string, Language>} [extraParsers]
*/

/**
Expand Down Expand Up @@ -384,6 +385,7 @@ export {};
* @property {Array<string>} [searchSuffixes]
* @property {Record<string, string>} [commonDependencies]
* @property {SourceMapHook} [sourceMapHook]
* @property {Record<string, Language>} [extraParsers] Additional mapping of file extension to parser
*/

// /////////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit a4d6b59

Please sign in to comment.