Skip to content

Commit 39d3726

Browse files
ntkmenex3
andauthored
Lazy load @parcel/watcher and fallback to chokidar (#2414)
Co-authored-by: Natalie Weizenbaum <[email protected]>
1 parent a9254df commit 39d3726

File tree

13 files changed

+75
-64
lines changed

13 files changed

+75
-64
lines changed

CHANGELOG.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
## 1.80.6-dev
1+
## 1.80.6
22

3-
* No user-visible changes.
3+
### Command-Line Interface
4+
5+
* Make `@parcel/watcher` an optional dependency so this can still be installed
6+
on operating systems where it's unavailable.
47

58
## 1.80.5
69

lib/src/io/js.dart

+25-25
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,31 @@ Future<Stream<WatchEvent>> watchDir(String path, {bool poll = false}) async {
257257
// Don't assign the controller until after the ready event fires. Otherwise,
258258
// Chokidar will give us a bunch of add events for files that already exist.
259259
StreamController<WatchEvent>? controller;
260-
if (poll) {
261-
var watcher = chokidar.watch(path, ChokidarOptions(usePolling: true));
260+
if (parcelWatcher case var parcel? when !poll) {
261+
var subscription = await parcel.subscribe(path,
262+
(Object? error, List<ParcelWatcherEvent> events) {
263+
if (error != null) {
264+
controller?.addError(error);
265+
} else {
266+
for (var event in events) {
267+
switch (event.type) {
268+
case 'create':
269+
controller?.add(WatchEvent(ChangeType.ADD, event.path));
270+
case 'update':
271+
controller?.add(WatchEvent(ChangeType.MODIFY, event.path));
272+
case 'delete':
273+
controller?.add(WatchEvent(ChangeType.REMOVE, event.path));
274+
}
275+
}
276+
}
277+
});
278+
279+
return (controller = StreamController<WatchEvent>(onCancel: () {
280+
subscription.unsubscribe();
281+
}))
282+
.stream;
283+
} else {
284+
var watcher = chokidar.watch(path, ChokidarOptions(usePolling: poll));
262285
watcher
263286
..on(
264287
'add',
@@ -286,28 +309,5 @@ Future<Stream<WatchEvent>> watchDir(String path, {bool poll = false}) async {
286309
}));
287310

288311
return completer.future;
289-
} else {
290-
var subscription = await ParcelWatcher.subscribeFuture(path,
291-
(Object? error, List<ParcelWatcherEvent> events) {
292-
if (error != null) {
293-
controller?.addError(error);
294-
} else {
295-
for (var event in events) {
296-
switch (event.type) {
297-
case 'create':
298-
controller?.add(WatchEvent(ChangeType.ADD, event.path));
299-
case 'update':
300-
controller?.add(WatchEvent(ChangeType.MODIFY, event.path));
301-
case 'delete':
302-
controller?.add(WatchEvent(ChangeType.REMOVE, event.path));
303-
}
304-
}
305-
}
306-
});
307-
308-
return (controller = StreamController<WatchEvent>(onCancel: () {
309-
subscription.unsubscribe();
310-
}))
311-
.stream;
312312
}
313313
}

lib/src/js/parcel_watcher.dart

+28-23
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,48 @@
22
// MIT-style license that can be found in the LICENSE file or at
33
// https://opensource.org/licenses/MIT.
44

5-
import 'package:js/js.dart';
6-
import 'package:node_interop/js.dart';
7-
import 'package:node_interop/util.dart';
5+
import 'dart:js_interop';
86

97
@JS()
10-
class ParcelWatcherSubscription {
8+
extension type ParcelWatcherSubscription(JSObject _) implements JSObject {
119
external void unsubscribe();
1210
}
1311

1412
@JS()
15-
class ParcelWatcherEvent {
13+
extension type ParcelWatcherEvent(JSObject _) implements JSObject {
1614
external String get type;
1715
external String get path;
1816
}
1917

2018
/// The @parcel/watcher module.
2119
///
2220
/// See [the docs on npm](https://www.npmjs.com/package/@parcel/watcher).
23-
@JS('parcel_watcher')
24-
class ParcelWatcher {
25-
external static Promise subscribe(String path, Function callback);
26-
static Future<ParcelWatcherSubscription> subscribeFuture(String path,
21+
@JS()
22+
extension type ParcelWatcher(JSObject _) implements JSObject {
23+
@JS('subscribe')
24+
external JSPromise<ParcelWatcherSubscription> _subscribe(
25+
String path, JSFunction callback);
26+
Future<ParcelWatcherSubscription> subscribe(String path,
2727
void Function(Object? error, List<ParcelWatcherEvent>) callback) =>
28-
promiseToFuture(
29-
subscribe(path, allowInterop((Object? error, List<dynamic> events) {
30-
callback(error, events.cast<ParcelWatcherEvent>());
31-
})));
28+
_subscribe(
29+
path,
30+
(JSObject? error, JSArray<ParcelWatcherEvent> events) {
31+
callback(error, events.toDart);
32+
}.toJS)
33+
.toDart;
3234

33-
external static Promise getEventsSince(String path, String snapshotPath);
34-
static Future<List<ParcelWatcherEvent>> getEventsSinceFuture(
35-
String path, String snapshotPath) async {
36-
List<dynamic> events =
37-
await promiseToFuture(getEventsSince(path, snapshotPath));
38-
return events.cast<ParcelWatcherEvent>();
39-
}
35+
@JS('getEventsSince')
36+
external JSPromise<JSArray<ParcelWatcherEvent>> _getEventsSince(
37+
String path, String snapshotPath);
38+
Future<List<ParcelWatcherEvent>> getEventsSince(
39+
String path, String snapshotPath) async =>
40+
(await _getEventsSince(path, snapshotPath).toDart).toDart;
4041

41-
external static Promise writeSnapshot(String path, String snapshotPath);
42-
static Future<void> writeSnapshotFuture(String path, String snapshotPath) =>
43-
promiseToFuture(writeSnapshot(path, snapshotPath));
42+
@JS('writeSnapshot')
43+
external JSPromise<JSAny> _writeSnapshot(String path, String snapshotPath);
44+
Future<void> writeSnapshot(String path, String snapshotPath) =>
45+
_writeSnapshot(path, snapshotPath).toDart;
4446
}
47+
48+
@JS('parcel_watcher')
49+
external ParcelWatcher? get parcelWatcher;

lib/src/parse/parser.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ class Parser {
651651
var span = scanner.spanFrom(state);
652652
return _interpolationMap == null
653653
? span
654-
: LazyFileSpan(() => _interpolationMap!.mapSpan(span));
654+
: LazyFileSpan(() => _interpolationMap.mapSpan(span));
655655
}
656656

657657
/// Throws an error associated with [span].

lib/src/visitor/async_evaluate.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -1812,7 +1812,7 @@ final class _EvaluateVisitor
18121812
if (result != null) {
18131813
isDependency = _inDependency;
18141814
} else {
1815-
result = await _nodeImporter!.loadAsync(originalUrl, previous, forImport);
1815+
result = await _nodeImporter.loadAsync(originalUrl, previous, forImport);
18161816
if (result == null) return null;
18171817
isDependency = true;
18181818
}

lib/src/visitor/evaluate.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// DO NOT EDIT. This file was generated from async_evaluate.dart.
66
// See tool/grind/synchronize.dart for details.
77
//
8-
// Checksum: 396c8f169d95c601598b8c3be1f4b948ca22effa
8+
// Checksum: 3986f5db33dd220dcd971a39e8587ca4e52d9a3f
99
//
1010
// ignore_for_file: unused_import
1111

@@ -1808,7 +1808,7 @@ final class _EvaluateVisitor
18081808
if (result != null) {
18091809
isDependency = _inDependency;
18101810
} else {
1811-
result = _nodeImporter!.load(originalUrl, previous, forImport);
1811+
result = _nodeImporter.load(originalUrl, previous, forImport);
18121812
if (result == null) return null;
18131813
isDependency = true;
18141814
}

package/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717
"node": ">=14.0.0"
1818
},
1919
"dependencies": {
20-
"@parcel/watcher": "^2.4.1",
2120
"chokidar": "^4.0.0",
2221
"immutable": "^4.0.0",
2322
"source-map-js": ">=0.6.2 <2.0.0"
2423
},
24+
"optionalDependencies": {
25+
"@parcel/watcher": "^2.4.1"
26+
},
2527
"keywords": [
2628
"style",
2729
"scss",

pkg/sass-parser/CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## 0.4.3-dev
1+
## 0.4.3
22

33
* Add support for parsing the `@while` rule.
44

pkg/sass-parser/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sass-parser",
3-
"version": "0.4.3-dev",
3+
"version": "0.4.3",
44
"description": "A PostCSS-compatible wrapper of the official Sass parser",
55
"repository": "sass/sass",
66
"author": "Google Inc.",

pkg/sass_api/CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## 14.1.2-dev
1+
## 14.1.2
22

33
* No user-visible changes.
44

pkg/sass_api/pubspec.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ name: sass_api
22
# Note: Every time we add a new Sass AST node, we need to bump the *major*
33
# version because it's a breaking change for anyone who's implementing the
44
# visitor interface(s).
5-
version: 14.1.2-dev
5+
version: 14.1.2
66
description: Additional APIs for Dart Sass.
77
homepage: https://github.com/sass/dart-sass
88

99
environment:
10-
sdk: ">=3.0.0 <4.0.0"
10+
sdk: ">=3.3.0 <4.0.0"
1111

1212
dependencies:
1313
sass: 1.80.6

pubspec.yaml

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: sass
2-
version: 1.80.6-dev
2+
version: 1.80.6
33
description: A Sass implementation in Dart.
44
homepage: https://github.com/sass/dart-sass
55

@@ -8,13 +8,13 @@ executables:
88
sass: sass
99

1010
environment:
11-
sdk: ">=3.0.0 <4.0.0"
11+
sdk: ">=3.3.0 <4.0.0"
1212

1313
dependencies:
1414
args: ^2.0.0
1515
async: ^2.5.0
1616
charcode: ^1.2.0
17-
cli_pkg: ^2.8.0
17+
cli_pkg: ^2.11.0
1818
cli_repl: ^0.2.1
1919
collection: ^1.16.0
2020
http: ^1.1.0

tool/grind.dart

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ void main(List<String> args) {
3535
pkg.homebrewFormula.value = "Formula/sass.rb";
3636
pkg.homebrewEditFormula.value = _updateHomebrewLanguageRevision;
3737
pkg.jsRequires.value = [
38-
pkg.JSRequire("@parcel/watcher", target: pkg.JSRequireTarget.cli),
38+
pkg.JSRequire("@parcel/watcher",
39+
target: pkg.JSRequireTarget.cli, lazy: true, optional: true),
3940
pkg.JSRequire("immutable", target: pkg.JSRequireTarget.all),
4041
pkg.JSRequire("chokidar", target: pkg.JSRequireTarget.cli),
4142
pkg.JSRequire("readline", target: pkg.JSRequireTarget.cli),

0 commit comments

Comments
 (0)