Skip to content

Commit

Permalink
Revert "module: add require.resolve (#445)" (#485)
Browse files Browse the repository at this point in the history
This reverts commit 7807fe4.
This causes the `require` performance problems.
  • Loading branch information
yorkie authored and legendecas committed Jan 16, 2019
1 parent 9fa6808 commit 25e9fee
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 117 deletions.
18 changes: 0 additions & 18 deletions benchmark/module/require.js

This file was deleted.

18 changes: 0 additions & 18 deletions benchmark/module/require.resolve.js

This file was deleted.

6 changes: 1 addition & 5 deletions docs/api/Module.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,8 @@ For each directory in search paths above:
- If `id/package.json` contains **main** property, load the file named **main** property.
- If `id/package.json` exists, but neither the **main** property nor the file named **main** property exist, load `index.js`.

### require.resolve(request)
* `request` {string} Module name to be queried.

Get the module absolute path about `request` module but not load it.

### require.main
* accessing the main module

When a file is run directly, `require.main` is set to its module. That means that it is possible to determine whether a file has been run directly by testing `require.main === module`. For a file foo.js, this will be true if run via cmd directly, but false if run by `require('./foo')`, because `module` object is set to the `foo` module but `require.main` is still your entry file module. So you can get the entry point of the current application from checking `require.main.filename` anywhere.

49 changes: 8 additions & 41 deletions src/js/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,21 +126,18 @@ iotjs_module_t.resolveFilepath = function(id, directories) {
};


iotjs_module_t._resolveFilepath = function(id, root, ext_index) {
iotjs_module_t._resolveFilepath = function(id, root) {
var modulePath = root ? path.join(root, id) : id;
var filepath;
var exts = ['.js', '.json', '.node'];
if (ext_index === undefined) {
ext_index = 0;
}
var ext = '.js';

// id[.ext]
if (filepath = tryPath(modulePath, exts[ext_index])) {
if (filepath = tryPath(modulePath, ext)) {
return filepath;
}

// id/index[.ext]
if (filepath = tryPath(modulePath + '/index', exts[ext_index])) {
if (filepath = tryPath(modulePath + '/index', ext)) {
return filepath;
}

Expand All @@ -152,14 +149,10 @@ iotjs_module_t._resolveFilepath = function(id, root, ext_index) {
var pkgMainFile = JSON.parse(pkgSrc).main;

// pkgmain[.ext]
if (filepath = tryPath(modulePath + '/' + pkgMainFile, exts[ext_index])) {
if (filepath = tryPath(modulePath + '/' + pkgMainFile, ext)) {
return filepath;
}
}
ext_index++;
if (ext_index < exts.length) {
return iotjs_module_t._resolveFilepath(id, root, ext_index);
}
};


Expand Down Expand Up @@ -278,34 +271,6 @@ iotjs_module_t.load = function(id, parent, isMain) {
return module.exports;
};

function _makeRequireFunction(mod) {
var Module = mod.constructor;
function require(id) {
return mod.require(id);
}

function _resolve(request) {
if (!request || typeof request !== 'string') {
throw new TypeError('module must be a non-null string');
}

if (process.builtin_modules[request]) {
return request;
}

var path = Module.resolveModPath(request, mod);
if (!path) {
throw new Error('Module not found: ' + request);
}
return path;
}
require.resolve = _resolve;
require.main = mainModule;
require.cache = Module.cache;

return require;
}

iotjs_module_t.prototype.compile = function(snapshot) {
var __filename = this.filename;
var __dirname = path.dirname(__filename);
Expand All @@ -318,7 +283,9 @@ iotjs_module_t.prototype.compile = function(snapshot) {
throw new TypeError('Invalid snapshot file.');
}

var _require = _makeRequireFunction(this);
var _require = this.require.bind(this);
_require.main = mainModule;
_require.cache = iotjs_module_t.cache;

fn.apply(this.exports, [
this.exports, // exports
Expand Down
16 changes: 0 additions & 16 deletions test/common/fixtures.js

This file was deleted.

17 changes: 0 additions & 17 deletions test/run_pass/test_require_resolve.js

This file was deleted.

2 changes: 0 additions & 2 deletions test/testsets.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,6 @@
{ "name": "test_process_unhandled_rejection.js" },
{ "name": "test_pwm_async.js", "skip": ["all"], "reason": "need to setup test environment" },
{ "name": "test_pwm_sync.js", "skip": ["all"], "reason": "need to setup test environment" },
{ "name": "test_require_main.js" },
{ "name": "test_require_resolve.js" },
{ "name": "test_shebang.js" },
{ "name": "test_signal_simple.js" },
{ "name": "test_spi.js", "skip": ["linux"], "reason": "Differend env on Linux desktop/travis/rpi" },
Expand Down

0 comments on commit 25e9fee

Please sign in to comment.