ng-packagr
Version:
Compile and package Angular libraries in Angular Package Format (APF)
161 lines • 7.95 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.cacheCompilerHost = cacheCompilerHost;
exports.augmentProgramWithVersioning = augmentProgramWithVersioning;
const crypto_1 = require("crypto");
const esbuild_1 = require("esbuild");
const node_assert_1 = __importDefault(require("node:assert"));
const node_path_1 = require("node:path");
const typescript_1 = __importDefault(require("typescript"));
const node_1 = require("../graph/node");
const nodes_1 = require("../ng-package/nodes");
const log_1 = require("../utils/log");
const path_1 = require("../utils/path");
function cacheCompilerHost(graph, entryPoint, compilerOptions, moduleResolutionCache, stylesheetProcessor, inlineStyleLanguage, sourcesFileCache = entryPoint.cache.sourcesFileCache) {
const compilerHost = typescript_1.default.createIncrementalCompilerHost(compilerOptions);
const getNode = (fileName) => {
const nodeUri = (0, nodes_1.fileUrl)((0, path_1.ensureUnixPath)(fileName));
let node = graph.get(nodeUri);
if (!node) {
node = new node_1.Node(nodeUri);
graph.put(node);
}
return node;
};
const addDependee = (fileName) => {
const node = getNode(fileName);
entryPoint.dependsOn(node);
};
return {
...compilerHost,
// Set the parsing mode to the same as TS 5.3+ default for tsc. This provides a parse
// performance improvement by skipping non-type related JSDoc parsing.
jsDocParsingMode: typescript_1.default.JSDocParsingMode.ParseForTypeErrors,
// ts specific
getModuleResolutionCache: () => moduleResolutionCache,
fileExists: (fileName) => {
const cache = sourcesFileCache.getOrCreate(fileName);
if (cache.exists === undefined) {
cache.exists = compilerHost.fileExists(fileName);
}
return cache.exists;
},
getSourceFile: (fileName, languageVersion, onError, shouldCreateNewSourceFile, ...parameters) => {
addDependee(fileName);
const cache = sourcesFileCache.getOrCreate(fileName);
if (shouldCreateNewSourceFile || !cache.sourceFile) {
cache.sourceFile = compilerHost.getSourceFile(fileName, languageVersion, onError, true, ...parameters);
}
return cache.sourceFile;
},
writeFile: (fileName, data, writeByteOrderMark, onError, sourceFiles) => {
if (fileName.includes('.ngtypecheck.')) {
return;
}
const extension = (0, node_path_1.extname)(fileName);
if (!sourceFiles?.length && extension === '.tsbuildinfo') {
// Save builder info contents to specified location
compilerHost.writeFile(fileName, data, writeByteOrderMark, onError, sourceFiles);
return;
}
(0, node_assert_1.default)(sourceFiles?.length === 1, 'Invalid TypeScript program emit for ' + fileName);
const outputCache = entryPoint.cache.outputCache;
if (outputCache.get(fileName)?.content === data) {
return;
}
outputCache.set(fileName, {
content: data,
version: (0, crypto_1.createHash)('sha256').update(data).digest('hex'),
});
},
readFile: (fileName) => {
addDependee(fileName);
const cache = sourcesFileCache.getOrCreate(fileName);
if (cache.content === undefined) {
cache.content = compilerHost.readFile(fileName);
}
return cache.content;
},
resourceNameToFileName: (resourceName, containingFilePath) => {
const resourcePath = (0, node_path_1.resolve)((0, node_path_1.dirname)(containingFilePath), resourceName);
const containingNode = getNode(containingFilePath);
const resourceNode = getNode(resourcePath);
containingNode.dependsOn(resourceNode);
return resourcePath;
},
readResource: async (fileName) => {
addDependee(fileName);
const cache = sourcesFileCache.getOrCreate(fileName);
if (cache.content === undefined) {
if (!compilerHost.fileExists(fileName)) {
throw new Error(`Cannot read file ${fileName}.`);
}
if (/(?:html?|svg)$/.test((0, node_path_1.extname)(fileName))) {
// template
cache.content = compilerHost.readFile(fileName);
}
else {
// stylesheet
if (!stylesheetProcessor) {
throw new Error('stylesheetProcessor is not available for processing stylesheets');
}
const { referencedFiles, contents, errors: esbuildErrors, warnings: esBuildWarnings, } = await stylesheetProcessor.bundleFile(fileName);
const node = getNode(fileName);
const depNodes = [...(referencedFiles ?? [])].map(getNode).filter(n => n !== node);
node.dependsOn(depNodes);
for (const n of node.dependees) {
if (n.url.endsWith('.ts')) {
n.dependsOn(depNodes);
}
}
if (esBuildWarnings?.length) {
(await (0, esbuild_1.formatMessages)(esBuildWarnings, { kind: 'warning' })).forEach(msg => (0, log_1.warn)(msg));
}
if (esbuildErrors?.length) {
(await (0, esbuild_1.formatMessages)(esbuildErrors, { kind: 'error' })).forEach(msg => (0, log_1.error)(msg));
throw new Error(`An error has occuried while processing ${fileName}.`);
}
return contents;
}
cache.exists = true;
}
return cache.content ?? '';
},
transformResource: async (data, context) => {
const { containingFile, resourceFile, type } = context;
if (resourceFile || type !== 'style') {
return null;
}
if (inlineStyleLanguage) {
const { contents, referencedFiles, errors: esbuildErrors, warnings: esBuildWarnings, } = await stylesheetProcessor.bundleInline(data, containingFile, containingFile.endsWith('.html') ? 'css' : inlineStyleLanguage);
const node = getNode(containingFile);
node.dependsOn([...referencedFiles].map(getNode));
if (esBuildWarnings?.length) {
(await (0, esbuild_1.formatMessages)(esBuildWarnings, { kind: 'warning' })).forEach(msg => (0, log_1.warn)(msg));
}
if (esbuildErrors?.length) {
(await (0, esbuild_1.formatMessages)(esbuildErrors, { kind: 'error' })).forEach(msg => (0, log_1.error)(msg));
throw new Error(`An error has occuried while processing ${containingFile}.`);
}
return { content: contents };
}
return null;
},
};
}
function augmentProgramWithVersioning(program) {
const baseGetSourceFiles = program.getSourceFiles;
program.getSourceFiles = function (...parameters) {
const files = baseGetSourceFiles(...parameters);
for (const file of files) {
if (file.version === undefined) {
file.version = (0, crypto_1.createHash)('sha256').update(file.text).digest('hex');
}
}
return files;
};
}
//# sourceMappingURL=cache-compiler-host.js.map