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