fix(compartment-mapper): avoid mislabeling cjs files as esm based on type field
MODIFY
packages/compartment-mapper/src/link.js
packages/compartment-mapper/src/link.js
@@ -54,6 +54,14 @@ const q = JSON.stringify;
*/
const has = (object, key) => apply(hasOwnProperty, object, [key]);
+/**
+ * Decide if extension is clearly indicating a parser/language for a file
+ *
+ * @param {string} extension
+ * @returns {boolean}
+ */
+const extensionImpliesLanguage = extension => extension !== 'js';
+
/**
* `makeExtensionParser` produces a `parser` that parses the content of a
* module according to the corresponding module language, given the extension
@@ -79,10 +87,14 @@ const makeExtensionParser = (
) => {
return async (bytes, specifier, location, packageLocation, options) => {
let language;
- if (has(languageForModuleSpecifier, specifier)) {
+ const extension = parseExtension(location);
+
+ if (
+ !extensionImpliesLanguage(extension) &&
+ has(languageForModuleSpecifier, specifier)
+ ) {
language = languageForModuleSpecifier[specifier];
} else {
- const extension = parseExtension(location);
if (!has(languageForExtension, extension)) {
throw new Error(
`Cannot parse module ${specifier} at ${location}, no parser configured for extension ${extension}`,

MODIFY
packages/compartment-mapper/test/fixtures-nested-pkg/node_modules/app/index.js
packages/compartment-mapper/test/fixtures-nested-pkg/node_modules/app/index.js
import { isOk as file1 } from "apackage/file";
import { isOk as file2 } from "apackage/file2";
+import * as bpackage from "bpackage";
\ No newline at end of file

MODIFY
packages/compartment-mapper/test/fixtures-nested-pkg/node_modules/app/package.json
packages/compartment-mapper/test/fixtures-nested-pkg/node_modules/app/package.json
"main": "./index.js",
"type": "module",
"dependencies": {
- "apackage": "^1.0.0"
+ "apackage": "^1.0.0",
+ "bpackage": "^1.0.0"
}
}

ADD
packages/compartment-mapper/test/fixtures-nested-pkg/node_modules/bpackage/a.cjs
+exports.a = 42;
\ No newline at end of file

ADD
packages/compartment-mapper/test/fixtures-nested-pkg/node_modules/bpackage/package.json
+{
+ "name": "bpackage",
+ "type": "module",
+ "main": "a.cjs"
+}
\ No newline at end of file

fix(compartment-mapper): avoid mislabeling cjs files as esm based on type field