From 6be80bcd3ec53d218392a654f7a2d9edff5d42f2 Mon Sep 17 00:00:00 2001 From: vseventer Date: Wed, 22 Apr 2020 20:57:28 -0400 Subject: [PATCH] Support aliases #81 --- src/extractLoader.js | 12 ++++++++++-- test/extractLoader.test.js | 2 +- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/extractLoader.js b/src/extractLoader.js index 1ceea3f..a0b9d8a 100644 --- a/src/extractLoader.js +++ b/src/extractLoader.js @@ -127,7 +127,7 @@ function evalDependencyGraph({loaderContext, src, filename, publicPath = ""}) { const indexOfLastExclMark = relativePathWithoutQuery.lastIndexOf("!"); const loaders = givenRelativePath.slice(0, indexOfLastExclMark + 1); const relativePath = relativePathWithoutQuery.slice(indexOfLastExclMark + 1); - const absolutePath = resolve.sync(relativePath, { + const absolutePath = tryResolve(relativePath, { basedir: path.dirname(filename), }); const ext = path.extname(absolutePath); @@ -138,7 +138,7 @@ function evalDependencyGraph({loaderContext, src, filename, publicPath = ""}) { // If the required file is a js file, we just require it with node's require. // If the required file should be processed by a loader we do not touch it (even if it is a .js file). - if (loaders === "" && ext === ".js") { + if (loaders === "" && absolutePath.indexOf("node_modules/") !== -1 && ext === ".js") { // Mark the file as dependency so webpack's watcher is working for the css-loader helper. // Other dependencies are automatically added by loadModule() below loaderContext.addDependency(absolutePath); @@ -221,6 +221,14 @@ function getPublicPath(options, context) { } /* eslint-enable complexity */ +function tryResolve(filename, options) { + try { + return resolve.sync(filename, options); + } catch (e) { // Return original. + return filename; + } +} + // For CommonJS interoperability module.exports = extractLoader; export default extractLoader; diff --git a/test/extractLoader.test.js b/test/extractLoader.test.js index 4bad1a9..2dd97a4 100644 --- a/test/extractLoader.test.js +++ b/test/extractLoader.test.js @@ -209,7 +209,7 @@ describe("extractLoader", () => { throw new Error("Did not throw expected error"); }, message => { - expect(message).to.match(/Error: Cannot find module '\.\/does-not-exist\.jpg'/); + expect(message).to.match(/Error: Can't resolve '\.\/does-not-exist\.jpg'/); } )); it("should report resolve loader errors", () =>