Projekt

Obecné

Profil

Stáhnout (1.6 KB) Statistiky
| Větev: | Revize:
1
/*
2
	MIT License http://www.opensource.org/licenses/mit-license.php
3
	Author Tobias Koppers @sokra
4
*/
5
"use strict";
6

    
7
const path = require("path");
8

    
9
module.exports = class MainFieldPlugin {
10
	constructor(source, options, target) {
11
		this.source = source;
12
		this.options = options;
13
		this.target = target;
14
	}
15

    
16
	apply(resolver) {
17
		const target = resolver.ensureHook(this.target);
18
		resolver.getHook(this.source).tapAsync("MainFieldPlugin", (request, resolveContext, callback) => {
19
			if(request.path !== request.descriptionFileRoot) return callback();
20
			if(request.alreadyTriedMainField === request.descriptionFilePath) return callback();
21
			const content = request.descriptionFileData;
22
			const filename = path.basename(request.descriptionFilePath);
23
			let mainModule;
24
			const field = this.options.name;
25
			if(Array.isArray(field)) {
26
				let current = content;
27
				for(let j = 0; j < field.length; j++) {
28
					if(current === null || typeof current !== "object") {
29
						current = null;
30
						break;
31
					}
32
					current = current[field[j]];
33
				}
34
				if(typeof current === "string") {
35
					mainModule = current;
36
				}
37
			} else {
38
				if(typeof content[field] === "string") {
39
					mainModule = content[field];
40
				}
41
			}
42
			if(!mainModule) return callback();
43
			if(this.options.forceRelative && !/^\.\.?\//.test(mainModule))
44
				mainModule = "./" + mainModule;
45
			const obj = Object.assign({}, request, {
46
				request: mainModule,
47
				alreadyTriedMainField: request.descriptionFilePath
48
			});
49
			return resolver.doResolve(target, obj, "use " + mainModule + " from " + this.options.name + " in " + filename, resolveContext, callback);
50
		});
51
	}
52
};
(16-16/39)