UNPKG

@nxext/stencil

Version:

Nx plugin for stenciljs

120 lines 7.25 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.libraryGenerator = libraryGenerator; const tslib_1 = require("tslib"); const devkit_1 = require("@nx/devkit"); const typings_1 = require("../../utils/typings"); const utillities_1 = require("../../utils/utillities"); const init_1 = require("../../generators/init/init"); const add_project_1 = require("./lib/add-project"); const make_lib_buildable_1 = require("../../generators/make-lib-buildable/make-lib-buildable"); const internal_1 = require("@nx/devkit/internal"); const js_1 = require("@nx/js"); 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; yield (0, internal_1.ensureRootProjectName)(options, 'library'); const { projectName: resolvedProjectName, projectRoot, importPath, } = yield (0, internal_1.determineProjectNameAndRootOptions)(host, { name: options.name, projectType: 'library', directory: options.directory, importPath: options.importPath, }); const isUsingTsSolutionConfig = (0, common_1.isUsingTsSolutionSetup)(host); // Design 1.5 (mirrors @nxext/common's normalizeViteLibCore/@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 style = (0, utillities_1.calculateStyle)(options.style); const appType = typings_1.AppType.library; return Object.assign(Object.assign({}, options), { name: projectName, projectName, projectRoot, projectDirectory: projectRoot, parsedTags, style, appType, importPath, // Always the plain, pre-importPath-substitution name (see schema doc) - // used for Stencil's own `namespace` config value, which must never // contain the npm scope. 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): lib source, stencil config, // the runtime tsconfigs (tsconfig.lib.json's TS-solution-specific // `extends`/compilerOptions divergence is applied programmatically // afterwards by `wireTsSolutionProject`, see libraryGenerator below). (0, devkit_1.generateFiles)(host, (0, devkit_1.joinPathFragments)(__dirname, './files/common'), options.projectRoot, substitutions); // The outer `tsconfig.json` is the one file that genuinely differs by // mode - see the application generator's identical comment. (0, devkit_1.generateFiles)(host, (0, devkit_1.joinPathFragments)(__dirname, options.isUsingTsSolutionConfig ? './files/ts-solution' : './files/non-ts-solution'), options.projectRoot, substitutions); if (options.unitTestRunner === 'none') { host.delete(`${options.projectRoot}/src/components/my-component/my-component.spec.ts`); } if (options.e2eTestRunner === 'none') { host.delete(`${options.projectRoot}/src/components/my-component/my-component.e2e.ts`); } if (!options.component) { host.delete(`${options.projectRoot}/src/components/my-component`); } } function libraryGenerator(host, schema) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const options = yield normalizeOptions(host, schema); if (options.publishable === true && !options.importPath) { throw new Error(`For publishable libs you have to provide a proper "--importPath" which needs to be a valid npm package name (e.g. my-awesome-lib or @myorg/my-lib)`); } 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, options); 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 `make-lib-buildable` below // (when buildable/publishable) resolves the project via // `readProjectConfiguration`, which depends on this registration. // // Deliberately NOT `wireTsSolutionProject`/`updateTsconfigFiles` and NO // root tsconfig.json `references` entry - same rationale as the // application generator (see the detailed comment there): Stencil // reads the project's own `./tsconfig.json`, force-overrides // declaration emit per output target, and only preserves // `moduleResolution: bundler`; the workspace TS-solution base // (`composite`, `emitDeclarationOnly`, `customConditions`, // `module: nodenext`, `strict`) is unbuildable for it. The generated // `tsconfig.json` (files/ts-solution) is a standalone, non-composite // Stencil config - and non-composite projects must not appear in the // root tsconfig.json `references`. yield (0, internal_2.addProjectToTsSolutionWorkspace)(host, options.projectRoot); } // TS-solution mode never writes a `paths` entry (Design 1.6): cross-project // resolution happens via package-manager workspace symlinks + project // references (wireTsSolutionProject above) instead. (0, common_1.maybeAddTsConfigPath)(host, options.importPath, [(0, devkit_1.joinPathFragments)(options.projectRoot, 'src/index.ts')], options.isUsingTsSolutionConfig); if (options.buildable || options.publishable) { yield (0, make_lib_buildable_1.makeLibBuildableGenerator)(host, { name: options.projectName, importPath: options.importPath, style: options.style, }); } if (!options.skipFormat) { yield (0, devkit_1.formatFiles)(host); } return (0, devkit_1.runTasksInSerial)(jsInitTask, initTask, () => (0, internal_1.logShowProjectCommand)(options.projectName)); }); } exports.default = libraryGenerator; //# sourceMappingURL=generator.js.map