Skip to content

Commit

Permalink
Merge pull request #8452 from webpack/feature/resolveWithOptions
Browse files Browse the repository at this point in the history
add getResolve method to loader context
  • Loading branch information
sokra committed Dec 4, 2018
2 parents 96f625c + 56feccc commit a67ffcd
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/NormalModule.js
Expand Up @@ -184,6 +184,21 @@ class NormalModule extends Module {
resolve(context, request, callback) {
resolver.resolve({}, context, request, {}, callback);
},
getResolve(options) {
const child = options ? resolver.withOptions(options) : resolver;
return (context, request, callback) => {
if (callback) {
child.resolve({}, context, request, {}, callback);
} else {
return new Promise((resolve, reject) => {
child.resolve({}, context, request, {}, (err, result) => {
if (err) reject(err);
else resolve(result);
});
});
}
};
},
emitFile: (name, content, sourceMap) => {
if (!this.buildInfo.assets) {
this.buildInfo.assets = Object.create(null);
Expand Down
13 changes: 13 additions & 0 deletions lib/ResolverFactory.js
Expand Up @@ -7,6 +7,8 @@
const { Tapable, HookMap, SyncHook, SyncWaterfallHook } = require("tapable");
const Factory = require("enhanced-resolve").ResolverFactory;

/** @typedef {import("enhanced-resolve").Resolver} Resolver */

module.exports = class ResolverFactory extends Tapable {
constructor() {
super();
Expand Down Expand Up @@ -53,11 +55,22 @@ module.exports = class ResolverFactory extends Tapable {
}

_create(type, resolveOptions) {
const originalResolveOptions = Object.assign({}, resolveOptions);
resolveOptions = this.hooks.resolveOptions.for(type).call(resolveOptions);
const resolver = Factory.createResolver(resolveOptions);
if (!resolver) {
throw new Error("No resolver created");
}
/** @type {Map<Object, Resolver>} */
const childCache = new Map();
resolver.withOptions = options => {
const cacheEntry = childCache.get(options);
if (cacheEntry !== undefined) return cacheEntry;
const mergedOptions = Object.assign({}, originalResolveOptions, options);
const resolver = this.get(type, mergedOptions);
childCache.set(options, resolver);
return resolver;
};
this.hooks.resolver.for(type).call(resolver, resolveOptions);
return resolver;
}
Expand Down
7 changes: 7 additions & 0 deletions test/cases/loaders/resolve/index.js
@@ -0,0 +1,7 @@
it("should be possible to create resolver with different options", () => {
const result = require("./loader!");
expect(result).toEqual({
one: "index.js",
two: "index.xyz"
});
})
Empty file.
16 changes: 16 additions & 0 deletions test/cases/loaders/resolve/loader.js
@@ -0,0 +1,16 @@
const path = require("path");
module.exports = function() {
const resolve1 = this.getResolve();
const resolve2 = this.getResolve({
extensions: [".xyz", ".js"]
});
return Promise.all([
resolve1(__dirname, "./index"),
resolve2(__dirname, "./index")
]).then(([one, two]) => {
return `module.exports = ${JSON.stringify({
one: path.basename(one),
two: path.basename(two),
})}`;
});
};

0 comments on commit a67ffcd

Please sign in to comment.