@nx/angular
Version:
56 lines (55 loc) • 2.59 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.exportComponentInEntryPoint = exportComponentInEntryPoint;
const devkit_1 = require("@nx/devkit");
const ensure_typescript_1 = require("@nx/js/src/utils/typescript/ensure-typescript");
const entry_point_1 = require("../../utils/entry-point");
const path_1 = require("../../utils/path");
const module_1 = require("./module");
function exportComponentInEntryPoint(tree, schema) {
if (!schema.export || schema.skipImport) {
return;
}
const { root, projectType } = (0, devkit_1.readProjectConfiguration)(tree, schema.projectName);
if (projectType === 'application') {
return;
}
const entryPointPath = (0, entry_point_1.locateLibraryEntryPointFromDirectory)(tree, schema.directory, root, schema.projectSourceRoot);
if (!entryPointPath) {
devkit_1.logger.warn(`Unable to determine whether the component should be exported in the library entry point file. ` +
`The library's entry point file could not be found. Skipping exporting the component in the entry point file.`);
return;
}
if (!schema.standalone) {
let modulePath;
try {
modulePath = (0, module_1.findModuleFromOptions)(tree, schema, root);
}
catch (e) {
modulePath = (0, module_1.findModuleFromOptions)(tree, {
...schema,
moduleExt: '-module.ts',
routingModuleExt: '-routing-module.ts',
}, root);
}
if (!shouldExportInEntryPoint(tree, entryPointPath, modulePath)) {
return;
}
}
const relativePathFromEntryPoint = (0, path_1.getRelativeImportToFile)(entryPointPath, schema.filePath);
const updateEntryPointContent = (0, devkit_1.stripIndents) `${tree.read(entryPointPath, 'utf-8')}
export * from '${relativePathFromEntryPoint}';`;
tree.write(entryPointPath, updateEntryPointContent);
}
function shouldExportInEntryPoint(tree, entryPoint, modulePath) {
if (!modulePath) {
return false;
}
(0, ensure_typescript_1.ensureTypescript)();
const { tsquery } = require('@phenomnomnominal/tsquery');
const moduleImportPath = (0, path_1.getRelativeImportToFile)(entryPoint, modulePath);
const entryPointContent = tree.read(entryPoint, 'utf-8');
const entryPointAst = tsquery.ast(entryPointContent);
const moduleExport = tsquery(entryPointAst, `ExportDeclaration StringLiteral[value='${moduleImportPath}']`, { visitAllChildren: true })[0];
return Boolean(moduleExport);
}