Skip to content

Commit

Permalink
Merge pull request #1 from vseventer/resolve-alias
Browse files Browse the repository at this point in the history
Support aliases peerigon#81
  • Loading branch information
boroth authored May 20, 2021
2 parents 8500840 + 6be80bc commit f3f4c15
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
12 changes: 10 additions & 2 deletions src/extractLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,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);
Expand All @@ -135,7 +135,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);
Expand Down Expand Up @@ -222,6 +222,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;
2 changes: 1 addition & 1 deletion test/extractLoader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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", () =>
Expand Down

0 comments on commit f3f4c15

Please sign in to comment.