|
| 1 | +import { setupTs, readConfigFile, LoaderConfig } from './instance'; |
| 2 | +import * as path from 'path'; |
| 3 | + |
| 4 | +const ModulesInRootPlugin: new (a: string, b: string, c: string) => ResolverPlugin |
| 5 | + = require('enhanced-resolve/lib/ModulesInRootPlugin'); |
| 6 | + |
| 7 | +const createInnerCallback: CreateInnerCallback = require('enhanced-resolve/lib/createInnerCallback'); |
| 8 | +const getInnerRequest: getInnerRequest = require('enhanced-resolve/lib/getInnerRequest'); |
| 9 | + |
| 10 | +type CreateInnerCallback = (callback: Callback, options: Callback, message?: string, messageOptional?: string) => Callback; |
| 11 | +type getInnerRequest = (resolver: Resolver, request: Request) => string; |
| 12 | + |
| 13 | +export interface Request { |
| 14 | + request?: Request; |
| 15 | + relativePath: string; |
| 16 | +} |
| 17 | + |
| 18 | +export interface Callback { |
| 19 | + (err?: Error, result?: any): void; |
| 20 | + |
| 21 | + log?: any; |
| 22 | + stack?: any; |
| 23 | + missing?: any; |
| 24 | +} |
| 25 | + |
| 26 | +type ResolverCallback = (request: Request, callback: Callback) => void; |
| 27 | + |
| 28 | +interface ResolverPlugin { |
| 29 | + apply(resolver: Resolver): void; |
| 30 | +} |
| 31 | + |
| 32 | +export interface Resolver { |
| 33 | + apply(plugin: ResolverPlugin): void; |
| 34 | + plugin(source: string, cb: ResolverCallback); |
| 35 | + doResolve(target: string, req: Request, desc: string, Callback); |
| 36 | + join(relativePath: string, innerRequest: Request): Request; |
| 37 | +} |
| 38 | + |
| 39 | +interface Mapping { |
| 40 | + onlyModule: boolean; |
| 41 | + alias: string; |
| 42 | + aliasPattern: RegExp; |
| 43 | + target: string; |
| 44 | +} |
| 45 | + |
| 46 | +function escapeRegExp(str) { |
| 47 | + return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"); |
| 48 | +} |
| 49 | + |
| 50 | +export class PathsPlugin implements ResolverPlugin { |
| 51 | + source: string; |
| 52 | + target: string; |
| 53 | + ts: typeof ts; |
| 54 | + configFilePath: string; |
| 55 | + options: ts.CompilerOptions; |
| 56 | + |
| 57 | + baseUrl: string; |
| 58 | + mappings: Mapping[]; |
| 59 | + absoluteBaseUrl: string; |
| 60 | + |
| 61 | + constructor(config: LoaderConfig & ts.CompilerOptions = {} as any) { |
| 62 | + this.source = 'described-resolve'; |
| 63 | + this.target = 'resolve'; |
| 64 | + |
| 65 | + this.ts = setupTs(config.compiler).tsImpl; |
| 66 | + |
| 67 | + let { configFilePath, compilerConfig } = readConfigFile(process.cwd(), config, this.ts); |
| 68 | + this.options = compilerConfig.options; |
| 69 | + this.configFilePath = configFilePath; |
| 70 | + |
| 71 | + this.baseUrl = this.options.baseUrl; |
| 72 | + this.absoluteBaseUrl = path.resolve( |
| 73 | + path.dirname(this.configFilePath), |
| 74 | + this.baseUrl |
| 75 | + ); |
| 76 | + |
| 77 | + this.mappings = []; |
| 78 | + let paths = this.options.paths || {}; |
| 79 | + Object.keys(paths).forEach(alias => { |
| 80 | + let onlyModule = alias.indexOf('*') === -1; |
| 81 | + let excapedAlias = escapeRegExp(alias); |
| 82 | + let targets = paths[alias]; |
| 83 | + targets.forEach(target => { |
| 84 | + let aliasPattern: RegExp; |
| 85 | + if (onlyModule) { |
| 86 | + aliasPattern = new RegExp(`^${excapedAlias}$`); |
| 87 | + } else { |
| 88 | + let withStarCapturing = excapedAlias.replace('\\*', '(.*)'); |
| 89 | + aliasPattern = new RegExp(`^${withStarCapturing}`); |
| 90 | + } |
| 91 | + |
| 92 | + this.mappings.push({ |
| 93 | + onlyModule, |
| 94 | + alias, |
| 95 | + aliasPattern, |
| 96 | + target: target |
| 97 | + }); |
| 98 | + }); |
| 99 | + }); |
| 100 | + } |
| 101 | + |
| 102 | + apply(resolver: Resolver) { |
| 103 | + let { baseUrl, mappings } = this; |
| 104 | + |
| 105 | + if (baseUrl) { |
| 106 | + resolver.apply(new ModulesInRootPlugin("module", this.absoluteBaseUrl, "resolve")); |
| 107 | + } |
| 108 | + |
| 109 | + mappings.forEach(mapping => { |
| 110 | + resolver.plugin(this.source, this.createPlugin(resolver, mapping)); |
| 111 | + }); |
| 112 | + } |
| 113 | + |
| 114 | + createPlugin(resolver: Resolver, mapping: Mapping) { |
| 115 | + return (request, callback) => { |
| 116 | + let innerRequest = getInnerRequest(resolver, request); |
| 117 | + if (!innerRequest) { |
| 118 | + return callback(); |
| 119 | + } |
| 120 | + |
| 121 | + let match = innerRequest.match(mapping.aliasPattern); |
| 122 | + if (!match) { |
| 123 | + return callback(); |
| 124 | + } |
| 125 | + |
| 126 | + let newRequestStr = mapping.target; |
| 127 | + if (!mapping.onlyModule) { |
| 128 | + newRequestStr = newRequestStr.replace('*', match[1]); |
| 129 | + } |
| 130 | + |
| 131 | + if (newRequestStr[0] === '.') { |
| 132 | + newRequestStr = path.resolve(this.absoluteBaseUrl, newRequestStr); |
| 133 | + } |
| 134 | + |
| 135 | + let newRequest: Request = Object.assign({}, request, { |
| 136 | + request: newRequestStr |
| 137 | + }); |
| 138 | + |
| 139 | + return resolver.doResolve( |
| 140 | + this.target, |
| 141 | + newRequest, |
| 142 | + "aliased with mapping '" + innerRequest + "': '" + mapping.alias + "' to '" + newRequestStr + "'", |
| 143 | + createInnerCallback( |
| 144 | + function(err, result) { |
| 145 | + console.log(err, result, arguments.length > 0); |
| 146 | + if (arguments.length > 0) { |
| 147 | + return callback(err, result); |
| 148 | + } |
| 149 | + |
| 150 | + // don't allow other aliasing or raw request |
| 151 | + callback(null, null); |
| 152 | + }, |
| 153 | + callback |
| 154 | + ) |
| 155 | + ); |
| 156 | + }; |
| 157 | + } |
| 158 | +} |
0 commit comments