UNPKG

@nxext/stencil

Version:

Nx plugin for stenciljs

141 lines (140 loc) 8.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.applicationGenerator = applicationGenerator; const tslib_1 = require("tslib"); const typings_1 = require("./../../utils/typings"); const devkit_1 = require("@nx/devkit"); const js_1 = require("@nx/js"); const utillities_1 = require("../../utils/utillities"); const init_1 = require("../init/init"); const path_1 = require("path"); const node_os_1 = require("node:os"); const add_project_1 = require("./lib/add-project"); const add_linting_1 = require("./lib/add-linting"); const add_cypress_1 = require("./lib/add-cypress"); const internal_1 = require("@nx/devkit/internal"); const common_1 = require("@nxext/common"); // Not re-exported by @nxext/common's ts-solution module (which only wraps // the full wireTsSolutionProject combo) - imported directly. @nx/js is // already a runtime dependency of this package. const internal_2 = require("@nx/js/internal"); function normalizeOptions(host, options) { return tslib_1.__awaiter(this, void 0, void 0, function* () { var _a, _b, _c, _d, _e, _f; yield (0, internal_1.ensureRootProjectName)(options, 'application'); const { projectName: resolvedProjectName, projectRoot, importPath, } = yield (0, internal_1.determineProjectNameAndRootOptions)(host, { name: options.name, projectType: 'application', directory: options.directory, importPath: options.importPath, }); const isUsingTsSolutionConfig = (0, common_1.isUsingTsSolutionSetup)(host); // Design 1.5 (mirrors @nxext/common's normalizeViteAppCore/@nx/react): // without an explicit --name, the Nx project identifier defaults to the // full scoped importPath in TS-solution mode. const projectName = !isUsingTsSolutionConfig || options.name ? resolvedProjectName : importPath; (_a = options.name) !== null && _a !== void 0 ? _a : (options.name = projectName); const parsedTags = options.tags ? options.tags.split(',').map((s) => s.trim()) : []; const nxJson = (0, devkit_1.readNxJson)(host); const e2eWebServerTarget = 'serve'; let e2ePort = 4200; if (((_b = nxJson.targetDefaults) === null || _b === void 0 ? void 0 : _b[e2eWebServerTarget]) && ((_d = (_c = nxJson.targetDefaults) === null || _c === void 0 ? void 0 : _c[e2eWebServerTarget].options) === null || _d === void 0 ? void 0 : _d.port)) { e2ePort = (_f = (_e = nxJson.targetDefaults) === null || _e === void 0 ? void 0 : _e[e2eWebServerTarget].options) === null || _f === void 0 ? void 0 : _f.port; } const e2eProjectName = `${projectName}-e2e`; const e2eProjectRoot = `${projectRoot}-e2e`; const e2eWebServerAddress = `http://localhost:${e2ePort}`; const style = (0, utillities_1.calculateStyle)(options.style); const appType = typings_1.AppType.application; return Object.assign(Object.assign({}, options), { name: projectName, projectName, projectRoot, projectDirectory: projectRoot, parsedTags, e2eProjectName, e2eProjectRoot, e2eWebServerAddress, e2eWebServerTarget, style, appType, importPath, // Always the plain, pre-importPath-substitution name (see schema doc). simpleProjectName: resolvedProjectName, isUsingTsSolutionConfig, useProjectJson: !isUsingTsSolutionConfig }); }); } function createFiles(host, options) { const substitutions = Object.assign(Object.assign(Object.assign({}, options), (0, devkit_1.names)(options.name)), { offsetFromRoot: (0, devkit_1.offsetFromRoot)(options.projectRoot) }); // Files identical in both modes (Design 3.2): app source, stencil config, // the runtime tsconfigs (tsconfig.app.json's TS-solution-specific // `extends`/compilerOptions divergence is applied programmatically // afterwards by `wireTsSolutionProject`, see applicationGenerator below). (0, devkit_1.generateFiles)(host, (0, path_1.join)(__dirname, './files/common'), options.projectRoot, substitutions); // The outer `tsconfig.json` is the one file that genuinely differs by // mode: legacy mode bakes Stencil's JSX compilerOptions into it (extended // by tsconfig.app.json); TS-solution mode keeps it a thin // references-only pointer (mirrors @nxext/svelte/@nxext/sveltekit - // Design 3.2). (0, devkit_1.generateFiles)(host, (0, path_1.join)(__dirname, options.isUsingTsSolutionConfig ? './files/ts-solution' : './files/non-ts-solution'), options.projectRoot, substitutions); if (options.unitTestRunner === 'none') { host.delete(`${options.projectRoot}/src/components/app-home/app-home.spec.ts`); host.delete(`${options.projectRoot}/src/components/app-root/app-root.spec.ts`); host.delete(`${options.projectRoot}/src/components/app-profile/app-profile.spec.ts`); } if (options.e2eTestRunner === 'none') { host.delete(`${options.projectRoot}/src/components/app-home/app-home.e2e.ts`); host.delete(`${options.projectRoot}/src/components/app-root/app-root.e2e.ts`); host.delete(`${options.projectRoot}/src/components/app-profile/app-profile.e2e.ts`); } } function applicationGenerator(host, schema) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const options = yield normalizeOptions(host, schema); const jsInitTask = yield (0, js_1.initGenerator)(host, Object.assign(Object.assign({}, options), { tsConfigName: 'tsconfig.base.json', skipFormat: true })); const initTask = yield (0, init_1.initGenerator)(host, Object.assign(Object.assign({}, options), { skipFormat: true })); createFiles(host, options); (0, add_project_1.addProject)(host, options); if (options.isUsingTsSolutionConfig) { // Register in pnpm-workspace.yaml/package.json.workspaces ONLY (Design // 1.2). Must run AFTER `addProject`: this project is package.json-backed // (no project.json) in TS-solution mode, and `addLinting` below resolves // the project via the project graph, which depends on this registration. // // Deliberately NOT `wireTsSolutionProject`/`updateTsconfigFiles` and NO // root tsconfig.json `references` entry: Stencil reads the project's // own `./tsconfig.json` and force-overrides `declaration: false` for // www builds (no dist-types output target) while only preserving // `moduleResolution` when it is `bundler` (see Stencil's // `getTsOptionsToExtend`). A tsconfig chain inheriting the workspace's // TS-solution base (`composite: true`, `emitDeclarationOnly: true`, // `customConditions`, `module: nodenext`, `strict`) is therefore // unbuildable for Stencil ("Composite projects may not disable // declaration emit", TS5098 customConditions/moduleResolution). The // generated `tsconfig.json` (files/ts-solution) is a standalone, // non-composite Stencil config instead - and non-composite projects // must not appear in the root tsconfig.json `references`. yield (0, internal_2.addProjectToTsSolutionWorkspace)(host, options.projectRoot); } const lintTask = yield (0, add_linting_1.addLinting)(host, options); const cypressTask = yield (0, add_cypress_1.addCypress)(host, options); const ignoresToUpdate = ['.gitignore', '.prettierignore', '.nxignore']; const toBeIgnored = ['.stencil'].join(node_os_1.EOL); ignoresToUpdate.forEach((ignoreFile) => { if (host.exists(ignoreFile)) { const gitignoreContent = host.read(ignoreFile, 'utf-8'); if (!gitignoreContent.includes('.stencil')) { host.write(ignoreFile, `${gitignoreContent} ${toBeIgnored} `); } } }); if (!options.skipFormat) { yield (0, devkit_1.formatFiles)(host); } return (0, devkit_1.runTasksInSerial)(jsInitTask, initTask, lintTask, cypressTask, () => (0, internal_1.logShowProjectCommand)(options.projectName)); }); } exports.default = applicationGenerator; //# sourceMappingURL=generator.js.map