Skip to content

Commit 6de3de9

Browse files
committed
build: add path mapping support to broccoli typescript
1 parent ba120c2 commit 6de3de9

File tree

2 files changed

+139
-13
lines changed

2 files changed

+139
-13
lines changed

lib/broccoli/broccoli-typescript.js

Lines changed: 107 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,14 @@ class BroccoliTypeScriptCompiler extends Plugin {
159159

160160
this._tsConfigFiles = tsconfig.files.splice(0);
161161

162-
this._tsOpts = ts.convertCompilerOptionsFromJson(tsconfig.compilerOptions, '', null).options;
162+
this._tsOpts = ts.convertCompilerOptionsFromJson(tsconfig['compilerOptions'],
163+
this.inputPaths[0], this._tsConfigPath).options;
163164
this._tsOpts.rootDir = '';
164165
this._tsOpts.outDir = '';
165166

166167
this._tsServiceHost = new CustomLanguageServiceHost(
167-
this._tsOpts, this._rootFilePaths, this._fileRegistry, this.inputPaths[0]);
168+
this._tsOpts, this._rootFilePaths, this._fileRegistry, this.inputPaths[0],
169+
tsconfig['compilerOptions'].paths, this._tsConfigPath);
168170
this._tsService = ts.createLanguageService(this._tsServiceHost, ts.createDocumentRegistry());
169171
}
170172

@@ -249,13 +251,15 @@ class BroccoliTypeScriptCompiler extends Plugin {
249251
}
250252

