@nxext/stencil
Version:
Nx plugin for stenciljs
99 lines • 6.26 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.addAngularGenerator = addAngularGenerator;
const tslib_1 = require("tslib");
const devkit_1 = require("@nx/devkit");
const versions_1 = require("../../../utils/versions");
const calculate_stencil_source_options_1 = require("../lib/calculate-stencil-source-options");
const path_1 = require("path");
const fileutils_1 = require("../../../utils/fileutils");
const stencil_core_utils_1 = require("../../../stencil-core-utils");
const typescript_1 = require("typescript");
const angular_ast_utils_1 = require("../../../utils/angular-ast-utils");
const generator_1 = require("../../storybook-configuration/generator");
const common_1 = require("@nxext/common");
const js_1 = require("@nx/js");
function prepareAngularLibrary(host, options) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const angularProjectName = `${options.projectName}-angular`;
const angularProjectDir = `libs/${angularProjectName}`;
yield (0, devkit_1.ensurePackage)('@nx/angular', devkit_1.NX_VERSION);
// `@nx/angular`'s root entry ships the Angular-framework build (no Node
// exports); `/generators` is Node-only and, unlike other `@nx/*` optional
// peers, has no top-level .d.ts stub for classic `moduleResolution: "node"`
// to resolve its exports-map-only subpath.
// @ts-expect-error -- see comment above; resolves fine at runtime
const generators = yield Promise.resolve().then(() => require('@nx/angular/generators'));
const libraryTarget = yield generators.libraryGenerator(host, {
directory: angularProjectDir,
skipFormat: true,
publishable: options.publishable,
importPath: options.importPath,
// `addLibraryDirectives`/`angular-ast-utils.ts` below patch a generated
// `*.module.ts` to wire up the Stencil directive proxies — Angular 21's
// `standalone: true` default skips creating that module entirely.
standalone: false,
// Forward the schema's own unitTestRunner choice; left unset, Angular
// 21+ defaults publishable libraries to `vitest-angular`, which then
// hard-validates against a vitest version this workspace doesn't pin.
unitTestRunner: options.unitTestRunner,
});
(0, devkit_1.addDependenciesToPackageJson)(host, {}, {
'@stencil/angular-output-target': versions_1.STENCIL_OUTPUTTARGET_VERSION['angular'],
});
return libraryTarget;
});
}
function addAngularOutputtarget(host, projectName, stencilProjectConfig, stencilConfigPath, stencilConfigSource, packageName) {
const angularProjectConfig = (0, devkit_1.readProjectConfiguration)(host, `${projectName}-angular`);
const realtivePath = (0, path_1.relative)((0, fileutils_1.getDistDir)(stencilProjectConfig.root), angularProjectConfig.root);
const proxyPath = (0, devkit_1.joinPathFragments)(realtivePath, 'src/generated/directives/proxies.ts');
const arrayPath = (0, devkit_1.joinPathFragments)(realtivePath, 'src/generated/directives/index.ts');
const changes = (0, devkit_1.applyChangesToString)(stencilConfigSource.text, [
...(0, common_1.addImport)(stencilConfigSource, `import { angularOutputTarget, ValueAccessorConfig } from '@stencil/angular-output-target';`),
...(0, stencil_core_utils_1.addOutputTarget)(stencilConfigSource, `
angularOutputTarget({
componentCorePackage: '${packageName}',
directivesProxyFile: '${proxyPath}',
directivesArrayFile: '${arrayPath}',
valueAccessorConfigs: angularValueAccessorBindings
}),
`),
]);
host.write(stencilConfigPath, changes);
(0, js_1.addGlobal)(host, stencilConfigSource, stencilConfigPath, 'const angularValueAccessorBindings: ValueAccessorConfig[] = [];');
}
function addLibraryDirectives(host, options) {
const angularProjectName = `${options.projectName}-angular`;
// The Angular library was already generated by `prepareAngularLibrary`
// above, so its root is read from the project graph rather than
// re-derived from the (possibly non-default) workspace layout.
const angularProjectRoot = (0, devkit_1.readProjectConfiguration)(host, angularProjectName).root;
// `@nx/angular`'s libraryGenerator (Angular 21+) names the module file
// `<project>-module.ts`, not the older `<project>.module.ts`.
const modulePath = (0, devkit_1.joinPathFragments)(`${angularProjectRoot}/src/lib/${angularProjectName}-module.ts`);
if (!host.exists(modulePath)) {
return;
}
const sourceText = host.read(modulePath, 'utf-8');
let sourceFile = (0, typescript_1.createSourceFile)(modulePath, sourceText, typescript_1.ScriptTarget.Latest, true);
sourceFile = (0, angular_ast_utils_1.addDeclarationToModule)(host, sourceFile, modulePath, '...DIRECTIVES');
sourceFile = (0, angular_ast_utils_1.addExportToModule)(host, sourceFile, modulePath, '...DIRECTIVES');
const changes = (0, devkit_1.applyChangesToString)(sourceFile.getFullText(), [
...(0, common_1.addImport)(sourceFile, `import { DIRECTIVES } from '../generated/directives';`),
...(0, common_1.addImport)(sourceFile, `import { defineCustomElements} from '${(0, generator_1.getProjectTsImportPath)(host, options.projectName)}/loader';`),
(0, common_1.addAfterLastImport)(sourceFile, `\ndefineCustomElements();\n`),
]);
host.write(modulePath, changes);
}
function addAngularGenerator(host, options) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const libraryTarget = yield prepareAngularLibrary(host, options);
addLibraryDirectives(host, options);
const { stencilProjectConfig, stencilConfigPath, stencilConfigSource, packageName, } = (0, calculate_stencil_source_options_1.calculateStencilSourceOptions)(host, options.projectName);
addAngularOutputtarget(host, options.projectName, stencilProjectConfig, stencilConfigPath, stencilConfigSource, packageName);
return libraryTarget;
});
}
exports.default = addAngularGenerator;
//# sourceMappingURL=generator.js.map