123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- const fs = require("fs");
- const micromatch = require("micromatch");
- const path = require("path");
- const typescript = require("typescript");
- const constants = require("./constants");
- /**
- * The default error formatter.
- */
- function defaultErrorFormatter(error, colors) {
- const messageColor = error.severity === 'warning' ? colors.bold.yellow : colors.bold.red;
- return (colors.grey('[tsl] ') +
- messageColor(error.severity.toUpperCase()) +
- (error.file === ''
- ? ''
- : messageColor(' in ') +
- colors.bold.cyan(`${error.file}(${error.line},${error.character})`)) +
- constants.EOL +
- messageColor(` TS${error.code}: ${error.content}`));
- }
- /**
- * Take TypeScript errors, parse them and format to webpack errors
- * Optionally adds a file name
- */
- function formatErrors(diagnostics, loaderOptions, colors, compiler, merge, context) {
- return diagnostics === undefined
- ? []
- : diagnostics
- .filter(diagnostic => {
- if (loaderOptions.ignoreDiagnostics.indexOf(diagnostic.code) !== -1) {
- return false;
- }
- if (loaderOptions.reportFiles.length > 0 &&
- diagnostic.file !== undefined) {
- const relativeFileName = path.relative(context, diagnostic.file.fileName);
- const matchResult = micromatch([relativeFileName], loaderOptions.reportFiles);
- if (matchResult.length === 0) {
- return false;
- }
- }
- return true;
- })
- .map(diagnostic => {
- const file = diagnostic.file;
- const position = file === undefined
- ? undefined
- : file.getLineAndCharacterOfPosition(diagnostic.start);
- const errorInfo = {
- code: diagnostic.code,
- severity: compiler.DiagnosticCategory[diagnostic.category].toLowerCase(),
- content: compiler.flattenDiagnosticMessageText(diagnostic.messageText, constants.EOL),
- file: file === undefined ? '' : path.normalize(file.fileName),
- line: position === undefined ? 0 : position.line + 1,
- character: position === undefined ? 0 : position.character + 1,
- context
- };
- const message = loaderOptions.errorFormatter === undefined
- ? defaultErrorFormatter(errorInfo, colors)
- : loaderOptions.errorFormatter(errorInfo, colors);
- const error = makeError(message, merge.file === undefined ? errorInfo.file : merge.file, position === undefined
- ? undefined
- : { line: errorInfo.line, character: errorInfo.character });
- return Object.assign(error, merge);
- });
- }
- exports.formatErrors = formatErrors;
- function readFile(fileName, encoding = 'utf8') {
- fileName = path.normalize(fileName);
- try {
- return fs.readFileSync(fileName, encoding);
- }
- catch (e) {
- return undefined;
- }
- }
- exports.readFile = readFile;
- function makeError(message, file, location) {
- return {
- message,
- location,
- file,
- loaderSource: 'ts-loader'
- };
- }
- exports.makeError = makeError;
- function appendSuffixIfMatch(patterns, filePath, suffix) {
- if (patterns.length > 0) {
- for (const regexp of patterns) {
- if (filePath.match(regexp) !== null) {
- return filePath + suffix;
- }
- }
- }
- return filePath;
- }
- exports.appendSuffixIfMatch = appendSuffixIfMatch;
- function appendSuffixesIfMatch(suffixDict, filePath) {
- let amendedPath = filePath;
- for (const suffix in suffixDict) {
- amendedPath = appendSuffixIfMatch(suffixDict[suffix], amendedPath, suffix);
- }
- return amendedPath;
- }
- exports.appendSuffixesIfMatch = appendSuffixesIfMatch;
- function unorderedRemoveItem(array, item) {
- for (let i = 0; i < array.length; i++) {
- if (array[i] === item) {
- // Fill in the "hole" left at `index`.
- array[i] = array[array.length - 1];
- array.pop();
- return true;
- }
- }
- return false;
- }
- exports.unorderedRemoveItem = unorderedRemoveItem;
- /**
- * Recursively collect all possible dependants of passed file
- */
- function collectAllDependants(reverseDependencyGraph, fileName, collected = {}) {
- const result = {};
- result[fileName] = true;
- collected[fileName] = true;
- const dependants = reverseDependencyGraph[fileName];
- if (dependants !== undefined) {
- Object.keys(dependants).forEach(dependantFileName => {
- if (!collected[dependantFileName]) {
- collectAllDependants(reverseDependencyGraph, dependantFileName, collected).forEach(fName => (result[fName] = true));
- }
- });
- }
- return Object.keys(result);
- }
- exports.collectAllDependants = collectAllDependants;
- /**
- * Recursively collect all possible dependencies of passed file
- */
- function collectAllDependencies(dependencyGraph, filePath, collected = {}) {
- const result = {};
- result[filePath] = true;
- collected[filePath] = true;
- const directDependencies = dependencyGraph[filePath];
- if (directDependencies !== undefined) {
- directDependencies.forEach(dependencyModule => {
- if (!collected[dependencyModule.originalFileName]) {
- collectAllDependencies(dependencyGraph, dependencyModule.resolvedFileName, collected).forEach(depFilePath => (result[depFilePath] = true));
- }
- });
- }
- return Object.keys(result);
- }
- exports.collectAllDependencies = collectAllDependencies;
- function arrify(val) {
- if (val === null || val === undefined) {
- return [];
- }
- return Array.isArray(val) ? val : [val];
- }
- exports.arrify = arrify;
- function ensureProgram(instance) {
- if (instance && instance.watchHost) {
- if (instance.hasUnaccountedModifiedFiles) {
- if (instance.changedFilesList) {
- instance.watchHost.updateRootFileNames();
- }
- if (instance.watchOfFilesAndCompilerOptions) {
- instance.program = instance.watchOfFilesAndCompilerOptions
- .getProgram()
- .getProgram();
- }
- instance.hasUnaccountedModifiedFiles = false;
- }
- return instance.program;
- }
- if (instance.languageService) {
- return instance.languageService.getProgram();
- }
- return instance.program;
- }
- exports.ensureProgram = ensureProgram;
- function supportsProjectReferences(instance) {
- const program = ensureProgram(instance);
- return program && !!program.getProjectReferences;
- }
- exports.supportsProjectReferences = supportsProjectReferences;
- function isUsingProjectReferences(instance) {
- if (instance.loaderOptions.projectReferences &&
- supportsProjectReferences(instance)) {
- const program = ensureProgram(instance);
- return Boolean(program && program.getProjectReferences());
- }
- return false;
- }
- exports.isUsingProjectReferences = isUsingProjectReferences;
- /**
- * Gets the project reference for a file from the cache if it exists,
- * or gets it from TypeScript and caches it otherwise.
- */
- function getAndCacheProjectReference(filePath, instance) {
- const file = instance.files.get(filePath);
- if (file !== undefined && file.projectReference) {
- return file.projectReference.project;
- }
- const projectReference = getProjectReferenceForFile(filePath, instance);
- if (file !== undefined) {
- file.projectReference = { project: projectReference };
- }
- return projectReference;
- }
- exports.getAndCacheProjectReference = getAndCacheProjectReference;
- function getResolvedProjectReferences(program) {
- const getProjectReferences = program.getResolvedProjectReferences ||
- program.getProjectReferences;
- if (getProjectReferences) {
- return getProjectReferences();
- }
- return;
- }
- function getProjectReferenceForFile(filePath, instance) {
- if (isUsingProjectReferences(instance)) {
- const program = ensureProgram(instance);
- return (program &&
- getResolvedProjectReferences(program).find(ref => (ref &&
- ref.commandLine.fileNames.some(file => path.normalize(file) === filePath)) ||
- false));
- }
- return;
- }
- function validateSourceMapOncePerProject(instance, loader, jsFileName, project) {
- const { projectsMissingSourceMaps = new Set() } = instance;
- if (!projectsMissingSourceMaps.has(project.sourceFile.fileName)) {
- instance.projectsMissingSourceMaps = projectsMissingSourceMaps;
- projectsMissingSourceMaps.add(project.sourceFile.fileName);
- const mapFileName = jsFileName + '.map';
- if (!instance.compiler.sys.fileExists(mapFileName)) {
- const [relativeJSPath, relativeProjectConfigPath] = [
- path.relative(loader.rootContext, jsFileName),
- path.relative(loader.rootContext, project.sourceFile.fileName)
- ];
- loader.emitWarning(new Error('Could not find source map file for referenced project output ' +
- `${relativeJSPath}. Ensure the 'sourceMap' compiler option ` +
- `is enabled in ${relativeProjectConfigPath} to ensure Webpack ` +
- 'can map project references to the appropriate source files.'));
- }
- }
- }
- exports.validateSourceMapOncePerProject = validateSourceMapOncePerProject;
- /**
- * Gets the output JS file path for an input file governed by a composite project.
- * Pulls from the cache if it exists; computes and caches the result otherwise.
- */
- function getAndCacheOutputJSFileName(inputFileName, projectReference, instance) {
- const file = instance.files.get(inputFileName);
- if (file && file.projectReference && file.projectReference.outputFileName) {
- return file.projectReference.outputFileName;
- }
- const outputFileName = getOutputJavaScriptFileName(inputFileName, projectReference);
- if (file !== undefined) {
- file.projectReference = file.projectReference || {
- project: projectReference
- };
- file.projectReference.outputFileName = outputFileName;
- }
- return outputFileName;
- }
- exports.getAndCacheOutputJSFileName = getAndCacheOutputJSFileName;
- // Adapted from https://github.com/Microsoft/TypeScript/blob/45101491c0b077c509b25830ef0ee5f85b293754/src/compiler/tsbuild.ts#L305
- function getOutputJavaScriptFileName(inputFileName, projectReference) {
- const { options } = projectReference.commandLine;
- const projectDirectory = options.rootDir || path.dirname(projectReference.sourceFile.fileName);
- const relativePath = path.relative(projectDirectory, inputFileName);
- const outputPath = path.resolve(options.outDir || projectDirectory, relativePath);
- const newExtension = constants.jsonRegex.test(inputFileName)
- ? '.json'
- : constants.tsxRegex.test(inputFileName) &&
- options.jsx === typescript.JsxEmit.Preserve
- ? '.jsx'
- : '.js';
- return outputPath.replace(constants.extensionRegex, newExtension);
- }
|