Skip to content

Commit

Permalink
Expose require.resolve.paths()
Browse files Browse the repository at this point in the history
  • Loading branch information
zertosh committed May 10, 2019
1 parent 2a5e61a commit 90fac12
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 2 deletions.
9 changes: 9 additions & 0 deletions test/fixtures/require-resolve-paths.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
console.log(
'%j',
{
hasRequireResolve: typeof require.resolve.paths === 'function',
value: typeof require.resolve.paths === 'function'
? require.resolve.paths(process.argv[2])
: undefined
}
);
68 changes: 68 additions & 0 deletions test/module-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
'use strict';

const child_process = require('child_process');
const tap = require('tap');
const semver = require('semver');

tap.beforeEach(cb => {
delete process.env.DISABLE_V8_COMPILE_CACHE;
cb();
});

tap.test('require.resolve.paths module', t => {
const psWithoutCache = child_process.spawnSync(
process.execPath,
[require.resolve('./fixtures/require-resolve-paths'), 'tap'],
{cwd: __dirname}
);

const psWithCache = child_process.spawnSync(
process.execPath,
[
'--require',
'..',
require.resolve('./fixtures/require-resolve-paths'),
'tap',
],
{cwd: __dirname}
);

const actual = JSON.parse(psWithCache.stdout);
const expected = JSON.parse(psWithoutCache.stdout);

t.same(actual, expected);
t.equal(psWithCache.stderr.toString(), '');
t.equal(psWithCache.status, 0);

t.equal(
actual.hasRequireResolve,
semver.satisfies(process.versions.node, '>=8.9.0')
);

t.end();
});

tap.test('require.resolve.paths relative', t => {
const psWithoutCache = child_process.spawnSync(
process.execPath,
[require.resolve('./fixtures/require-resolve-paths'), './foo'],
{cwd: __dirname}
);

const psWithCache = child_process.spawnSync(
process.execPath,
[
'--require',
'..',
require.resolve('./fixtures/require-resolve-paths'),
'./foo',
],
{cwd: __dirname}
);

t.same(JSON.parse(psWithCache.stdout), JSON.parse(psWithoutCache.stdout));
t.equal(psWithCache.stderr.toString(), '');
t.equal(psWithCache.status, 0);

t.end();
});
18 changes: 16 additions & 2 deletions v8-compile-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,29 @@ class NativeCompileCache {

install() {
const self = this;
const hasRequireResolvePaths = typeof require.resolve.paths === 'function';
this._previousModuleCompile = Module.prototype._compile;
Module.prototype._compile = function(content, filename) {
const mod = this;

function require(id) {
return mod.require(id);
}
require.resolve = function(request, options) {

// https://github.com/nodejs/node/blob/v10.15.3/lib/internal/modules/cjs/helpers.js#L28
function resolve(request, options) {
return Module._resolveFilename(request, mod, false, options);
};
}
require.resolve = resolve;

// https://github.com/nodejs/node/blob/v10.15.3/lib/internal/modules/cjs/helpers.js#L37
// resolve.resolve.paths was added in v8.9.0
if (hasRequireResolvePaths) {
resolve.paths = function paths(request) {
return Module._resolveLookupPaths(request, mod, true);
};
}

require.main = process.mainModule;

// Enable support to add extra extension types
Expand Down

0 comments on commit 90fac12

Please sign in to comment.