Skip to content

Commit

Permalink
GH-2992: Changed the way we resolve an extension.
Browse files Browse the repository at this point in the history
Instead of creating a new file on the fly with the corresponding resolve
code, we use `require.resolve`'s
`paths` option.

It was introduced in Node.js `v8.9.0`.
https://nodejs.org/api/modules.html#modules_require_resolve_paths_request

Closes #2992.

Signed-off-by: Akos Kitta <kittaakos@typefox.io>
  • Loading branch information
Akos Kitta committed Apr 24, 2019
1 parent 7087a38 commit f8137fe
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 12 deletions.
13 changes: 1 addition & 12 deletions dev-packages/application-package/src/application-package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import * as fs from 'fs-extra';
import * as paths from 'path';
import { readJsonFile, writeJsonFile } from './json-file';
import { NpmRegistry, NodePackage, PublishedNodePackage, sortByKey } from './npm-registry';
import { Extension, ExtensionPackage, RawExtensionPackage } from './extension-package';
import { ExtensionPackageCollector } from './extension-package-collector';
import { ApplicationProps } from './application-props';
import { environment } from './environment';

// tslint:disable:no-implicit-dependencies

Expand Down Expand Up @@ -261,16 +259,7 @@ export class ApplicationPackage {
*/
get resolveModule(): ApplicationModuleResolver {
if (!this._moduleResolver) {
// If running a bundled electron application, we cannot create a file for the module on the fly.
// https://github.com/theia-ide/theia/issues/2992
if (environment.electron.is() && !environment.electron.isDevMode()) {
this._moduleResolver = modulePath => require.resolve(modulePath);
} else {
const loaderPath = this.path('.application-module-loader.js');
fs.writeFileSync(loaderPath, 'module.exports = modulePath => require.resolve(modulePath);');
this._moduleResolver = require(loaderPath) as ApplicationModuleResolver;
fs.removeSync(loaderPath);
}
this._moduleResolver = modulePath => require.resolve(modulePath, { paths: [this.packagePath || process.cwd()] });
}
return this._moduleResolver!;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export class ExtensionPackageCollector {
try {
packagePath = this.resolveModule(name + '/package.json');
} catch (error) {
console.error(error);
console.warn(`Failed to resolve module: ${name}`);
}
if (!packagePath) {
Expand Down

0 comments on commit f8137fe

Please sign in to comment.