support ES6 default require for packages (#21112)

This allows loading the packages that are Es module and export their function wrapped in a default object. This is the case for any package that uses Babel 6, Babel 7, or other modern compilers for transpiling their package.
This commit is contained in:
Amin Yahyaabadi 2020-11-18 01:57:21 -06:00 committed by GitHub
parent d93f2dd039
commit bd189ebce9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 3 deletions

21
src/module-utils.js Normal file
View File

@ -0,0 +1,21 @@
// a require function with both ES5 and ES6 default export support
function requireModule(path) {
const modul = require(path);
if (modul === null || modul === undefined) {
// if null do not bother
return modul;
} else {
if (
modul.__esModule === true &&
(modul.default !== undefined && modul.default !== null)
) {
// __esModule flag is true and default is exported, which means that
// an object containing the main functions (e.g. activate, etc) is default exported
return modul.default;
} else {
return modul;
}
}
}
exports.requireModule = requireModule;

View File

@ -8,6 +8,7 @@ const dedent = require('dedent');
const CompileCache = require('./compile-cache');
const ModuleCache = require('./module-cache');
const BufferedProcess = require('./buffered-process');
const { requireModule } = require('./module-utils');
// Extended: Loads and activates a package's main module and resources such as
// stylesheets, keymaps, grammar, editor properties, and menus.
@ -881,8 +882,9 @@ module.exports = class Package {
requireMainModule() {
if (this.bundledPackage && this.packageManager.packagesCache[this.name]) {
if (this.packageManager.packagesCache[this.name].main) {
this.mainModule = require(this.packageManager.packagesCache[this.name]
.main);
this.mainModule = requireModule(
this.packageManager.packagesCache[this.name].main
);
return this.mainModule;
}
} else if (this.mainModuleRequired) {
@ -904,7 +906,7 @@ module.exports = class Package {
const previousViewProviderCount = this.viewRegistry.getViewProviderCount();
const previousDeserializerCount = this.deserializerManager.getDeserializerCount();
this.mainModule = require(mainModulePath);
this.mainModule = requireModule(mainModulePath);
if (
this.viewRegistry.getViewProviderCount() ===
previousViewProviderCount &&