1 | "use strict";
|
---|
2 | /**
|
---|
3 | * @license
|
---|
4 | * Copyright Google LLC All Rights Reserved.
|
---|
5 | *
|
---|
6 | * Use of this source code is governed by an MIT-style license that can be
|
---|
7 | * found in the LICENSE file at https://angular.io/license
|
---|
8 | */
|
---|
9 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
10 | const sass_1 = require("sass");
|
---|
11 | const worker_threads_1 = require("worker_threads");
|
---|
12 | if (!worker_threads_1.parentPort) {
|
---|
13 | throw new Error('Sass worker must be executed as a Worker.');
|
---|
14 | }
|
---|
15 | // The importer variables are used to proxy import requests to the main thread
|
---|
16 | let { workerImporterPort, importerSignal } = (worker_threads_1.workerData || {});
|
---|
17 | worker_threads_1.parentPort.on('message', (message) => {
|
---|
18 | // The init message is only needed to support Node.js < 12.17 and can be removed once support is dropped
|
---|
19 | if (message.init) {
|
---|
20 | workerImporterPort = message.workerImporterPort;
|
---|
21 | importerSignal = message.importerSignal;
|
---|
22 | return;
|
---|
23 | }
|
---|
24 | const { id, hasImporter, options } = message;
|
---|
25 | try {
|
---|
26 | if (hasImporter) {
|
---|
27 | // When a custom importer function is present, the importer request must be proxied
|
---|
28 | // back to the main thread where it can be executed.
|
---|
29 | // This process must be synchronous from the perspective of dart-sass. The `Atomics`
|
---|
30 | // functions combined with the shared memory `importSignal` and the Node.js
|
---|
31 | // `receiveMessageOnPort` function are used to ensure synchronous behavior.
|
---|
32 | options.importer = function (url, prev) {
|
---|
33 | var _a;
|
---|
34 | Atomics.store(importerSignal, 0, 0);
|
---|
35 | // `this.fromImport` was added in dart-sass in 1.33.0, `@types/sass` doesn't include it yet.
|
---|
36 | const { fromImport } = this;
|
---|
37 | workerImporterPort.postMessage({ id, url, prev, fromImport });
|
---|
38 | Atomics.wait(importerSignal, 0, 0);
|
---|
39 | return (_a = worker_threads_1.receiveMessageOnPort(workerImporterPort)) === null || _a === void 0 ? void 0 : _a.message;
|
---|
40 | };
|
---|
41 | }
|
---|
42 | // The synchronous Sass render function can be up to two times faster than the async variant
|
---|
43 | const result = sass_1.renderSync(options);
|
---|
44 | worker_threads_1.parentPort === null || worker_threads_1.parentPort === void 0 ? void 0 : worker_threads_1.parentPort.postMessage({ id, result });
|
---|
45 | }
|
---|
46 | catch (error) {
|
---|
47 | // Needed because V8 will only serialize the message and stack properties of an Error instance.
|
---|
48 | const { formatted, file, line, column, message, stack } = error;
|
---|
49 | worker_threads_1.parentPort === null || worker_threads_1.parentPort === void 0 ? void 0 : worker_threads_1.parentPort.postMessage({ id, error: { formatted, file, line, column, message, stack } });
|
---|
50 | }
|
---|
51 | });
|
---|