-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(components/packages): configure commonjs dependencies (#2280)
* feat(components/packages): configure commonjs dependencies * Add tests
- Loading branch information
1 parent
bf75976
commit 1fd31ff
Showing
3 changed files
with
107 additions
and
0 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
50 changes: 50 additions & 0 deletions
50
libs/components/packages/src/schematics/rules/add-commonjs-config.spec.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,50 @@ | ||
import { SchematicTestRunner } from '@angular-devkit/schematics/testing'; | ||
import { getWorkspace } from '@schematics/angular/utility/workspace'; | ||
|
||
import { firstValueFrom } from 'rxjs'; | ||
|
||
import { createTestLibrary } from '../testing/scaffold'; | ||
|
||
import { addCommonJsConfig } from './add-commonjs-config'; | ||
|
||
describe('add-commonjs-config', () => { | ||
const runner = new SchematicTestRunner( | ||
'schematics', | ||
require.resolve('../../../collection.json'), | ||
); | ||
|
||
it('should not update library config', async () => { | ||
const tree = await createTestLibrary(runner, { | ||
projectName: 'my-lib', | ||
}); | ||
await firstValueFrom(runner.callRule(addCommonJsConfig('my-lib'), tree)); | ||
const workspace = await getWorkspace(tree); | ||
expect( | ||
workspace.projects.get('my-lib')?.targets.get('build')?.options?.[ | ||
'allowedCommonJsDependencies' | ||
], | ||
).toBeUndefined(); | ||
}); | ||
|
||
it('should update app config', async () => { | ||
const tree = await createTestLibrary(runner, { | ||
projectName: 'my-lib', | ||
}); | ||
await firstValueFrom( | ||
runner.callRule(addCommonJsConfig('my-lib-showcase'), tree), | ||
); | ||
const workspace = await getWorkspace(tree); | ||
expect( | ||
workspace.projects.get('my-lib-showcase')?.targets.get('build') | ||
?.options?.['allowedCommonJsDependencies'], | ||
).toEqual([ | ||
'@skyux/icons', | ||
'autonumeric', | ||
'dom-autoscroller', | ||
'dragula', | ||
'fontfaceobserver', | ||
'intl-tel-input', | ||
'moment', | ||
]); | ||
}); | ||
}); |
55 changes: 55 additions & 0 deletions
55
libs/components/packages/src/schematics/rules/add-commonjs-config.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,55 @@ | ||
import { Rule } from '@angular-devkit/schematics'; | ||
|
||
import { updateWorkspace } from '../utility/workspace'; | ||
|
||
// List of builders that support allowedCommonJsDependencies. | ||
const allowedCommonJsDependenciesBuilders = [ | ||
'@angular-devkit/build-angular:application', | ||
'@angular-devkit/build-angular:browser', | ||
'@blackbaud-internal/skyux-angular-builders:browser', | ||
]; | ||
|
||
/** | ||
* Adds allowedCommonJsDependencies to the given targets' configuration. | ||
*/ | ||
export function addCommonJsConfig(projectName: string): Rule { | ||
const targetNames = ['build']; | ||
return () => | ||
updateWorkspace((workspace) => { | ||
const project = workspace.projects.get(projectName); | ||
|
||
/* istanbul ignore next */ | ||
if (!project) { | ||
return; | ||
} | ||
|
||
for (const targetName of targetNames) { | ||
const target = project.targets.get(targetName); | ||
|
||
/* istanbul ignore next */ | ||
if (!target) { | ||
return; | ||
} | ||
|
||
// Abort if builder is not approved. | ||
if (!allowedCommonJsDependenciesBuilders.includes(target.builder)) { | ||
continue; | ||
} | ||
|
||
target.options ??= {}; | ||
target.options['allowedCommonJsDependencies'] ??= []; | ||
target.options['allowedCommonJsDependencies'] = [ | ||
...new Set([ | ||
...(target.options['allowedCommonJsDependencies'] as string[]), | ||
'@skyux/icons', | ||
'autonumeric', | ||
'dom-autoscroller', | ||
'dragula', | ||
'fontfaceobserver', | ||
'intl-tel-input', | ||
'moment', | ||
]), | ||
].sort((a, b) => a.localeCompare(b)); | ||
} | ||
}); | ||
} |