Skip to content

Commit

Permalink
feat: add context into resolver arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin committed Aug 11, 2022
1 parent e4c90e5 commit 674eb8a
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 48 deletions.
4 changes: 2 additions & 2 deletions src/ExportMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ ExportMap.parse = function (path, content, context) {
const namespaces = new Map();

function remotePath(value) {
return resolve.relative(value, path, context.settings);
return resolve.relative(value, path, context.settings, context);
}

function resolveImport(value) {
Expand Down Expand Up @@ -534,7 +534,7 @@ ExportMap.parse = function (path, content, context) {
if (tsConfigInfo.tsConfigPath !== undefined) {
// Projects not using TypeScript won't have `typescript` installed.
if (!ts) { ts = require('typescript'); }

const configFile = ts.readConfigFile(tsConfigInfo.tsConfigPath, ts.sys.readFile);
return ts.parseJsonConfigFileContent(
configFile.config,
Expand Down
26 changes: 14 additions & 12 deletions tests/files/foo-bar-resolver-no-version.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
var path = require('path')

exports.resolveImport = function (modulePath, sourceFile, config) {
var sourceFileName = path.basename(sourceFile)
if (sourceFileName === 'foo.js') {
return path.join(__dirname, 'bar.jsx')
}
if (sourceFileName === 'exception.js') {
throw new Error('foo-bar-resolver-v1 resolveImport test exception')
}
return undefined;
}
var assert = require('assert')
var path = require('path')

exports.resolveImport = function (modulePath, sourceFile, config, context) {
var sourceFileName = path.basename(sourceFile)
if (sourceFileName === 'foo.js') {
return path.join(__dirname, 'bar.jsx')
}
if (sourceFileName === 'exception.js') {
throw new Error('foo-bar-resolver-v1 resolveImport test exception')
}
assert.ok(context, 'the `context` must be presented')
return undefined;
}
30 changes: 16 additions & 14 deletions tests/files/foo-bar-resolver-v1.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
var path = require('path')

exports.resolveImport = function (modulePath, sourceFile, config) {
var sourceFileName = path.basename(sourceFile)
if (sourceFileName === 'foo.js') {
return path.join(__dirname, 'bar.jsx');
}
if (sourceFileName === 'exception.js') {
throw new Error('foo-bar-resolver-v1 resolveImport test exception');
}
return undefined;
};

exports.interfaceVersion = 1;
var assert = require('assert')
var path = require('path')

exports.resolveImport = function (modulePath, sourceFile, config, context) {
var sourceFileName = path.basename(sourceFile)
if (sourceFileName === 'foo.js') {
return path.join(__dirname, 'bar.jsx');
}
if (sourceFileName === 'exception.js') {
throw new Error('foo-bar-resolver-v1 resolveImport test exception');
}
assert.ok(context, 'the `context` must be presented')
return undefined;
};

exports.interfaceVersion = 1;
30 changes: 16 additions & 14 deletions tests/files/foo-bar-resolver-v2.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
var path = require('path')

exports.resolve = function (modulePath, sourceFile, config) {
var sourceFileName = path.basename(sourceFile)
if (sourceFileName === 'foo.js') {
return { found: true, path: path.join(__dirname, 'bar.jsx') }
}
if (sourceFileName === 'exception.js') {
throw new Error('foo-bar-resolver-v2 resolve test exception')
}
return { found: false };
};

exports.interfaceVersion = 2;
var assert = require('assert')
var path = require('path')

exports.resolve = function (modulePath, sourceFile, config, context) {
var sourceFileName = path.basename(sourceFile)
if (sourceFileName === 'foo.js') {
return { found: true, path: path.join(__dirname, 'bar.jsx') }
}
if (sourceFileName === 'exception.js') {
throw new Error('foo-bar-resolver-v2 resolve test exception')
}
assert.ok(context, 'the `context` must be presented')
return { found: false };
};

exports.interfaceVersion = 2;
12 changes: 6 additions & 6 deletions utils/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ exports.fileExistsWithCaseSync = function fileExistsWithCaseSync(filepath, cache
return result;
};

function relative(modulePath, sourceFile, settings) {
return fullResolve(modulePath, sourceFile, settings).path;
function relative(modulePath, sourceFile, settings, context) {
return fullResolve(modulePath, sourceFile, settings, context).path;
}

function fullResolve(modulePath, sourceFile, settings) {
function fullResolve(modulePath, sourceFile, settings, context) {
// check if this is a bonus core module
const coreSet = new Set(settings['import/core-modules']);
if (coreSet.has(modulePath)) return { found: true, path: null };
Expand All @@ -104,7 +104,7 @@ function fullResolve(modulePath, sourceFile, settings) {

function v1() {
try {
const resolved = resolver.resolveImport(modulePath, sourceFile, config);
const resolved = resolver.resolveImport(modulePath, sourceFile, config, context);
if (resolved === undefined) return { found: false };
return { found: true, path: resolved };
} catch (err) {
Expand All @@ -113,7 +113,7 @@ function fullResolve(modulePath, sourceFile, settings) {
}

function v2() {
return resolver.resolve(modulePath, sourceFile, config);
return resolver.resolve(modulePath, sourceFile, config, context);
}

switch (resolver.interfaceVersion) {
Expand Down Expand Up @@ -216,7 +216,7 @@ const erroredContexts = new Set();
*/
function resolve(p, context) {
try {
return relative(p, context.getPhysicalFilename ? context.getPhysicalFilename() : context.getFilename(), context.settings);
return relative(p, context.getPhysicalFilename ? context.getPhysicalFilename() : context.getFilename(), context.settings, context);
} catch (err) {
if (!erroredContexts.has(context)) {
// The `err.stack` string starts with `err.name` followed by colon and `err.message`.
Expand Down

0 comments on commit 674eb8a

Please sign in to comment.