Skip to content

Commit

Permalink
feat(@angular-devkit/build-angular): switch to use Sass modern API
Browse files Browse the repository at this point in the history
Sass modern API provides faster compilations times when used in an async manner.

Users can temporary opt-out from using the modern API by setting `NG_BUILD_LEGACY_SASS` to `true` or `1`.

Application compilation duration | Sass API and Compiler
-- | --
60852ms | dart-sass legacy sync API
52666ms | dart-sass modern API

Note: https://github.com/johannesjo/super-productivity was used for benchmarking.

Prior art: http://docs/document/d/1CvEceWMpBoEBd8SfvksGMdVHxaZMH93b0EGS3XbR3_Q?resourcekey=0-vFm-xMspT65FZLIyX7xWFQ
  • Loading branch information
alan-agius4 committed Aug 4, 2022
1 parent 88c3b71 commit c748753
Show file tree
Hide file tree
Showing 8 changed files with 646 additions and 174 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,54 +6,74 @@
* found in the LICENSE file at https://angular.io/license
*/

import type { Plugin, PluginBuild } from 'esbuild';
import type { LegacyResult } from 'sass';
import { SassWorkerImplementation } from '../../sass/sass-service';
import type { PartialMessage, Plugin, PluginBuild } from 'esbuild';
import type { CompileResult } from 'sass';
import { fileURLToPath } from 'url';

