Skip to content

Commit

Permalink
add getResolve method to loader context
Browse files Browse the repository at this point in the history
with allows to pass options
  • Loading branch information
sokra committed Dec 3, 2018
1 parent 72a8a1f commit 162da1c
Show file tree
Hide file tree
Showing 5 changed files with 46 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.
11 changes: 11 additions & 0 deletions test/cases/loaders/resolve/loader.js
@@ -0,0 +1,11 @@
const path = require("path");
module.exports = async function() {
const resolve1 = this.getResolve();
const resolve2 = this.getResolve({
extensions: [".xyz", ".js"]
});
return `module.exports = ${JSON.stringify({
one: path.basename(await resolve1(__dirname, "./index")),
two: path.basename(await resolve2(__dirname, "./index")),
})}`;
};

0 comments on commit 162da1c

Please sign in to comment.