GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Merge pull request #8452 from webpack/feature/resolveWithOptions · webpack/webpack@a67ffcd

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib
    • NormalModule.js
    • ResolverFactory.js
  • test/cases/loaders/resolve
    • index.js
    • index.xyz
    • loader.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/NormalModule.js‎

Copy file name to clipboardExpand all lines: lib/NormalModule.js
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,21 @@ class NormalModule extends Module {
184184
resolve(context, request, callback) {
185185
resolver.resolve({}, context, request, {}, callback);
186186
},
187+
getResolve(options) {
188+
const child = options ? resolver.withOptions(options) : resolver;
189+
return (context, request, callback) => {
190+
if (callback) {
191+
child.resolve({}, context, request, {}, callback);
192+
} else {
193+
return new Promise((resolve, reject) => {
194+
child.resolve({}, context, request, {}, (err, result) => {
195+
if (err) reject(err);
196+
else resolve(result);
197+
});
198+
});
199+
}
200+
};
201+
},
187202
emitFile: (name, content, sourceMap) => {
188203
if (!this.buildInfo.assets) {
189204
this.buildInfo.assets = Object.create(null);
Collapse file

‎lib/ResolverFactory.js‎

Copy file name to clipboardExpand all lines: lib/ResolverFactory.js
+13Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
const { Tapable, HookMap, SyncHook, SyncWaterfallHook } = require("tapable");
88
const Factory = require("enhanced-resolve").ResolverFactory;
99

10+
/** @typedef {import("enhanced-resolve").Resolver} Resolver */
11+
1012
module.exports = class ResolverFactory extends Tapable {
1113
constructor() {
1214
super();
@@ -53,11 +55,22 @@ module.exports = class ResolverFactory extends Tapable {
5355
}
5456

5557
_create(type, resolveOptions) {
58+
const originalResolveOptions = Object.assign({}, resolveOptions);
5659
resolveOptions = this.hooks.resolveOptions.for(type).call(resolveOptions);
5760
const resolver = Factory.createResolver(resolveOptions);
5861
if (!resolver) {
5962
throw new Error("No resolver created");
6063
}
64+
/** @type {Map<Object, Resolver>} */
65+
const childCache = new Map();
66+
resolver.withOptions = options => {
67+
const cacheEntry = childCache.get(options);
68+
if (cacheEntry !== undefined) return cacheEntry;
69+
const mergedOptions = Object.assign({}, originalResolveOptions, options);
70+
const resolver = this.get(type, mergedOptions);
71+
childCache.set(options, resolver);
72+
return resolver;
73+
};
6174
this.hooks.resolver.for(type).call(resolver, resolveOptions);
6275
return resolver;
6376
}
Collapse file

‎test/cases/loaders/resolve/index.js‎

Copy file name to clipboard
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
it("should be possible to create resolver with different options", () => {
2+
const result = require("./loader!");
3+
expect(result).toEqual({
4+
one: "index.js",
5+
two: "index.xyz"
6+
});
7+
})
Collapse file

‎test/cases/loaders/resolve/index.xyz‎

Copy file name to clipboardExpand all lines: test/cases/loaders/resolve/index.xyz
Whitespace-only changes.
Collapse file

‎test/cases/loaders/resolve/loader.js‎

Copy file name to clipboard
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const path = require("path");
2+
module.exports = function() {
3+
const resolve1 = this.getResolve();
4+
const resolve2 = this.getResolve({
5+
extensions: [".xyz", ".js"]
6+
});
7+
return Promise.all([
8+
resolve1(__dirname, "./index"),
9+
resolve2(__dirname, "./index")
10+
]).then(([one, two]) => {
11+
return `module.exports = ${JSON.stringify({
12+
one: path.basename(one),
13+
two: path.basename(two),
14+
})}`;
15+
});
16+
};

0 commit comments

Comments
0 (0)
You are visitor 01402605 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.