UNPKG

@o3r/dynamic-content

Version:

This module provides a mechanism to retrieve media and data depending on the host or a server specific url.

119 lines 5.03 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.updateV14_0 = exports.removeStyleLazyLoaderModule = void 0; const schematics_1 = require("@o3r/schematics"); const ts = require("typescript"); /** * Compute the position to remove a node * @param node */ const computeRemovePosition = (node) => ({ start: node.getFullStart(), length: node.getEnd() - node.getFullStart() }); /** * Find and compute remove position for an import symbol * If the symbol is the only import from the module, remove the entire import declaration * @param source * @param symbolName * @param fileName */ const removeImport = (source, symbolName, fileName) => { const importDeclaration = source.statements.find((statement) => ts.isImportDeclaration(statement) && ts.isStringLiteral(statement.moduleSpecifier) && statement.moduleSpecifier.text === fileName); if (!importDeclaration || !importDeclaration.importClause?.namedBindings || !ts.isNamedImports(importDeclaration.importClause.namedBindings)) { return undefined; } const namedImports = importDeclaration.importClause.namedBindings; const importElement = namedImports.elements.find((e) => e.name.escapedText.toString() === symbolName); if (!importElement) { return undefined; } // If this is the only import, remove the entire import declaration if (namedImports.elements.length === 1) { return computeRemovePosition(importDeclaration); } // Otherwise, just remove the specific import element return computeRemovePosition(importElement); }; /** * Find StyleLazyLoaderModule in any decorator's imports array (NgModule, Component, Injectable, etc.) * @param node */ const findStyleLazyLoaderModuleInImports = (node) => { const decorators = ts.getDecorators(node); if (!decorators) { return []; } const moduleReferences = []; decorators.forEach((decorator) => { if (!ts.isCallExpression(decorator.expression)) { return; } const metadata = decorator.expression.arguments[0]; if (!metadata || !ts.isObjectLiteralExpression(metadata)) { return; } const importsProperty = metadata.properties.find((prop) => ts.isPropertyAssignment(prop) && ts.isIdentifier(prop.name) && prop.name.escapedText.toString() === 'imports'); if (!importsProperty || !ts.isArrayLiteralExpression(importsProperty.initializer)) { return; } const moduleReference = importsProperty.initializer.elements.find((element) => ts.isIdentifier(element) && element.escapedText.toString() === 'StyleLazyLoaderModule'); if (moduleReference) { moduleReferences.push(moduleReference); } }); return moduleReferences; }; /** * Remove StyleLazyLoaderModule from decorator imports arrays and import statements * @param tree * @param context */ const removeStyleLazyLoaderModule = (tree, context) => { const files = (0, schematics_1.findFilesInTree)(tree.getDir('/'), (filePath) => /\.ts$/.test(filePath)); files.forEach(({ path }) => { const source = ts.createSourceFile(path, tree.readText(path), ts.ScriptTarget.ES2015, true); const recorder = tree.beginUpdate(path); let shouldRemoveImport = false; source.forEachChild((node) => { if (ts.isClassDeclaration(node)) { const moduleReferences = findStyleLazyLoaderModuleInImports(node); if (moduleReferences.length > 0) { moduleReferences.forEach((moduleReference) => { recorder.remove(moduleReference.getFullStart(), moduleReference.getEnd() - moduleReference.getFullStart()); }); shouldRemoveImport = true; } } }); // Remove the import statement once per file if any references were found if (shouldRemoveImport) { const removePos = removeImport(source, 'StyleLazyLoaderModule', '@o3r/dynamic-content'); if (removePos) { recorder.remove(removePos.start, removePos.length); } context.logger.info(`Removed StyleLazyLoaderModule from ${path}`); tree.commitUpdate(recorder); } else { // No changes, discard the recorder tree.commitUpdate(recorder); } }); return tree; }; exports.removeStyleLazyLoaderModule = removeStyleLazyLoaderModule; /** * Update of Otter library V14.0 */ // eslint-disable-next-line @typescript-eslint/naming-convention -- function name contains the version function updateV14_0Fn() { return exports.removeStyleLazyLoaderModule; } /** * Update of Otter library V14.0 */ // eslint-disable-next-line @typescript-eslint/naming-convention -- function name contains the version exports.updateV14_0 = (0, schematics_1.createOtterSchematic)(updateV14_0Fn); //# sourceMappingURL=index.js.map