diff --git a/resolvers/webpack/index.js b/resolvers/webpack/index.js index 146213a0c..0f75a2840 100644 --- a/resolvers/webpack/index.js +++ b/resolvers/webpack/index.js @@ -38,10 +38,6 @@ exports.resolve = function (source, file, settings) { source = source.slice(0, finalQuestionMark) } - if (source in coreLibs) { - return { found: true, path: coreLibs[source] } - } - var webpackConfig var configPath = get(settings, 'config') @@ -73,7 +69,7 @@ exports.resolve = function (source, file, settings) { throw e } } else { - log("No config path found relative to", file, "; using {}") + log('No config path found relative to', file, '; using {}') webpackConfig = {} } @@ -123,6 +119,10 @@ exports.resolve = function (source, file, settings) { try { return { found: true, path: resolveSync(path.dirname(file), source) } } catch (err) { + if (source in coreLibs) { + return { found: true, path: coreLibs[source] } + } + log('Error during module resolution:', err) return { found: false } } @@ -136,7 +136,7 @@ function getResolveSync(configPath, webpackConfig) { if (!cached) { cached = { key: cacheKey, - value: createResolveSync(configPath, webpackConfig) + value: createResolveSync(configPath, webpackConfig), } // put in front and pop last item if (_cache.unshift(cached) > MAX_CACHE) { diff --git a/src/core/importType.js b/src/core/importType.js index 5725e9ec6..7755bb4a2 100644 --- a/src/core/importType.js +++ b/src/core/importType.js @@ -21,7 +21,9 @@ export function isAbsolute(name) { return name.indexOf('/') === 0 } -export function isBuiltIn(name, settings) { +// path is defined only when a resolver resolves to a non-standard path +export function isBuiltIn(name, settings, path) { + if (path) return false const base = baseModule(name) const extras = (settings && settings['import/core-modules']) || [] return coreModules[base] || extras.indexOf(base) > -1 diff --git a/tests/files/constants/index.js b/tests/files/constants/index.js new file mode 100644 index 000000000..2d7500a68 --- /dev/null +++ b/tests/files/constants/index.js @@ -0,0 +1 @@ +export const FOO = 'FOO' \ No newline at end of file diff --git a/tests/src/core/importType.js b/tests/src/core/importType.js index 54a5adc3a..f60063991 100644 --- a/tests/src/core/importType.js +++ b/tests/src/core/importType.js @@ -7,6 +7,7 @@ import { testContext } from '../utils' describe('importType(name)', function () { const context = testContext() + const pathToTestFiles = path.join(__dirname, '..', '..', 'files') it("should return 'absolute' for paths starting with a /", function() { expect(importType('/', context)).to.equal('absolute') @@ -42,20 +43,36 @@ describe('importType(name)', function () { }) it("should return 'internal' for non-builtins resolved outside of node_modules", function () { - const pathContext = testContext({ "import/resolver": { node: { paths: [ path.join(__dirname, '..', '..', 'files') ] } } }) + const pathContext = testContext({ "import/resolver": { node: { paths: [pathToTestFiles] } } }) expect(importType('importType', pathContext)).to.equal('internal') }) it.skip("should return 'internal' for scoped packages resolved outside of node_modules", function () { - const pathContext = testContext({ "import/resolver": { node: { paths: [ path.join(__dirname, '..', '..', 'files') ] } } }) + const pathContext = testContext({ "import/resolver": { node: { paths: [pathToTestFiles] } } }) expect(importType('@importType/index', pathContext)).to.equal('internal') }) it("should return 'internal' for internal modules that are referenced by aliases", function () { - const pathContext = testContext({ 'import/resolver': { node: { paths: [path.join(__dirname, '..', '..', 'files')] } } }) + const pathContext = testContext({ 'import/resolver': { node: { paths: [pathToTestFiles] } } }) expect(importType('@my-alias/fn', pathContext)).to.equal('internal') }) + it("should return 'internal' for aliased internal modules that look like core modules (node resolver)", function () { + const pathContext = testContext({ 'import/resolver': { node: { paths: [pathToTestFiles] } } }) + expect(importType('constants/index', pathContext)).to.equal('internal') + expect(importType('constants/', pathContext)).to.equal('internal') + // resolves exact core modules over internal modules + expect(importType('constants', pathContext)).to.equal('builtin') + }) + + it("should return 'internal' for aliased internal modules that look like core modules (webpack resolver)", function () { + const webpackConfig = { resolve: { modules: [pathToTestFiles, 'node_modules'] } } + const pathContext = testContext({ 'import/resolver': { webpack: { config: webpackConfig } } }) + expect(importType('constants/index', pathContext)).to.equal('internal') + expect(importType('constants/', pathContext)).to.equal('internal') + expect(importType('constants', pathContext)).to.equal('internal') + }) + it("should return 'parent' for internal modules that go through the parent", function() { expect(importType('../foo', context)).to.equal('parent') expect(importType('../../foo', context)).to.equal('parent')