Skip to content

Commit

Permalink
feat(compartment-mapper): Workspace language-for-extension options
Browse files Browse the repository at this point in the history
  • Loading branch information
kriskowal committed Nov 8, 2024
1 parent 4ea0f5a commit d6ce6de
Show file tree
Hide file tree
Showing 16 changed files with 709 additions and 57 deletions.
24 changes: 24 additions & 0 deletions packages/compartment-mapper/src/archive.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ export const makeArchive = async (powers, moduleLocation, options = {}) => {
languageForExtension,
commonjsLanguageForExtension,
moduleLanguageForExtension,
workspaceLanguageForExtension,
workspaceCommonjsLanguageForExtension,
workspaceModuleLanguageForExtension,
parserForLanguage,
languages,
...otherOptions
Expand All @@ -102,6 +105,9 @@ export const makeArchive = async (powers, moduleLocation, options = {}) => {
languageForExtension,
commonjsLanguageForExtension,
moduleLanguageForExtension,
workspaceLanguageForExtension,
workspaceCommonjsLanguageForExtension,
workspaceModuleLanguageForExtension,
languages,
});

Expand Down Expand Up @@ -130,6 +136,9 @@ export const mapLocation = async (powers, moduleLocation, options = {}) => {
languageForExtension,
commonjsLanguageForExtension,
moduleLanguageForExtension,
workspaceLanguageForExtension,
workspaceCommonjsLanguageForExtension,
workspaceModuleLanguageForExtension,
...otherOptions
} = assignParserForLanguage(options);

Expand All @@ -142,6 +151,9 @@ export const mapLocation = async (powers, moduleLocation, options = {}) => {
languageForExtension,
commonjsLanguageForExtension,
moduleLanguageForExtension,
workspaceLanguageForExtension,
workspaceCommonjsLanguageForExtension,
workspaceModuleLanguageForExtension,
});

