Skip to content

Commit e15aee6

Browse files
committed
build: refactor code to use types from sass and copy-webpack-plugin instead of @types
These packages now ship their own type decleration files.
1 parent 0db333f commit e15aee6

File tree

5 files changed

+29
-21
lines changed

5 files changed

+29
-21
lines changed

package.json

-3
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858
]
5959
},
6060
"resolutions": {
61-
"**/@types/copy-webpack-plugin/webpack": "5.65.0",
6261
"**/@types/webpack-dev-server/webpack": "5.65.0",
6362
"ajv-formats/ajv": "8.8.2"
6463
},
@@ -96,7 +95,6 @@
9695
"@types/babel__core": "7.1.17",
9796
"@types/babel__template": "7.4.1",
9897
"@types/cacache": "^15.0.0",
99-
"@types/copy-webpack-plugin": "^8.0.0",
10098
"@types/debug": "^4.1.2",
10199
"@types/express": "^4.16.0",
102100
"@types/glob": "^7.1.1",
@@ -115,7 +113,6 @@
115113
"@types/postcss-preset-env": "^6.7.1",
116114
"@types/progress": "^2.0.3",
117115
"@types/resolve": "^1.17.1",
118-
"@types/sass": "^1.43.0",
119116
"@types/semver": "^7.0.0",
120117
"@types/text-table": "^0.2.1",
121118
"@types/uuid": "^8.0.0",

packages/angular_devkit/build_angular/BUILD.bazel

