GeezerVison is showing github.com — Merge pull request #8452 from webpack/feature/resolveWithOptions · webpack/webpack@a67ffcd
File treeExpand file treeCollapse file tree Open diff view settings test/cases/loaders/resolve
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/NormalModule.js +15Lines changed: 15 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -184,6 +184,21 @@ class NormalModule extends Module { | 184 | 184 | resolve(context, request, callback) { | 185 | 185 | resolver.resolve({}, context, request, {}, callback); | 186 | 186 | }, | | 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 | + }, | 187 | 202 | emitFile: (name, content, sourceMap) => { | 188 | 203 | if (!this.buildInfo.assets) { | 189 | 204 | this.buildInfo.assets = Object.create(null); | |
Collapse file Copy file name to clipboardExpand all lines: lib/ResolverFactory.js +13Lines changed: 13 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 7 | 7 | const { Tapable, HookMap, SyncHook, SyncWaterfallHook } = require("tapable"); | 8 | 8 | const Factory = require("enhanced-resolve").ResolverFactory; | 9 | 9 |
| | 10 | +/** @typedef {import("enhanced-resolve").Resolver} Resolver */ | | 11 | + | 10 | 12 | module.exports = class ResolverFactory extends Tapable { | 11 | 13 | constructor() { | 12 | 14 | super(); | @@ -53,11 +55,22 @@ module.exports = class ResolverFactory extends Tapable { | 53 | 55 | } | 54 | 56 |
| 55 | 57 | _create(type, resolveOptions) { | | 58 | + const originalResolveOptions = Object.assign({}, resolveOptions); | 56 | 59 | resolveOptions = this.hooks.resolveOptions.for(type).call(resolveOptions); | 57 | 60 | const resolver = Factory.createResolver(resolveOptions); | 58 | 61 | if (!resolver) { | 59 | 62 | throw new Error("No resolver created"); | 60 | 63 | } | | 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 | + }; | 61 | 74 | this.hooks.resolver.for(type).call(resolver, resolveOptions); | 62 | 75 | return resolver; | 63 | 76 | } | |
Collapse file Copy file name to clipboard +7Lines changed: 7 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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 Copy file name to clipboardExpand all lines: test/cases/loaders/resolve/index.xyz Collapse file Copy file name to clipboard +16Lines changed: 16 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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 | +}; |
|