Skip to content

Commit 8f6ce9e

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

File tree

19 files changed

+160
-362
lines changed

19 files changed

+160
-362
lines changed

packages/common/.gitignore

Lines changed: 17 additions & 0 deletions
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

Lines changed: 31 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 2 additions & 4 deletions
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

Lines changed: 9 additions & 0 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 8 additions & 4 deletions
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

Lines changed: 10 additions & 7 deletions
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

Lines changed: 7 additions & 5 deletions
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

Lines changed: 2 additions & 2 deletions
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
}

0 commit comments

Comments
 (0)