-
Notifications
You must be signed in to change notification settings - Fork 365
Implement sass --embedded
in pure JS mode
#2413
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ntkme
wants to merge
22
commits into
sass:main
Choose a base branch
from
ntkme:embedded-compiler
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
36f0207
Implement `sass --embedded` in pure JS mode
ntkme f5d0b0d
Run embedded test on both dart and node
ntkme 0927ea7
Run embedded JS API test on both dart and node compiler
ntkme 01820bb
Update README.md
ntkme bf27d79
Pass exitCode via message
ntkme 95b3201
Address review feedbacks
ntkme 7f9ab5d
Rename files and move things around
ntkme d91d922
Add a comment for gracefulShutdown
ntkme 4c0fa77
Update README.md
ntkme d9e08e6
Add diagrams explaining DartVM and NodeJS worker management
ntkme dc9b6ff
Update README.md
ntkme 375c1af
Use a separate byte to send exitCode
ntkme b8387fa
Update README.md
ntkme ffa5f31
Reflow a comment
ntkme e50ed4f
Grammar/formatting changes to the embedded README
nex3 214b0b6
Make the two ReusableWorker interfaces match
nex3 4c75653
Condense graph in README.md
ntkme 40c5c96
Apply review feedbacks
ntkme 4b7854a
Apply review feedbacks
ntkme 6f0b86e
Apply review feedbacks
ntkme f3c27f9
Apply review feedbacks
ntkme 3686b63
Apply review feedbacks
ntkme File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,81 @@ | ||
# Embedded Sass Compiler | ||
|
||
This directory contains the Dart Sass embedded compiler. This is a special mode | ||
of the Dart Sass command-line executable, only supported on the Dart VM, in | ||
which it uses stdin and stdout to communicate with another endpoint, the | ||
"embedded host", using a protocol buffer-based protocol. See [the embedded | ||
protocol specification] for details. | ||
of the Dart Sass command-line executable, in which it uses stdin and stdout to | ||
communicate with another endpoint, the "embedded host", using a protocol | ||
buffer-based protocol. See [the embedded protocol specification] for details. | ||
|
||
[the embedded protocol specification]: https://github.com/sass/sass/blob/main/spec/embedded-protocol.md | ||
|
||
The embedded compiler has two different levels of dispatchers for handling | ||
incoming messages from the embedded host: | ||
|
||
1. The [`IsolateDispatcher`] is the first recipient of each packet. It decodes | ||
1. The [`WorkerDispatcher`] is the first recipient of each packet. It decodes | ||
the packets _just enough_ to determine which compilation they belong to, and | ||
forwards them to the appropriate compilation dispatcher. It also parses and | ||
handles messages that aren't compilation specific, such as `VersionRequest`. | ||
|
||
[`IsolateDispatcher`]: isolate_dispatcher.dart | ||
[`WorkerDispatcher`]: worker_dispatcher.dart | ||
|
||
2. The [`CompilationDispatcher`] fully parses and handles messages for a single | ||
compilation. Each `CompilationDispatcher` runs in a separate isolate so that | ||
compilation. Each `CompilationDispatcher` runs in a separate worker so that | ||
the embedded compiler can run multiple compilations in parallel. | ||
|
||
[`CompilationDispatcher`]: compilation_dispatcher.dart | ||
|
||
Otherwise, most of the code in this directory just wraps Dart APIs to | ||
Otherwise, most of the code in this directory just wraps Dart APIs or JS APIs to | ||
communicate with their protocol buffer equivalents. | ||
|
||
## Worker Communication and Management | ||
|
||
The way the Dart VM launches lightweight isolates is very different from how | ||
Node.js launches worker threads. In the Dart VM, the lightweight isolates share | ||
program structures like loaded libraries, classes, functions, and so on, even | ||
including JIT optimized code. This allows main isolate to spawn child isolate | ||
with a reference to the entry point function. | ||
|
||
``` | ||
┌─────────────────┐ ┌─────────────────┐ | ||
│ Main Isolate │ Isolate.spawn(workerEntryPoint, mailbox, sendPort) │ Worker Isolate │ | ||
│ ├───────────────────────────────────────────────────►│ │ | ||
│ │ │ │ | ||
│ ┌─────────────┐ │ Synchronous Messaging │ ┌─────────────┐ │ | ||
│ │ Mailbox ├─┼────────────────────────────────────────────────────┼►│ Mailbox │ │ | ||
│ └─────────────┘ │ │ └─────────────┘ │ | ||
│ │ │ │ | ||
│ ┌─────────────┐ │ Asynchronous Messaging │ ┌─────────────┐ │ | ||
│ │ ReceivePort │◄┼────────────────────────────────────────────────────┼─┤ SendPort │ │ | ||
│ └─────────────┘ │ │ └─────────────┘ │ | ||
│ │ │ │ | ||
└─────────────────┘ └─────────────────┘ | ||
``` | ||
|
||
In Node.JS, the worker threads do not share program structures. In order to | ||
launch a worker thread, it needs an entry point file, with the entry point | ||
function effectly hard-coded in that file. While it's possible to have a | ||
separate entry point file for the worker threads, it would require complex | ||
packaging changes within `cli_pkg`, so instead the main thread and the worker | ||
threads share [the same entry point file](js/executable.dart), which decides | ||
what to run based on `worker_threads.isMainThread`. | ||
|
||
``` | ||
if (worker_threads.isMainThread) { if (worker_threads.isMainThread) { | ||
mainEntryPoint(); mainEntryPoint(); | ||
} else { } else { | ||
workerEntryPoint(); new Worker(process.argv[1], { workerEntryPoint(); | ||
} argv: process.argv.slice(2), } | ||
workerData: channel.port2, | ||
┌────────────────────────────────────┐ transferList: [channel.port2] ┌────────────────────────────────────┐ | ||
│ Main Thread │ }) │ Worker Thread │ | ||
│ ├───────────────────────────────────────────────►│ │ | ||
│ │ │ │ | ||
│ ┌────────────────────────────────┐ │ Synchronous Messaging │ ┌────────────────────────────────┐ │ | ||
│ │ SyncMessagePort(channel.port1) ├─┼────────────────────────────────────────────────┼►│ SyncMessagePort(channel.port2) │ │ | ||
│ └────────────────────────────────┘ │ │ └────────────────────────────────┘ │ | ||
│ │ │ │ | ||
│ ┌────────────────────────────────┐ │ Asynchronous Messaging │ ┌────────────────────────────────┐ │ | ||
│ │ channel.port1 │◄┼────────────────────────────────────────────────┼─┤ channel.port2 │ │ | ||
│ └────────────────────────────────┘ │ │ └────────────────────────────────┘ │ | ||
│ │ │ │ | ||
└────────────────────────────────────┘ └────────────────────────────────────┘ | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ntkme marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,5 @@ | ||
// Copyright 2019 Google Inc. Use of this source code is governed by an | ||
// Copyright 2025 Google Inc. Use of this source code is governed by an | ||
// MIT-style license that can be found in the LICENSE file or at | ||
// https://opensource.org/licenses/MIT. | ||
|
||
import 'dart:io'; | ||
import 'dart:convert'; | ||
|
||
import 'package:stream_channel/stream_channel.dart'; | ||
|
||
import 'isolate_dispatcher.dart'; | ||
import 'util/length_delimited_transformer.dart'; | ||
|
||
void main(List<String> args) { | ||
switch (args) { | ||
case ["--version", ...]: | ||
var response = IsolateDispatcher.versionResponse(); | ||
response.id = 0; | ||
stdout.writeln( | ||
JsonEncoder.withIndent(" ").convert(response.toProto3Json()), | ||
); | ||
return; | ||
|
||
case [_, ...]: | ||
stderr.writeln( | ||
"sass --embedded is not intended to be executed with additional " | ||
"arguments.\n" | ||
"See https://github.com/sass/dart-sass#embedded-dart-sass for " | ||
"details.", | ||
); | ||
// USAGE error from https://bit.ly/2poTt90 | ||
exitCode = 64; | ||
return; | ||
} | ||
|
||
IsolateDispatcher( | ||
StreamChannel.withGuarantees( | ||
stdin, | ||
stdout, | ||
allowSinkErrors: false, | ||
).transform(lengthDelimited), | ||
).listen(); | ||
} | ||
export 'vm/executable.dart' if (dart.library.js) 'js/executable.dart'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// Copyright 2025 Google Inc. Use of this source code is governed by an | ||
// MIT-style license that can be found in the LICENSE file or at | ||
// https://opensource.org/licenses/MIT. | ||
|
||
import 'dart:js_interop'; | ||
|
||
@JS('os.cpus') | ||
external JSArray _cpus(); | ||
|
||
int get concurrencyLimit => _cpus().length; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright 2025 Google Inc. Use of this source code is governed by an | ||
// MIT-style license that can be found in the LICENSE file or at | ||
// https://opensource.org/licenses/MIT. | ||
|
||
import 'package:stream_channel/stream_channel.dart'; | ||
|
||
import '../compilation_dispatcher.dart'; | ||
import '../options.dart'; | ||
import '../util/length_delimited_transformer.dart'; | ||
import '../worker_dispatcher.dart'; | ||
import 'io.dart'; | ||
import 'sync_receive_port.dart'; | ||
import 'worker_threads.dart'; | ||
|
||
void main(List<String> args) { | ||
if (isMainThread) { | ||
if (parseOptions(args)) { | ||
WorkerDispatcher(StreamChannel.withGuarantees(stdin, stdout, | ||
allowSinkErrors: false) | ||
.transform(lengthDelimited)) | ||
.listen(); | ||
} | ||
} else { | ||
var port = workerData! as MessagePort; | ||
CompilationDispatcher(JSSyncReceivePort(port), JSSendPort(port)).listen(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.