251253
class CustomLanguageServiceHost {
252-
constructor(compilerOptions, fileNames, fileRegistry, treeInputPath) {
254+
constructor(compilerOptions, fileNames, fileRegistry, treeInputPath, paths, tsConfigPath) {
253255
this.compilerOptions = compilerOptions;
254256
this.fileNames = fileNames;
255257
this.fileRegistry = fileRegistry;
256258
this.treeInputPath = treeInputPath;
257259
this.currentDirectory = treeInputPath;
258260
this.defaultLibFilePath = ts.getDefaultLibFilePath(compilerOptions).replace(/\\/g, '/');
261+
this.paths = paths;
262+
this.tsConfigPath = tsConfigPath;
259263
this.projectVersion = 0;
260264
}
261265

@@ -272,6 +276,74 @@ class CustomLanguageServiceHost {
272276
return this.projectVersion.toString();
273277
}
274278

279+
/**
280+
* Resolve a moduleName based on the path mapping defined in the tsconfig.
281+
* @param moduleName The module name to resolve.
282+
* @returns {?string} A string that is the path of the module, if found, or undefined.
283+
* @private
284+
*/
285+
_resolveModulePathWithMapping(moduleName) {
286+
// check if module name should be used as-is or it should be mapped to different value
287+
let longestMatchedPrefixLength = 0;
288+
let matchedPattern;
289+
let matchedWildcard;
290+
const paths = this.paths || {};
291+
292+
for (let pattern of Object.keys(paths)) {
293+
if (pattern.indexOf('*') != pattern.lastIndexOf('*')) {
294+
throw `Invalid path mapping pattern: "${pattern}"`;
295+
}
296+
297+
let indexOfWildcard = pattern.indexOf('*');
298+
if (indexOfWildcard !== -1) {
299+
// check if module name starts with prefix, ends with suffix and these two don't overlap
300+
let prefix = pattern.substr(0, indexOfWildcard);
301+
let suffix = pattern.substr(indexOfWildcard + 1);
302+
if (moduleName.length >= prefix.length + suffix.length &&
303+
moduleName.startsWith(prefix) &&
304+
moduleName.endsWith(suffix)) {
305+
306+
// use length of matched prefix as betterness criteria
307+
if (longestMatchedPrefixLength < prefix.length) {
308+
longestMatchedPrefixLength = prefix.length;
309+
matchedPattern = pattern;
310+
matchedWildcard = moduleName.substr(prefix.length, moduleName.length - suffix.length);
311+
}
312+
}
313+
} else {
314+
// Pattern does not contain asterisk - module name should exactly match pattern to succeed.
315+
if (pattern === moduleName) {
316+
matchedPattern = pattern;
317+
matchedWildcard = undefined;
318+
break;
319+
}
320+
}
321+
}
322+
323+
if (!matchedPattern) {
324+
// We fallback to the old module resolution.
325+
return undefined;
326+
}
327+
328+
// some pattern was matched - module name needs to be substituted
329+
let substitutions = this.paths[matchedPattern];
330+
for (let subst of substitutions) {
331+
if (subst.indexOf('*') != subst.lastIndexOf('*')) {
332+
throw `Invalid substitution: "${subst}" for pattern "${matchedPattern}".`;
333+
}
334+
// replace * in substitution with matched wildcard
335+
let p = matchedWildcard ? subst.replace('*', matchedWildcard) : subst;
336+
// if substituion is a relative path - combine it with baseUrl
337+
p = path.isAbsolute(p) ? p : path.join(this.treeInputPath, path.dirname(this.tsConfigPath), p);
338+
if (fs.existsSync(p)) {
339+
return p;
340+
}
341+
}
342+
343+
// This is an error; there was a match but no corresponding mapping was valid.
344+
throw `Path matched pattern "${matchedPattern}" but no valid substitution was found.`;
345+
}
346+
275347
/**
276348
* This method is called quite a bit to lookup 3 kinds of paths:
277349
* 1/ files in the fileRegistry
@@ -310,6 +382,38 @@ class CustomLanguageServiceHost {
310382
return ts.ScriptSnapshot.fromString(fs.readFileSync(absoluteTsFilePath, FS_OPTS));
311383
}
312384

385+
resolveModuleNames(moduleNames, containingFile)/*: ResolvedModule[]*/ {
386+
return moduleNames.map((moduleName) => {
387+
for (const ext of ['ts', 'd.ts']) {
388+
const name = `${moduleName}.${ext}`;
389+
const maybeModule = this._resolveModulePathWithMapping(name, containingFile);
390+
if (maybeModule) {
391+
return {
392+
resolvedFileName: maybeModule,
393+
isExternalLibraryImport: false
394+
};
395+
}
396+
}
397+
398+
return ts.resolveModuleName(moduleName, containingFile, this.compilerOptions, {
399+
fileExists(fileName) {
400+
return fs.existsSync(fileName);
401+
},
402+
readFile(fileName) {
403+
return fs.readFileSync(fileName, 'utf-8');
404+
},
405+
directoryExists(directoryName) {
406+
try {
407+
const stats = fs.statSync(directoryName);
408+
return stats && stats.isDirectory();
409+
} catch (e) {
410+
return false;
411+
}
412+
}
413+
}).resolvedModule;
414+
});
415+
}
416+
313417
getCurrentDirectory() {
314418
return this.currentDirectory;
315419
}

tests/e2e/e2e_workflow.spec.js

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -315,23 +315,45 @@ describe('Basic end-to-end Workflow', function () {
315315
let config = require(configFilePath);
316316

317317
config.compilerOptions.noImplicitAny = true;
318-
fs.writeFileSync(configFilePath, JSON.stringify(config), 'utf8');
318+
fs.writeFileSync(configFilePath, JSON.stringify(config, null, 2), 'utf8');
319319

320320
sh.rm('-rf', path.join(process.cwd(), 'dist'));
321321

322322
return ng(['build'])
323-
.then(function () {
323+
.then(() => {
324324
expect(existsSync(path.join(process.cwd(), 'dist'))).to.be.equal(true);
325325
})
326+
.then(done, done.fail);
327+
});
328+
329+
it('Turn on path mapping in tsconfig.json and rebuild', function (done) {
330+
this.timeout(420000);
331+
332+
const configFilePath = path.join(process.cwd(), 'src', 'tsconfig.json');
333+
let config = require(configFilePath);
334+
335+
config.compilerOptions.baseUrl = '';
336+
337+
// This should fail.
338+
config.compilerOptions.paths = { '@angular/*': [] };
339+
fs.writeFileSync(configFilePath, JSON.stringify(config, null, 2), 'utf8');
340+
341+
return ng(['build'])
342+
.catch(() => {
343+
return true;
344+
})
345+
.then((passed) => {
346+
expect(passed).to.equal(true);
347+
})
348+
.then(() => {
349+
// This should succeed.
350+
config.compilerOptions.paths = { '@angular/*': [ '../../node_modules/@angular/*' ] };
351+
fs.writeFileSync(configFilePath, JSON.stringify(config, null, 2), 'utf8');
352+
})
353+
.then(() => ng(['build']))
326354
.catch(() => {
327-
throw new Error('Build failed.');
355+
expect('build failed where it should have succeeded').to.equal('');
328356
})
329-
.finally(function () {
330-
// Clean `tmp` folder
331-
process.chdir(path.resolve(root, '..'));
332-
// sh.rm('-rf', './tmp'); // tmp.teardown takes too long
333-
done();
334-
});
357+
.then(done, done.fail);
335358
});
336-
337359
});

0 commit comments

Comments
 (0)