-
Notifications
You must be signed in to change notification settings - Fork 366
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
Draft
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.
Draft
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
96275cc
Implement `sass --embedded` in pure JS mode
ntkme 85784c4
Run embedded test on both dart and node
ntkme f31245c
Run embedded JS API test on both dart and node compiler
ntkme 496cb02
Update README.md
ntkme 1df6c06
Pass exitCode via message
ntkme 6f4de31
Address review feedbacks
ntkme 3d8b4f0
Rename files and move things around
ntkme fd74031
Add a comment for gracefulShutdown
ntkme 7514f29
Update README.md
ntkme 66437ca
Add diagrams explaining DartVM and NodeJS worker management
ntkme 931af8e
Update README.md
ntkme 3633df1
Use a separate byte to send exitCode
ntkme cad7dfe
Update README.md
ntkme 659e27e
Reflow a comment
ntkme a8e2fa0
Grammar/formatting changes to the embedded README
nex3 b27cbbb
Make the two ReusableWorker interfaces match
nex3 d134ad4
Condense graph in README.md
ntkme a0fc440
Apply review feedbacks
ntkme 4729aca
Apply review feedbacks
ntkme 627e522
Apply review feedbacks
ntkme c976459
Apply review feedbacks
ntkme ff957ec
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
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.
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.