Skip to content

Commit

Permalink
Re-enable support for default exports
Browse files Browse the repository at this point in the history
See #2008
  • Loading branch information
nex3 committed Jun 9, 2023
1 parent aa59a5f commit e4d0b6e
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
## 1.63.4

### JavaScript API

* Re-enable support for `import sass from 'sass'` when loading the package from
an ESM module in Node.js. However, this syntax is now deprecated; ESM users
should use `import * as sass from 'sass'` instead.

On the browser and other ESM-only platforms, only `import * as sass from
'sass'` is supported.

### Embedded Sass

* Fix a race condition where closing standard input while requests are in-flight
Expand Down
38 changes: 38 additions & 0 deletions tool/grind.dart
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ void main(List<String> args) {
};

pkg.addAllTasks();

afterTask("pkg-npm-dev", _addDefaultExport);
afterTask("pkg-npm-release", _addDefaultExport);

grind(args);
}

Expand Down Expand Up @@ -243,3 +247,37 @@ dart run protoc_plugin "\$@"
Platform.environment["PATH"]!
}));
}

/// After building the NPM package, add default exports to
/// `build/npm/sass.node.mjs`.
///
/// See sass/dart-sass#2008.
void _addDefaultExport() {
var buffer = StringBuffer();
buffer.writeln(File("build/npm/sass.node.mjs").readAsStringSync());

buffer.writeln("""
let printedDefaultExportDeprecation = false;
function defaultExportDeprecation() {
if (printedDefaultExportDeprecation) return;
printedDefaultExportDeprecation = true;
console.error(
"`import sass from 'sass'` is deprecated.\\n" +
"Please use `import * as sass from 'sass'` instead.");
}
""");

buffer.writeln("export default {");
for (var export in pkg.jsEsmExports.value!) {
buffer.write("""
get $export() {
defaultExportDeprecation();
return _cliPkgExports.$export;
},
""");
}

buffer.writeln("};");

File("build/npm/sass.node.mjs").writeAsStringSync(buffer.toString());
}
20 changes: 20 additions & 0 deletions tool/grind/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@
// MIT-style license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.

import 'dart:async';
import 'dart:convert';
import 'dart:io';

import 'package:cli_pkg/cli_pkg.dart' as pkg;
import 'package:grinder/grinder.dart';
import 'package:path/path.dart' as p;

// Work around the lack of google/grinder.dart#402.
import 'package:grinder/src/singleton.dart';

/// Options for [run] that tell Git to commit using SassBot's name and email.
final sassBotEnvironment = RunOptions(environment: {
"GIT_AUTHOR_NAME": pkg.botName.value,
Expand Down Expand Up @@ -75,3 +79,19 @@ String cloneOrCheckout(String url, String ref, {String? name}) {

return path;
}

/// Registers [callback] to run after the task named [taskName].
///
/// This must be called after the base [taskName] is registered.
void afterTask(String taskName, FutureOr<void> callback()) {
// This takes advantage of the fact that Grinder's task list is mutable to
// override the existing task with our new one.
var index = grinder.tasks.indexWhere((task) => task.name == taskName);
if (index == -1) fail("There is no task named $taskName.");

var oldTask = grinder.tasks[index];
grinder.tasks[index] = GrinderTask(taskName, description: oldTask.description, depends: oldTask.depends, taskFunction: (TaskArgs args) async {
await oldTask.execute(context, args);
await callback();
});
}

0 comments on commit e4d0b6e

Please sign in to comment.