Skip to content

Commit 8f6ce9e

Browse files
authored
refactor(common): move loadModule in shared package (#1654)
1 parent 6399221 commit 8f6ce9e

19 files changed

+160
-362
lines changed

packages/common/.gitignore

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
*.log
2+
.idea
3+
.DS_Store
4+
node_modules
5+
6+
.js
7+
typings
8+
9+
## this is generated by `npm pack`
10+
*.tgz
11+
package
12+
13+
dist
14+
**/schema.json
15+
*.js
16+
!karma.conf.js
17+
*.js.map

packages/common/package.json

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "@angular-builders/common",
3+
"version": "1.0.0",
4+
"description": "Common utility functions shared between @angular-builders packages",
5+
"main": "dist/index.js",
6+
"files": [
7+
"dist"
8+
],
9+
"publishConfig": {
10+
"access": "public"
11+
},
12+
"author": "JeB Barabanov",
13+
"license": "MIT",
14+
"engines": {
15+
"node": "^14.20.0 || ^16.13.0 || >=18.10.0"
16+
},
17+
"scripts": {
18+
"prebuild": "yarn clean",
19+
"build": "yarn prebuild && tsc",
20+
"clean": "rimraf dist"
21+
},
22+
"dependencies": {
23+
"@angular-devkit/core": "^17.1.0",
24+
"ts-node": "^10.0.0",
25+
"tsconfig-paths": "^4.1.0"
26+
},
27+
"devDependencies": {
28+
"rimraf": "^5.0.0",
29+
"typescript": "5.3.3"
30+
}
31+
}

packages/common/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './load-module';

packages/custom-esbuild/src/utils.ts renamed to packages/common/src/load-module.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { Path, getSystemPath, logging } from '@angular-devkit/core';
21
import * as path from 'node:path';
32
import * as url from 'node:url';
3+
import type { logging } from '@angular-devkit/core';
44