return mapFromMap(powers, compartmentMap, {
Expand Down Expand Up @@ -169,6 +181,9 @@ export const hashLocation = async (powers, moduleLocation, options = {}) => {
languageForExtension,
commonjsLanguageForExtension,
moduleLanguageForExtension,
workspaceLanguageForExtension,
workspaceCommonjsLanguageForExtension,
workspaceModuleLanguageForExtension,
...otherOptions
} = assignParserForLanguage(options);

Expand All @@ -181,6 +196,9 @@ export const hashLocation = async (powers, moduleLocation, options = {}) => {
languageForExtension,
commonjsLanguageForExtension,
moduleLanguageForExtension,
workspaceLanguageForExtension,
workspaceCommonjsLanguageForExtension,
workspaceModuleLanguageForExtension,
});

return hashFromMap(powers, compartmentMap, {
Expand Down Expand Up @@ -215,6 +233,9 @@ export const writeArchive = async (
languageForExtension,
commonjsLanguageForExtension,
moduleLanguageForExtension,
workspaceLanguageForExtension,
workspaceCommonjsLanguageForExtension,
workspaceModuleLanguageForExtension,
...otherOptions
} = assignParserForLanguage(options);
const compartmentMap = await mapNodeModules(readPowers, moduleLocation, {
Expand All @@ -225,6 +246,9 @@ export const writeArchive = async (
languageForExtension,
commonjsLanguageForExtension,
moduleLanguageForExtension,
workspaceLanguageForExtension,
workspaceCommonjsLanguageForExtension,
workspaceModuleLanguageForExtension,
languages,
});
return writeArchiveFromMap(
Expand Down
23 changes: 23 additions & 0 deletions packages/compartment-mapper/src/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,11 @@ export const makeBundle = async (readPowers, moduleLocation, options) => {
languageForExtension: languageForExtensionOption = {},
commonjsLanguageForExtension: commonjsLanguageForExtensionOption = {},
moduleLanguageForExtension: moduleLanguageForExtensionOption = {},
workspaceLanguageForExtension: workspaceLanguageForExtensionOption = {},
workspaceCommonjsLanguageForExtension:
workspaceCommonjsLanguageForExtensionOption = {},
workspaceModuleLanguageForExtension:
workspaceModuleLanguageForExtensionOption = {},
} = options || {};
const conditions = new Set(conditionsOption);

Expand All @@ -256,6 +261,21 @@ export const makeBundle = async (readPowers, moduleLocation, options) => {
const moduleLanguageForExtension = Object.freeze(
Object.assign(Object.create(null), moduleLanguageForExtensionOption),
);
const workspaceLanguageForExtension = Object.freeze(
Object.assign(Object.create(null), workspaceLanguageForExtensionOption),
);
const workspaceCommonjsLanguageForExtension = Object.freeze(
Object.assign(
Object.create(null),
workspaceCommonjsLanguageForExtensionOption,
),
);
const workspaceModuleLanguageForExtension = Object.freeze(
Object.assign(
Object.create(null),
workspaceModuleLanguageForExtensionOption,
),
);

const {
packageLocation,
Expand All @@ -280,6 +300,9 @@ export const makeBundle = async (readPowers, moduleLocation, options) => {
languageForExtension,
commonjsLanguageForExtension,
moduleLanguageForExtension,
workspaceLanguageForExtension,
workspaceCommonjsLanguageForExtension,
workspaceModuleLanguageForExtension,
},
);

Expand Down
6 changes: 6 additions & 0 deletions packages/compartment-mapper/src/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ export const loadLocation = async (
languageForExtension,
commonjsLanguageForExtension,
moduleLanguageForExtension,
workspaceLanguageForExtension,
workspaceCommonjsLanguageForExtension,
workspaceModuleLanguageForExtension,
...otherOptions
} = assignParserForLanguage(options);
// conditions are not present in SyncArchiveOptions
Expand All @@ -96,6 +99,9 @@ export const loadLocation = async (
languageForExtension,
commonjsLanguageForExtension,
moduleLanguageForExtension,
workspaceLanguageForExtension,
workspaceCommonjsLanguageForExtension,
workspaceModuleLanguageForExtension,
languages,
});
return loadFromMap(readPowers, compartmentMap, {
Expand Down
44 changes: 42 additions & 2 deletions packages/compartment-mapper/src/node-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@
* @typedef {object} LanguageOptions
* @property {LanguageForExtension} commonjsLanguageForExtension
* @property {LanguageForExtension} moduleLanguageForExtension
* @property {LanguageForExtension} workspaceCommonjsLanguageForExtension
* @property {LanguageForExtension} workspaceModuleLanguageForExtension
* @property {Set<string>} languages
*/

Expand Down Expand Up @@ -217,12 +219,22 @@ const defaultModuleLanguageForExtension = /** @type {const} */ ({
* @returns {Record<string, string>}
*/
const inferParsers = (descriptor, location, languageOptions) => {
let { moduleLanguageForExtension, commonjsLanguageForExtension } =
languageOptions;
const {
languages,
moduleLanguageForExtension,
commonjsLanguageForExtension,
workspaceModuleLanguageForExtension,
workspaceCommonjsLanguageForExtension,
} = languageOptions;

// Select languageForExtension options based on whether they are physically
// under node_modules, indicating that they have not been built for npm,
// so any languages that compile to JavaScript may need additional parsers.
if (!location.includes('/node_modules/')) {
moduleLanguageForExtension = workspaceModuleLanguageForExtension;
commonjsLanguageForExtension = workspaceCommonjsLanguageForExtension;
}

const {
type,
module,
Expand Down Expand Up @@ -771,13 +783,21 @@ const translateGraph = (
* 'languageForExtension' |
* 'moduleLanguageForExtension' |
* 'commonjsLanguageForExtension' |
* 'workspaceLanguageForExtension' |
* 'workspaceModuleLanguageForExtension' |
* 'workspaceCommonjsLanguageForExtension' |
* 'languages'
* >} options
*/
const makeLanguageOptions = ({
languageForExtension: additionalLanguageForExtension = {},
moduleLanguageForExtension: additionalModuleLanguageForExtension = {},
commonjsLanguageForExtension: additionalCommonjsLanguageForExtension = {},
workspaceLanguageForExtension: additionalWorkspaceLanguageForExtension = {},
workspaceModuleLanguageForExtension:
additionalWorkspaceModuleLanguageForExtension = {},
workspaceCommonjsLanguageForExtension:
additionalWorkspaceCommonjsLanguageForExtension = {},
languages: additionalLanguages = [],
}) => {
const commonjsLanguageForExtension = {
Expand All @@ -792,17 +812,37 @@ const makeLanguageOptions = ({
...defaultModuleLanguageForExtension,
...additionalModuleLanguageForExtension,
};
const workspaceCommonjsLanguageForExtension = {
...defaultLanguageForExtension,
...additionalLanguageForExtension,
...defaultCommonjsLanguageForExtension,
...additionalCommonjsLanguageForExtension,
...additionalWorkspaceLanguageForExtension,
...additionalWorkspaceCommonjsLanguageForExtension,
};
const workspaceModuleLanguageForExtension = {
...defaultLanguageForExtension,
...additionalLanguageForExtension,
...defaultModuleLanguageForExtension,
...additionalModuleLanguageForExtension,
...additionalWorkspaceLanguageForExtension,
...additionalWorkspaceModuleLanguageForExtension,
};

const languages = new Set([
...Object.values(moduleLanguageForExtension),
...Object.values(commonjsLanguageForExtension),
...Object.values(workspaceModuleLanguageForExtension),
...Object.values(workspaceCommonjsLanguageForExtension),
...additionalLanguages,
]);

return {
languages,
commonjsLanguageForExtension,
moduleLanguageForExtension,
workspaceCommonjsLanguageForExtension,
workspaceModuleLanguageForExtension,
};
};

Expand Down
16 changes: 14 additions & 2 deletions packages/compartment-mapper/src/types/external.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,22 @@ type MapNodeModulesOptionsOmitPolicy = Partial<{
commonDependencies: Record<string, string>;
/** Maps extensions to languages for all packages, like `txt` to `text` */
languageForExtension: LanguageForExtension;
/** Maps extensions to languages for all type=module packages */
/** Maps additional extensions to languages for all type=module packages */
moduleLanguageForExtension: LanguageForExtension;
/** Maps extensions to languages for all type=commonjs packages (default) */
/** Maps additional extensions to languages for all type=commonjs packages (default) */
commonjsLanguageForExtension: LanguageForExtension;
/** Maps extensions to languages for packages not under node_modules */
workspaceLanguageForExtension: LanguageForExtension;
/**
* Maps additional extensions to languages for all type=module packages that
* are not under node_modules
*/
workspaceModuleLanguageForExtension: LanguageForExtension;
/**
* Maps additional extensions to languages for all type=commonjs packages
* (default)
*/
workspaceCommonjsLanguageForExtension: LanguageForExtension;
/**
* Accounts for languages not present as values in any of the extension to
* language mappings.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports({ meaning: 42 });
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "app",
"type": "commonjs",
"scripts": {
"preinstall": "echo DO NOT INSTALL TEST FIXTURES; exit -1"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports({ meaning: 42 });
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "app",
"type": "module",
"scripts": {
"preinstall": "echo DO NOT INSTALL TEST FIXTURES; exit -1"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports({ meaning: 42 });
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "app",
"parsers": {
"xsonp": "jsonp"
},
"scripts": {
"preinstall": "echo DO NOT INSTALL TEST FIXTURES; exit -1"
}
}
Loading

0 comments on commit d6ce6de

Please sign in to comment.