export function createSassPlugin(options: { sourcemap: boolean; includePaths?: string[] }): Plugin {
export function createSassPlugin(options: { sourcemap: boolean; loadPaths?: string[] }): Plugin {
return {
name: 'angular-sass',
setup(build: PluginBuild): void {
let sass: SassWorkerImplementation;
let sass: typeof import('sass');

build.onStart(() => {
sass = new SassWorkerImplementation();
build.onStart(async () => {
// Lazily load Sass
sass = await import('sass');
});

build.onEnd(() => {
sass?.close();
});

build.onLoad({ filter: /\.s[ac]ss$/ }, async (args) => {
const result = await new Promise<LegacyResult>((resolve, reject) => {
sass.render(
{
file: args.path,
includePaths: options.includePaths,
indentedSyntax: args.path.endsWith('.sass'),
outputStyle: 'expanded',
sourceMap: options.sourcemap,
sourceMapContents: options.sourcemap,
sourceMapEmbed: options.sourcemap,
quietDeps: true,
},
(error, result) => {
if (error) {
reject(error);
}
if (result) {
resolve(result);
}
build.onLoad({ filter: /\.s[ac]ss$/ }, (args) => {
try {
const warnings: PartialMessage[] = [];
// Use sync version as async version is slower.
const { css, sourceMap, loadedUrls } = sass.compile(args.path, {
style: 'expanded',
loadPaths: options.loadPaths,
sourceMap: options.sourcemap,
sourceMapIncludeSources: options.sourcemap,
quietDeps: true,
logger: {
warn: (text, _options) => {
warnings.push({
text,
});
},
},
);
});

return {
contents: result.css,
loader: 'css',
watchFiles: result.stats.includedFiles,
};
});

return {
loader: 'css',
contents: css + sourceMapToUrlComment(sourceMap),
watchFiles: loadedUrls.map((url) => fileURLToPath(url)),
warnings,
};
} catch (error) {
if (error instanceof sass.Exception) {
const file = error.span.url ? fileURLToPath(error.span.url) : undefined;

return {
loader: 'css',
errors: [
{
text: error.toString(),
},
],
watchFiles: file ? [file] : undefined,
};
}

throw error;
}
});
},
};
}

function sourceMapToUrlComment(sourceMap: CompileResult['sourceMap']): string {
if (!sourceMap) {
return '';
}

const urlSourceMap = Buffer.from(JSON.stringify(sourceMap), 'utf-8').toString('base64');

return `//# sourceMappingURL=data:application/json;charset=utf-8;base64,${urlSourceMap}`;
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ async function bundleStylesheet(
entry: Required<Pick<BuildOptions, 'stdin'> | Pick<BuildOptions, 'entryPoints'>>,
options: BundleStylesheetOptions,
) {
const loadPaths = options.includePaths ?? [];
// Needed to resolve node packages.
loadPaths.push(path.join(options.workspaceRoot, 'node_modules'));

// Execute esbuild
const result = await bundle({
...entry,
Expand All @@ -40,9 +44,7 @@ async function bundleStylesheet(
preserveSymlinks: options.preserveSymlinks,
conditions: ['style', 'sass'],
mainFields: ['style', 'sass'],
plugins: [
createSassPlugin({ sourcemap: !!options.sourcemap, includePaths: options.includePaths }),
],
plugins: [createSassPlugin({ sourcemap: !!options.sourcemap, loadPaths })],
});

// Extract the result of the bundling from the output files
Expand Down
250 changes: 250 additions & 0 deletions packages/angular_devkit/build_angular/src/sass/legacy/sass-service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,250 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

import {
LegacyAsyncImporter as AsyncImporter,
LegacyResult as CompileResult,
LegacyException as Exception,
LegacyImporterResult as ImporterResult,
LegacyImporterThis as ImporterThis,
LegacyOptions as Options,
LegacySyncImporter as SyncImporter,
} from 'sass';
import { MessageChannel, Worker } from 'worker_threads';
import { maxWorkers } from '../../utils/environment-options';

/**
* The maximum number of Workers that will be created to execute render requests.
*/
const MAX_RENDER_WORKERS = maxWorkers;

/**
* The callback type for the `dart-sass` asynchronous render function.
*/
type RenderCallback = (error?: Exception, result?: CompileResult) => void;

/**
* An object containing the contextual information for a specific render request.
*/
interface RenderRequest {
id: number;
workerIndex: number;
callback: RenderCallback;
importers?: (SyncImporter | AsyncImporter)[];
}

/**
* A response from the Sass render Worker containing the result of the operation.
*/
interface RenderResponseMessage {
id: number;
error?: Exception;
result?: CompileResult;
}

/**
* A Sass renderer implementation that provides an interface that can be used by Webpack's
* `sass-loader`. The implementation uses a Worker thread to perform the Sass rendering
* with the `dart-sass` package. The `dart-sass` synchronous render function is used within
* the worker which can be up to two times faster than the asynchronous variant.
*/
export class SassWorkerImplementationLegacy {
private readonly workers: Worker[] = [];
private readonly availableWorkers: number[] = [];
private readonly requests = new Map<number, RenderRequest>();
private idCounter = 1;
private nextWorkerIndex = 0;

/**
* Provides information about the Sass implementation.
* This mimics enough of the `dart-sass` value to be used with the `sass-loader`.
*/
get info(): string {
return 'dart-sass\tworker';
}

/**
* The synchronous render function is not used by the `sass-loader`.
*/
renderSync(): never {
throw new Error('Sass renderSync is not supported.');
}

/**
* Asynchronously request a Sass stylesheet to be renderered.
*
* @param options The `dart-sass` options to use when rendering the stylesheet.
* @param callback The function to execute when the rendering is complete.
*/
render(options: Options<'async'>, callback: RenderCallback): void {
// The `functions`, `logger` and `importer` options are JavaScript functions that cannot be transferred.
// If any additional function options are added in the future, they must be excluded as well.
const { functions, importer, logger, ...serializableOptions } = options;

// The CLI's configuration does not use or expose the ability to defined custom Sass functions
if (functions && Object.keys(functions).length > 0) {
throw new Error('Sass custom functions are not supported.');
}

let workerIndex = this.availableWorkers.pop();
if (workerIndex === undefined) {
if (this.workers.length < MAX_RENDER_WORKERS) {
workerIndex = this.workers.length;
this.workers.push(this.createWorker());
} else {
workerIndex = this.nextWorkerIndex++;
if (this.nextWorkerIndex >= this.workers.length) {
this.nextWorkerIndex = 0;
}
}
}

const request = this.createRequest(workerIndex, callback, importer);
this.requests.set(request.id, request);

this.workers[workerIndex].postMessage({
id: request.id,
hasImporter: !!importer,
options: serializableOptions,
});
}

/**
* Shutdown the Sass render worker.
* Executing this method will stop any pending render requests.
*/
close(): void {
for (const worker of this.workers) {
try {
void worker.terminate();
} catch {}
}
this.requests.clear();
}

private createWorker(): Worker {
const { port1: mainImporterPort, port2: workerImporterPort } = new MessageChannel();
const importerSignal = new Int32Array(new SharedArrayBuffer(4));

const workerPath = require.resolve('./worker');
const worker = new Worker(workerPath, {
workerData: { workerImporterPort, importerSignal },
transferList: [workerImporterPort],
});

worker.on('message', (response: RenderResponseMessage) => {
const request = this.requests.get(response.id);
if (!request) {
return;
}

this.requests.delete(response.id);
this.availableWorkers.push(request.workerIndex);

if (response.result) {
// The results are expected to be Node.js `Buffer` objects but will each be transferred as
// a Uint8Array that does not have the expected `toString` behavior of a `Buffer`.
const { css, map, stats } = response.result;
const result: CompileResult = {
// This `Buffer.from` override will use the memory directly and avoid making a copy
css: Buffer.from(css.buffer, css.byteOffset, css.byteLength),
stats,
};
if (map) {
// This `Buffer.from` override will use the memory directly and avoid making a copy
result.map = Buffer.from(map.buffer, map.byteOffset, map.byteLength);
}
request.callback(undefined, result);
} else {
request.callback(response.error);
}
});

mainImporterPort.on(
'message',
({
id,
url,
prev,
fromImport,
}: {
id: number;
url: string;
prev: string;
fromImport: boolean;
}) => {
const request = this.requests.get(id);
if (!request?.importers) {
mainImporterPort.postMessage(null);
Atomics.store(importerSignal, 0, 1);
Atomics.notify(importerSignal, 0);

return;
}

this.processImporters(request.importers, url, prev, fromImport)
.then((result) => {
mainImporterPort.postMessage(result);
})
.catch((error) => {
mainImporterPort.postMessage(error);
})
.finally(() => {
Atomics.store(importerSignal, 0, 1);
Atomics.notify(importerSignal, 0);
});
},
);

mainImporterPort.unref();

return worker;
}

private async processImporters(
importers: Iterable<SyncImporter | AsyncImporter>,
url: string,
prev: string,
fromImport: boolean,
): Promise<ImporterResult> {
let result = null;
for (const importer of importers) {
result = await new Promise<ImporterResult>((resolve) => {
// Importers can be both sync and async
const innerResult = (importer as AsyncImporter).call(
{ fromImport } as ImporterThis,
url,
prev,
resolve,
);
if (innerResult !== undefined) {
resolve(innerResult);
}
});

if (result) {
break;
}
}

return result;
}

private createRequest(
workerIndex: number,
callback: RenderCallback,
importer: SyncImporter | AsyncImporter | (SyncImporter | AsyncImporter)[] | undefined,
): RenderRequest {
return {
id: this.idCounter++,
workerIndex,
callback,
importers: !importer || Array.isArray(importer) ? importer : [importer],
};
}
}
Loading

0 comments on commit c748753

Please sign in to comment.