Skip to content

Commit

Permalink
feat(components/packages): configure commonjs dependencies (#2280)
Browse files Browse the repository at this point in the history
* feat(components/packages): configure commonjs dependencies

* Add tests
  • Loading branch information
johnhwhite authored May 6, 2024
1 parent bf75976 commit 1fd31ff
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
import fs from 'fs-extra';
import path from 'path';

import { addCommonJsConfig } from '../rules/add-commonjs-config';
import { addPolyfillsConfig } from '../rules/add-polyfills-config';
import { applySkyuxStylesheetsToWorkspace } from '../rules/apply-skyux-stylesheets-to-workspace';
import { installAngularCdk } from '../rules/install-angular-cdk';
Expand Down Expand Up @@ -50,6 +51,7 @@ export default function ngAdd(options: Schema): Rule {
return chain([
installEssentialSkyUxPackages(skyuxVersion),
installAngularCdk(),
addCommonJsConfig(projectName),
addPolyfillsConfig(projectName, ['build', 'test']),
applySkyuxStylesheetsToWorkspace(projectName),
modifyTsConfig(),
Expand Down
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',
]);
});
});
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));
}
});
}

0 comments on commit 1fd31ff

Please sign in to comment.