|
| 1 | +import { |
| 2 | + addDependenciesToPackageJson, |
| 3 | + applyChangesToString, |
| 4 | + ensurePackage, |
| 5 | + getWorkspaceLayout, |
| 6 | + NX_VERSION, |
| 7 | + readProjectConfiguration, |
| 8 | + runTasksInSerial, |
| 9 | + Tree, |
| 10 | +} from '@nx/devkit'; |
| 11 | +import { initGenerator as jsInitGenerator } from '@nx/js'; |
| 12 | +import { AddOutputtargetSchematicSchema } from '../schema'; |
| 13 | +import { Linter } from '@nx/eslint'; |
| 14 | +import { STENCIL_OUTPUTTARGET_VERSION } from '../../../utils/versions'; |
| 15 | +import * as ts from 'typescript'; |
| 16 | +import { getDistDir, getRelativePath } from '../../../utils/fileutils'; |
| 17 | +import { addImport } from '@nxext/common'; |
| 18 | +import { addOutputTarget } from '../../../stencil-core-utils'; |
| 19 | +import { calculateStencilSourceOptions } from '../lib/calculate-stencil-source-options'; |
| 20 | + |
| 21 | +async function prepareVueLibrary( |
| 22 | + host: Tree, |
| 23 | + options: AddOutputtargetSchematicSchema |
| 24 | +) { |
| 25 | + const { libsDir } = getWorkspaceLayout(host); |
| 26 | + const vueProjectName = `${options.projectName}-vue`; |
| 27 | + const vueProjectDir = `libs/${vueProjectName}`; |
| 28 | + |
| 29 | + const jsInitTask = await jsInitGenerator(host, { |
| 30 | + ...options, |
| 31 | + tsConfigName: 'tsconfig.base.json', |
| 32 | + skipFormat: true, |
| 33 | + }); |
| 34 | + |
| 35 | + ensurePackage('@nx/vue', NX_VERSION); |
| 36 | + const { libraryGenerator } = await import('@nx/vue'); |
| 37 | + const libraryTarget = await libraryGenerator(host, { |
| 38 | + directory: vueProjectDir, |
| 39 | + publishable: options.publishable, |
| 40 | + bundler: options.publishable ? 'vite' : 'none', |
| 41 | + importPath: options.importPath, |
| 42 | + component: false, |
| 43 | + skipTsConfig: false, |
| 44 | + skipFormat: true, |
| 45 | + unitTestRunner: 'vitest', |
| 46 | + linter: Linter.EsLint, |
| 47 | + }); |
| 48 | + |
| 49 | + await addDependenciesToPackageJson( |
| 50 | + host, |
| 51 | + {}, |
| 52 | + { |
| 53 | + '@stencil/vue-output-target': STENCIL_OUTPUTTARGET_VERSION['vue'], |
| 54 | + } |
| 55 | + ); |
| 56 | + |
| 57 | + host.write( |
| 58 | + `${libsDir}/${vueProjectName}/src/index.ts`, |
| 59 | + `export * from './generated/components';` |
| 60 | + ); |
| 61 | + |
| 62 | + return runTasksInSerial(jsInitTask, libraryTarget); |
| 63 | +} |
| 64 | + |
| 65 | +function addVueOutputtarget( |
| 66 | + tree: Tree, |
| 67 | + projectName: string, |
| 68 | + stencilProjectConfig, |
| 69 | + stencilConfigPath: string, |
| 70 | + stencilConfigSource: ts.SourceFile, |
| 71 | + packageName: string |
| 72 | +) { |
| 73 | + const vueProjectConfig = readProjectConfiguration(tree, `${projectName}-vue`); |
| 74 | + const realtivePath = getRelativePath( |
| 75 | + getDistDir(stencilProjectConfig.root), |
| 76 | + vueProjectConfig.root |
| 77 | + ); |
| 78 | + |
| 79 | + const changes = applyChangesToString(stencilConfigSource.text, [ |
| 80 | + ...addImport( |
| 81 | + stencilConfigSource, |
| 82 | + `import { vueOutputTarget } from '@stencil/vue-output-target';` |
| 83 | + ), |
| 84 | + ...addOutputTarget( |
| 85 | + stencilConfigSource, |
| 86 | + ` |
| 87 | + vueOutputTarget({ |
| 88 | + componentCorePackage: '${packageName}', |
| 89 | + proxiesFile: '${realtivePath}/src/generated/components.ts', |
| 90 | + includeDefineCustomElements: true, |
| 91 | + }) |
| 92 | + ` |
| 93 | + ), |
| 94 | + ]); |
| 95 | + tree.write(stencilConfigPath, changes); |
| 96 | +} |
| 97 | + |
| 98 | +export async function addVueGenerator( |
| 99 | + host: Tree, |
| 100 | + options: AddOutputtargetSchematicSchema |
| 101 | +) { |
| 102 | + const libraryTarget = await prepareVueLibrary(host, options); |
| 103 | + |
| 104 | + const { |
| 105 | + stencilProjectConfig, |
| 106 | + stencilConfigPath, |
| 107 | + stencilConfigSource, |
| 108 | + packageName, |
| 109 | + } = calculateStencilSourceOptions(host, options.projectName); |
| 110 | + |
| 111 | + addVueOutputtarget( |
| 112 | + host, |
| 113 | + options.projectName, |
| 114 | + stencilProjectConfig, |
| 115 | + stencilConfigPath, |
| 116 | + stencilConfigSource, |
| 117 | + packageName |
| 118 | + ); |
| 119 | + |
| 120 | + return libraryTarget; |
| 121 | +} |
| 122 | + |
| 123 | +export default addVueGenerator; |
0 commit comments