Skip to content

Commit 2527559

Browse files
committed
Updated the Module resolution strategy. It looks like awesome-ts-loader doesn't like it, however when I implement the plugin it still works correctly. Need to tell a-t-l to stop trying to resolve itself.
1 parent 4e24aad commit 2527559

File tree

1 file changed

+36
-53
lines changed

1 file changed

+36
-53
lines changed

addon/ng2/utilities/ts-path-mappings-webpack-plugin.ts

+36-53
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export interface Mapping {
8383
export function readConfigFile(baseDir: string, query: QueryOptions, tsImpl: typeof ts): Configs {
8484
let configFilePath: string;
8585
if (query.tsconfig && query.tsconfig.match(/\.json$/)) {
86-
configFilePath = query.tsconfig;
86+
configFilePath = path.dirname(path.resolve(process.cwd(),query.tsconfig));
8787
} else {
8888
configFilePath = tsImpl.findConfigFile(process.cwd(), tsImpl.sys.fileExists);
8989
}
@@ -104,6 +104,8 @@ export function readConfigFile(baseDir: string, query: QueryOptions, tsImpl: typ
104104
};
105105
}
106106

107+
debugger;
108+
107109
let jsonConfigFile = tsImpl.readConfigFile(configFilePath, tsImpl.sys.readFile);
108110

109111
let compilerConfig = tsImpl.parseJsonConfigFileContent(
@@ -115,6 +117,7 @@ export function readConfigFile(baseDir: string, query: QueryOptions, tsImpl: typ
115117
);
116118

117119
return {
120+
jsonConfigFile,
118121
configFilePath,
119122
compilerConfig,
120123
loaderConfig: _.defaults<LoaderConfig, LoaderConfig>(
@@ -169,18 +172,17 @@ export class PathsPlugin implements ResolverPlugin {
169172

170173
this.ts = setupTs(config.compiler).tsImpl;
171174

172-
let { configFilePath, compilerConfig } = readConfigFile(process.cwd(), config, this.ts);
175+
let { configFilePath, compilerConfig, jsonConfigFile } = readConfigFile(process.cwd(), config, this.ts);
173176
this.options = compilerConfig.options;
174177
this.configFilePath = configFilePath;
175178

176-
this.baseUrl = this.options.baseUrl;
179+
this.baseUrl = this.options.configFilePath ? this.options.configFilePath : './';
180+
177181
this.absoluteBaseUrl = path.resolve(
178182
path.dirname(this.configFilePath),
179183
this.baseUrl
180184
);
181185

182-
debugger;
183-
184186
console.log("CONFIG FILE AND BASE URL");
185187
console.log(this.configFilePath, this.absoluteBaseUrl);
186188

@@ -210,63 +212,44 @@ export class PathsPlugin implements ResolverPlugin {
210212
}
211213

212214
apply(resolver: Resolver) {
213-
let { baseUrl, mappings } = this;
215+
let { baseUrl, mappings, absoluteBaseUrl } = this;
214216

215217
if (baseUrl) {
216-
resolver.apply(new ModulesInRootPlugin("module", this.absoluteBaseUrl, "resolve"));
218+
resolver.apply(new ModulesInRootPlugin("module", absoluteBaseUrl, "resolve"));
217219
}
218220

219221
mappings.forEach(mapping => {
220-
resolver.plugin(this.source, this.createPlugin(resolver, mapping));
221-
});
222-
}
223-
224-
createPlugin(resolver: Resolver, mapping: Mapping) {
225-
return (request, callback) => {
226-
let innerRequest = getInnerRequest(resolver, request);
227-
if (!innerRequest) {
228-
return callback();
229-
}
222+
// resolver.plugin(this.source, this.createPlugin(resolver, mapping));
223+
resolver.plugin(this.source, function(request, callback) {
224+
var innerRequest = getInnerRequest(resolver, request);
225+
if(!innerRequest) return callback();
226+
227+
var newRequestStr = mapping.target;
228+
var match = innerRequest.match(mapping.aliasPattern);
229+
if (!match) {
230+
return callback();
231+
}
232+
if (!mapping.onlyModule) {
233+
newRequestStr = newRequestStr.replace('*', match[1]);
234+
}
235+
if (newRequestStr[0] === '.') {
236+
newRequestStr = path.resolve(absoluteBaseUrl, newRequestStr);
237+
}
238+
var obj: Request = Object.assign({}, request, {
239+
request: newRequestStr
240+
});
230241

231-
let match = innerRequest.match(mapping.aliasPattern);
232-
if (!match) {
233-
return callback();
234-
}
242+
console.log("aliased'" + innerRequest + "': '" + mapping.alias + "' to '" + newRequestStr + "'", newRequest);
235243

236-
let newRequestStr = mapping.target;
237-
if (!mapping.onlyModule) {
238-
newRequestStr = newRequestStr.replace('*', match[1]);
239-
}
244+
return resolver.doResolve(this.target, obj,"aliased with mapping '" + innerRequest + "': '" + mapping.alias + "' to '" + newRequestStr + "'", createInnerCallback(function(err, result) {
245+
if(arguments.length > 0) return callback(err, result);
240246

241-
if (newRequestStr[0] === '.') {
242-
newRequestStr = path.resolve(this.absoluteBaseUrl, newRequestStr);
243-
}
247+
// don't allow other aliasing or raw request
248+
callback(null, null);
249+
}, callback));
244250

245-
let newRequest: Request = Object.assign({}, request, {
246-
request: newRequestStr
251+
return callback();
247252
});
248-
249-
console.log("aliased'" + innerRequest + "': '" + mapping.alias + "' to '" + newRequestStr + "'", newRequest);
250-
251-
let doResolve = resolver.doResolve(
252-
this.target,
253-
newRequest,
254-
"aliased with mapping '" + innerRequest + "': '" + mapping.alias + "' to '" + newRequestStr + "'",
255-
createInnerCallback(
256-
function(err, result) {
257-
console.log(err, result, arguments.length > 0);
258-
if (arguments.length > 0) {
259-
return callback(err, result);
260-
}
261-
262-
// don't allow other aliasing or raw request
263-
callback(null, null);
264-
},
265-
callback
266-
)
267-
);
268-
269-
return doResolve;
270-
};
253+
});
271254
}
272255
}

0 commit comments

Comments
 (0)