-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add possibility to configure how extensions should be loaded
Fixes avajs#2345 by adding an object form for the `"extensions"` configuration. This implementation: 1. if `"extensions"` is not defined uses the default module configuration for `mjs`, `cjs`, and `js` 2. generates the module types from the array form of the `extensions` configuration keeping the default module type for `mjs`, `cjs`, and `js` if they're defined and uses `"commonjs"` for everything else, thus maintaining backwords compatibility. 3. generates the module types from the object form of the `extensions` configuration and prevents changing the module type for `mjs`, `cjs`, and `js` extensions (for `js` the `"type"` field cannot be overridden).
- Loading branch information
Showing
3 changed files
with
71 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
module.exports = (configuredExtensions, defaultModuleType) => { | ||
const lockedModuleTypes = { | ||
cjs: 'commonjs', | ||
mjs: 'module', | ||
js: defaultModuleType | ||
}; | ||
|
||
const createModuleTypesFromExtensionsObject = extensionsObject => { | ||
const configuredModuleTypes = {}; | ||
|
||
for (const [extension, type] of Object.entries(extensionsObject)) { | ||
if (extension in lockedModuleTypes && type !== lockedModuleTypes[extension]) { | ||
throw new TypeError(`.${extension} files can only be configured as ’${lockedModuleTypes[extension]}’${ | ||
extension === 'js' ? | ||
` when the nearest parent package.json ${type === 'module' ? 'does not contain' : 'contains'} "type": "module"` : '' | ||
}, found ’${type}’ instead.`); | ||
} | ||
|
||
configuredModuleTypes[extension] = type; | ||
} | ||
|
||
return configuredModuleTypes; | ||
}; | ||
|
||
const createModuleTypesFromExtensionsArray = extensions => { | ||
const generatedModuleTypes = {}; | ||
|
||
for (const extension of extensions) { | ||
if (extension in lockedModuleTypes) { | ||
generatedModuleTypes[extension] = lockedModuleTypes[extension]; | ||
} else { | ||
generatedModuleTypes[extension] = 'commonjs'; | ||
} | ||
} | ||
|
||
return generatedModuleTypes; | ||
}; | ||
|
||
let moduleTypes; | ||
if (configuredExtensions === undefined) { | ||
moduleTypes = { | ||
...lockedModuleTypes | ||
}; | ||
} else if (Array.isArray(configuredExtensions)) { | ||
moduleTypes = createModuleTypesFromExtensionsArray(configuredExtensions); | ||
} else { | ||
moduleTypes = createModuleTypesFromExtensionsObject(configuredExtensions); | ||
} | ||
|
||
return moduleTypes; | ||
}; |