-2
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ ts_library(
114114
"@npm//@types/babel__template",
115115
"@npm//@types/browserslist",
116116
"@npm//@types/cacache",
117-
"@npm//@types/copy-webpack-plugin",
118117
"@npm//@types/glob",
119118
"@npm//@types/inquirer",
120119
"@npm//@types/karma",
@@ -123,7 +122,6 @@ ts_library(
123122
"@npm//@types/node",
124123
"@npm//@types/parse5-html-rewriting-stream",
125124
"@npm//@types/postcss-preset-env",
126-
"@npm//@types/sass",
127125
"@npm//@types/semver",
128126
"@npm//@types/text-table",
129127
"@npm//@types/webpack-dev-server",

packages/angular_devkit/build_angular/src/sass/sass-service.ts

+25-12
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,15 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import { Importer, ImporterReturnType, Options, Result, SassException } from 'sass';
9+
import {
10+
LegacyAsyncImporter as AsyncImporter,
11+
LegacyResult as CompileResult,
12+
LegacyException as Exception,
13+
LegacyImporterResult as ImporterResult,
14+
LegacyImporterThis as ImporterThis,
15+
LegacyOptions as Options,
16+
LegacySyncImporter as SyncImporter,
17+
} from 'sass';
1018
import { MessageChannel, Worker } from 'worker_threads';
1119
import { maxWorkers } from '../utils/environment-options';
1220

@@ -18,7 +26,7 @@ const MAX_RENDER_WORKERS = maxWorkers;
1826
/**
1927
* The callback type for the `dart-sass` asynchronous render function.
2028
*/
21-
type RenderCallback = (error?: SassException, result?: Result) => void;
29+
type RenderCallback = (error?: Exception, result?: CompileResult) => void;
2230

2331
/**
2432
* An object containing the contextual information for a specific render request.
@@ -27,16 +35,16 @@ interface RenderRequest {
2735
id: number;
2836
workerIndex: number;
2937
callback: RenderCallback;
30-
importers?: Importer[];
38+
importers?: (SyncImporter | AsyncImporter)[];
3139
}
3240

3341
/**
3442
* A response from the Sass render Worker containing the result of the operation.
3543
*/
3644
interface RenderResponseMessage {
3745
id: number;
38-
error?: SassException;
39-
result?: Result;
46+
error?: Exception;
47+
result?: CompileResult;
4048
}
4149

4250
/**
@@ -73,7 +81,7 @@ export class SassWorkerImplementation {
7381
* @param options The `dart-sass` options to use when rendering the stylesheet.
7482
* @param callback The function to execute when the rendering is complete.
7583
*/
76-
render(options: Options, callback: RenderCallback): void {
84+
render(options: Options<'async'>, callback: RenderCallback): void {
7785
// The `functions`, `logger` and `importer` options are JavaScript functions that cannot be transferred.
7886
// If any additional function options are added in the future, they must be excluded as well.
7987
const { functions, importer, logger, ...serializableOptions } = options;
@@ -142,7 +150,7 @@ export class SassWorkerImplementation {
142150
// The results are expected to be Node.js `Buffer` objects but will each be transferred as
143151
// a Uint8Array that does not have the expected `toString` behavior of a `Buffer`.
144152
const { css, map, stats } = response.result;
145-
const result: Result = {
153+
const result: CompileResult = {
146154
// This `Buffer.from` override will use the memory directly and avoid making a copy
147155
css: Buffer.from(css.buffer, css.byteOffset, css.byteLength),
148156
stats,
@@ -199,16 +207,21 @@ export class SassWorkerImplementation {
199207
}
200208

201209
private async processImporters(
202-
importers: Iterable<Importer>,
210+
importers: Iterable<SyncImporter | AsyncImporter>,
203211
url: string,
204212
prev: string,
205213
fromImport: boolean,
206-
): Promise<ImporterReturnType> {
214+
): Promise<ImporterResult> {
207215
let result = null;
208216
for (const importer of importers) {
209-
result = await new Promise<ImporterReturnType>((resolve) => {
217+
result = await new Promise<ImporterResult>((resolve) => {
210218
// Importers can be both sync and async
211-
const innerResult = importer.call({ fromImport }, url, prev, resolve);
219+
const innerResult = (importer as AsyncImporter).call(
220+
{ fromImport } as ImporterThis,
221+
url,
222+
prev,
223+
resolve,
224+
);
212225
if (innerResult !== undefined) {
213226
resolve(innerResult);
214227
}
@@ -225,7 +238,7 @@ export class SassWorkerImplementation {
225238
private createRequest(
226239
workerIndex: number,
227240
callback: RenderCallback,
228-
importer: Importer | Importer[] | undefined,
241+
importer: SyncImporter | AsyncImporter | (SyncImporter | AsyncImporter)[] | undefined,
229242
): RenderRequest {
230243
return {
231244
id: this.idCounter++,

packages/angular_devkit/build_angular/src/sass/worker.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import { ImporterReturnType, Options, renderSync } from 'sass';
9+
import { ImporterResult, LegacyOptions as Options, renderSync } from 'sass';
1010
import { MessagePort, parentPort, receiveMessageOnPort, workerData } from 'worker_threads';
1111

1212
/**
@@ -21,7 +21,7 @@ interface RenderRequestMessage {
2121
/**
2222
* The Sass options to provide to the `dart-sass` render function.
2323
*/
24-
options: Options;
24+
options: Options<'sync'>;
2525
/**
2626
* Indicates the request has a custom importer function on the main thread.
2727
*/
@@ -52,7 +52,7 @@ parentPort.on('message', ({ id, hasImporter, options }: RenderRequestMessage) =>
5252
workerImporterPort.postMessage({ id, url, prev, fromImport });
5353
Atomics.wait(importerSignal, 0, 0);
5454

55-
return receiveMessageOnPort(workerImporterPort)?.message as ImporterReturnType;
55+
return receiveMessageOnPort(workerImporterPort)?.message as ImporterResult;
5656
};
5757
}
5858

packages/angular_devkit/build_angular/src/webpack/utils/helpers.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import type { ObjectPattern } from 'copy-webpack-plugin';
9+
import type { ObjectPattern } from 'copy-webpack-plugin/types/index'; // Types are not exported properly. Hence the deep-import.
1010
import { createHash } from 'crypto';
1111
import { existsSync } from 'fs';
1212
import glob from 'glob';

0 commit comments

Comments
 (0)