UNPKG

ng-packagr

Version:

Compile and package Angular libraries in Angular Package Format (APF)

100 lines • 3.37 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.fileLoaderPlugin = fileLoaderPlugin; const node_path_1 = require("node:path"); const log_1 = __importDefault(require("../utils/log")); const path_1 = require("../utils/path"); /** * Loads a file and its map. */ function fileLoaderPlugin(fileCache, resolutionExtensions, dtsMode) { const resolutionCache = new Map(); function resolveFile(id, importer) { let resolved; if (importer) { if (id[0] !== '.' && id[0] !== '/' && !(0, node_path_1.isAbsolute)(id)) { return; } resolved = (0, path_1.ensureUnixPath)((0, node_path_1.resolve)((0, node_path_1.dirname)(importer), id)); } else { resolved = (0, path_1.ensureUnixPath)(id); } if (fileCache.has(resolved)) { return resolved; } const ext = (0, node_path_1.extname)(resolved); const base = resolved.slice(0, -ext.length); if (dtsMode) { let potential; switch (ext) { case '.js': case '.ts': potential = `${base}.d.ts`; break; case '.mjs': case '.mts': potential = `${base}.d.mts`; break; case '.cjs': case '.cts': potential = `${base}.d.cts`; break; } if (potential && fileCache.has(potential)) { return potential; } } else { let potential; switch (ext) { case '.ts': potential = `${base}.js`; break; case '.mts': potential = `${base}.mjs`; break; case '.cts': potential = `${base}.cjs`; break; } if (potential && fileCache.has(potential)) { return potential; } } for (const suffix of resolutionExtensions) { const potential = resolved + suffix; if (fileCache.has(potential)) { return potential; } } } return { name: 'file-loader', resolveId: function (id, importer) { const cacheKey = importer ? `${importer}\0${id}` : id; const cached = resolutionCache.get(cacheKey); if (cached !== undefined) { return cached ?? undefined; } const resolved = resolveFile(id, importer); resolutionCache.set(cacheKey, resolved ?? null); return resolved; }, load: function (id) { log_1.default.debug(`file-loader ${id}`); const data = fileCache.get(id); if (!data) { throw new Error(`Could not load '${id}' from memory.`); } return { code: data.content, map: fileCache.get(`${id}.map`)?.content, }; }, }; } //# sourceMappingURL=file-loader-plugin.js.map