ng-packagr
Version:
Compile and package Angular libraries in Angular Package Format (APF)
93 lines • 3.49 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.fileLoaderPlugin = fileLoaderPlugin;
const node_path_1 = require("node:path");
const log = __importStar(require("../utils/log"));
const path_1 = require("../utils/path");
/**
* A regex used to strip the file extension from a file path.
*/
const FILE_EXT_REGEXP = /\.(c|m)?(t|j)s$/;
/**
* Loads a file and it's map.
*/
function fileLoaderPlugin(fileCache, resolutionExtensions, dtsMode) {
return {
name: 'file-loader',
resolveId: function (id, importer) {
const normalizedId = (0, path_1.ensureUnixPath)(id);
if (fileCache.has(normalizedId)) {
return normalizedId;
}
const potentialId = normalizedId.endsWith('.d.ts')
? normalizedId
: normalizedId.replace(FILE_EXT_REGEXP, (_match, p1) => {
if (dtsMode) {
return p1 ? `.d.${p1}ts` : '.d.ts';
}
return p1 ? `.${p1}js` : '.js';
});
if (fileCache.has(potentialId)) {
return potentialId;
}
if (!importer) {
return;
}
const resolved = (0, path_1.ensureUnixPath)((0, node_path_1.resolve)((0, node_path_1.dirname)(importer), potentialId));
if (fileCache.has(resolved)) {
return resolved;
}
for (const suffix of resolutionExtensions) {
const potential = resolved + suffix;
if (fileCache.has(potential)) {
return potential;
}
}
},
load: function (id) {
log.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