Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(angular): support exporting standalone component in library's en…
…try point file (#10544)
- Loading branch information
1 parent
10e9992
commit 98014dd
Showing
7 changed files
with
223 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
packages/angular/src/generators/component/lib/component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import type { Tree } from '@nrwl/devkit'; | ||
import { | ||
joinPathFragments, | ||
logger, | ||
names, | ||
normalizePath, | ||
readProjectConfiguration, | ||
readWorkspaceConfiguration, | ||
stripIndents, | ||
} from '@nrwl/devkit'; | ||
import type { Schema } from '../schema'; | ||
import { | ||
locateLibraryEntryPointFromDirectory, | ||
shouldExportInEntryPoint, | ||
} from './entry-point'; | ||
import { findModuleFromOptions } from './module'; | ||
import { getRelativeImportToFile } from './path'; | ||
|
||
export function exportComponentInEntryPoint(tree: Tree, schema: Schema): void { | ||
if (!schema.export || (schema.skipImport && !schema.standalone)) { | ||
return; | ||
} | ||
|
||
const project = | ||
schema.project ?? readWorkspaceConfiguration(tree).defaultProject; | ||
|
||
const { root, sourceRoot, projectType } = readProjectConfiguration( | ||
tree, | ||
project | ||
); | ||
|
||
if (projectType === 'application') { | ||
return; | ||
} | ||
|
||
const componentNames = names(schema.name); | ||
|
||
const componentFileName = `${componentNames.fileName}.${ | ||
schema.type ? names(schema.type).fileName : 'component' | ||
}`; | ||
|
||
const projectSourceRoot = sourceRoot ?? joinPathFragments(root, 'src'); | ||
schema.path ??= joinPathFragments(projectSourceRoot, 'lib'); | ||
const componentDirectory = schema.flat | ||
? normalizePath(schema.path) | ||
: joinPathFragments(schema.path, componentNames.fileName); | ||
|
||
const componentFilePath = joinPathFragments( | ||
componentDirectory, | ||
`${componentFileName}.ts` | ||
); | ||
|
||
const entryPointPath = locateLibraryEntryPointFromDirectory( | ||
tree, | ||
componentDirectory, | ||
root, | ||
projectSourceRoot | ||
); | ||
if (!entryPointPath) { | ||
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) { | ||
const modulePath = findModuleFromOptions(tree, schema, root); | ||
if (!shouldExportInEntryPoint(tree, entryPointPath, modulePath)) { | ||
return; | ||
} | ||
} | ||
|
||
const relativePathFromEntryPoint = getRelativeImportToFile( | ||
entryPointPath, | ||
componentFilePath | ||
); | ||
const updateEntryPointContent = stripIndents`${tree.read( | ||
entryPointPath, | ||
'utf-8' | ||
)} | ||
export * from "${relativePathFromEntryPoint}";`; | ||
|
||
tree.write(entryPointPath, updateEntryPointContent); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98014dd
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
nx-dev – ./
nx-dev-nrwl.vercel.app
nx.dev
nx-dev-git-master-nrwl.vercel.app
nx-five.vercel.app