55
const _tsNodeRegister = (() => {
66
let lastTsConfig: string | undefined;
@@ -73,12 +73,10 @@ function loadEsmModule<T>(modulePath: string | URL): Promise<T> {
7373
* Loads CJS and ESM modules based on extension
7474
*/
7575
export async function loadModule<T>(
76-
workspaceRoot: Path,
77-
relativePath: string,
76+
modulePath: string,
7877
tsConfig: string,
7978
logger: logging.LoggerApi
8079
): Promise<T> {
81-
const modulePath = path.join(getSystemPath(workspaceRoot), relativePath);
8280
tsNodeRegister(modulePath, tsConfig, logger);
8381

8482
switch (path.extname(modulePath)) {

packages/common/tsconfig.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extends": "../../tsconfig.json",
3+
"compilerOptions": {
4+
"outDir": "dist"
5+
},
6+
"files": [
7+
"src/index.ts"
8+
]
9+
}

packages/custom-esbuild/package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,10 @@
3939
},
4040
"builders": "builders.json",
4141
"dependencies": {
42+
"@angular-builders/common": "workspace:*",
4243
"@angular-devkit/architect": ">=0.1701.0 < 0.1800.0",
4344
"@angular-devkit/build-angular": "^17.1.0",
44-
"@angular-devkit/core": "^17.1.0",
45-
"ts-node": "^10.0.0",
46-
"tsconfig-paths": "^4.1.0"
45+
"@angular-devkit/core": "^17.1.0"
4746
},
4847
"peerDependencies": {
4948
"@angular/compiler-cli": "^17.1.0"
@@ -52,6 +51,7 @@
5251
"esbuild": "0.20.0",
5352
"jest": "29.7.0",
5453
"rimraf": "^5.0.0",
54+
"ts-node": "^10.0.0",
5555
"typescript": "5.3.3"
5656
}
5757
}

packages/custom-esbuild/src/application/index.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,27 @@ import { buildApplication } from '@angular-devkit/build-angular';
44
import { getSystemPath, json, normalize } from '@angular-devkit/core';
55
import { ApplicationBuilderExtensions } from '@angular-devkit/build-angular/src/builders/application/options';
66
import { defer, switchMap } from 'rxjs';
7+
import { loadModule } from '@angular-builders/common';
78

8-
import { loadModule } from '../utils';
99
import { loadPlugins } from '../load-plugins';
1010
import { CustomEsbuildApplicationSchema } from '../custom-esbuild-schema';
1111

1212
export function buildCustomEsbuildApplication(
1313
options: CustomEsbuildApplicationSchema,
1414
context: BuilderContext
1515
) {
16-
const workspaceRoot = normalize(context.workspaceRoot);
17-
const tsConfig = path.join(getSystemPath(workspaceRoot), options.tsConfig);
16+
const workspaceRoot = getSystemPath(normalize(context.workspaceRoot));
17+
const tsConfig = path.join(workspaceRoot, options.tsConfig);
1818

1919
return defer(async () => {
2020
const codePlugins = await loadPlugins(options.plugins, workspaceRoot, tsConfig, context.logger);
2121

2222
const indexHtmlTransformer = options.indexHtmlTransformer
23-
? await loadModule(workspaceRoot, options.indexHtmlTransformer, tsConfig, context.logger)
23+
? await loadModule(
24+
path.join(workspaceRoot, options.indexHtmlTransformer),
25+
tsConfig,
26+
context.logger
27+
)
2428
: undefined;
2529

2630
return { codePlugins, indexHtmlTransformer } as ApplicationBuilderExtensions;

packages/custom-esbuild/src/dev-server/index.ts

+10-7
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import { IndexHtmlTransform } from '@angular-devkit/build-angular/src/utils/inde
99
import { getSystemPath, json, normalize } from '@angular-devkit/core';
1010
import { Observable, from, switchMap } from 'rxjs';
1111
import type { Connect } from 'vite';
12+
import { loadModule } from '@angular-builders/common';
1213

13-
import { loadModule } from '../utils';
1414
import { loadPlugins } from '../load-plugins';
1515
import { patchBuilderContext } from './patch-builder-context';
1616
import {
@@ -33,16 +33,20 @@ export function executeCustomDevServerBuilder(
3333
)) as unknown as CustomEsbuildApplicationSchema;
3434
}
3535

36-
const workspaceRoot = normalize(context.workspaceRoot);
36+
const workspaceRoot = getSystemPath(normalize(context.workspaceRoot));
3737

3838
return from(getBuildTargetOptions()).pipe(
3939
switchMap(async buildOptions => {
40-
const tsConfig = path.join(getSystemPath(workspaceRoot), buildOptions.tsConfig);
40+
const tsConfig = path.join(workspaceRoot, buildOptions.tsConfig);
4141

4242
const middleware = await Promise.all(
43-
(options.middlewares || []).map(path =>
43+
(options.middlewares || []).map(middlewarePath =>
4444
// https://github.com/angular/angular-cli/pull/26212/files#diff-a99020cbdb97d20b2bc686bcb64b31942107d56db06fd880171b0a86f7859e6eR52
45-
loadModule<Connect.NextHandleFunction>(workspaceRoot, path, tsConfig, context.logger)
45+
loadModule<Connect.NextHandleFunction>(
46+
path.join(workspaceRoot, middlewarePath),
47+
tsConfig,
48+
context.logger
49+
)
4650
)
4751
);
4852

@@ -55,8 +59,7 @@ export function executeCustomDevServerBuilder(
5559

5660
const indexHtmlTransformer: IndexHtmlTransform = buildOptions.indexHtmlTransformer
5761
? await loadModule(
58-
workspaceRoot,
59-
buildOptions.indexHtmlTransformer,
62+
path.join(workspaceRoot, buildOptions.indexHtmlTransformer),
6063
tsConfig,
6164
context.logger
6265
)

packages/custom-esbuild/src/load-plugins.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1+
import * as path from 'node:path';
12
import type { Plugin } from 'esbuild';
2-
import type { Path, logging } from '@angular-devkit/core';
3-
4-
import { loadModule } from './utils';
3+
import type { logging } from '@angular-devkit/core';
4+
import { loadModule } from '@angular-builders/common';
55

66
export async function loadPlugins(
77
paths: string[] | undefined,
8-
workspaceRoot: Path,
8+
workspaceRoot: string,
99
tsConfig: string,
1010
logger: logging.LoggerApi
1111
): Promise<Plugin[]> {
1212
const plugins = await Promise.all(
13-
(paths || []).map(path => loadModule<Plugin | Plugin[]>(workspaceRoot, path, tsConfig, logger))
13+
(paths || []).map(pluginPath =>
14+
loadModule<Plugin | Plugin[]>(path.join(workspaceRoot, pluginPath), tsConfig, logger)
15+
)
1416
);
1517

1618
return plugins.flat();

packages/custom-webpack/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,11 @@
4040
},
4141
"builders": "builders.json",
4242
"dependencies": {
43+
"@angular-builders/common": "workspace:*",
4344
"@angular-devkit/architect": ">=0.1700.0 < 0.1800.0",
4445
"@angular-devkit/build-angular": "^17.0.0",
4546
"@angular-devkit/core": "^17.0.0",
4647
"lodash": "^4.17.15",
47-
"ts-node": "^10.0.0",
48-
"tsconfig-paths": "^4.1.0",
4948
"webpack-merge": "^5.7.3"
5049
},
5150
"peerDependencies": {
@@ -54,6 +53,7 @@
5453
"devDependencies": {
5554
"jest": "29.7.0",
5655
"rimraf": "^5.0.0",
56+
"ts-node": "^10.0.0",
5757
"typescript": "5.3.3"
5858
}
5959
}

packages/custom-webpack/src/custom-webpack-builder.spec.ts

+14-15
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ const baseWebpackConfig = {
1111
entry: './main.ts',
1212
};
1313

14-
const buildOptions = {
15-
env: 'prod',
16-
};
17-
1814
const targetOptions: TargetOptions = {
1915
project: 'application',
2016
configuration: 'production',
@@ -56,6 +52,8 @@ const customWebpackFunctionObj = {
5652
},
5753
};
5854

55+
const tsConfig = './tsconfig.app.json';
56+
5957
function createConfigFile<T>(fileName: string, content: T) {
6058
jest.mock(`${__dirname}/${fileName}`, () => content, { virtual: true });
6159
}
@@ -70,7 +68,7 @@ describe('CustomWebpackBuilder', () => {
7068
__dirname as Path,
7169
null,
7270
baseWebpackConfig,
73-
{},
71+
{ tsConfig },
7472
targetOptions,
7573
{} as any
7674
);
@@ -85,7 +83,7 @@ describe('CustomWebpackBuilder', () => {
8583
__dirname as Path,
8684
{},
8785
baseWebpackConfig,
88-
{},
86+
{ tsConfig },
8987
targetOptions,
9088
{} as any
9189
);
@@ -111,7 +109,7 @@ describe('CustomWebpackBuilder', () => {
111109
__dirname as Path,
112110
{ path: fileName },
113111
baseWebpackConfig,
114-
{},
112+
{ tsConfig },
115113
targetOptions,
116114
{} as any
117115
);
@@ -137,7 +135,7 @@ describe('CustomWebpackBuilder', () => {
137135
__dirname as Path,
138136
{ mergeRules },
139137
baseWebpackConfig,
140-
{},
138+
{ tsConfig },
141139
targetOptions,
142140
{} as any
143141
);
@@ -162,7 +160,7 @@ describe('CustomWebpackBuilder', () => {
162160
__dirname as Path,
163161
{ replaceDuplicatePlugins: true },
164162
baseWebpackConfig,
165-
{},
163+
{ tsConfig },
166164
targetOptions,
167165
{} as any
168166
);
@@ -175,6 +173,7 @@ describe('CustomWebpackBuilder', () => {
175173
});
176174

177175
it('should pass build options to the webpack config function', async () => {
176+
const buildOptions = { tsConfig, env: 'prod' };
178177
const spy = jest.fn((config, options, targetOptions) => config);
179178
createConfigFile(defaultWebpackConfigPath, spy);
180179
await CustomWebpackBuilder.buildWebpackConfig(
@@ -195,7 +194,7 @@ describe('CustomWebpackBuilder', () => {
195194
__dirname as Path,
196195
{},
197196
baseWebpackConfig,
198-
{},
197+
{ tsConfig },
199198
targetOptions,
200199
{} as any
201200
);
@@ -220,7 +219,7 @@ describe('CustomWebpackBuilder', () => {
220219
__dirname as Path,
221220
{},
222221
baseWebpackConfig,
223-
{},
222+
{ tsConfig },
224223
targetOptions,
225224
{} as any
226225
);
@@ -250,7 +249,7 @@ describe('CustomWebpackBuilder', () => {
250249
__dirname as Path,
251250
{},
252251
baseWebpackConfig,
253-
{},
252+
{ tsConfig },
254253
targetOptions,
255254
{} as any
256255
);
@@ -288,7 +287,7 @@ describe('CustomWebpackBuilder', () => {
288287
},
289288
},
290289
baseWebpackConfig,
291-
{},
290+
{ tsConfig },
292291
targetOptions,
293292
logger
294293
);
@@ -313,7 +312,7 @@ describe('CustomWebpackBuilder', () => {
313312
},
314313
},
315314
baseWebpackConfig,
316-
{},
315+
{ tsConfig },
317316
targetOptions,
318317
logger
319318
);
@@ -346,7 +345,7 @@ describe('CustomWebpackBuilder', () => {
346345
},
347346
},
348347
baseWebpackConfig,
349-
{},
348+
{ tsConfig },
350349
targetOptions,
351350
logger
352351
);

0 commit comments

Comments
 (0)