Skip to content
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

Re-enable support for default exports #2009

Merged
merged 3 commits into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

### 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.

* Properly export the legacy API values `TRUE`, `FALSE`, `NULL`, and `types` from
the ECMAScript module API.

Expand Down
38 changes: 38 additions & 0 deletions tool/grind.dart
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ void main(List<String> args) {
};

pkg.addAllTasks();

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

grind(args);
}

Expand Down Expand Up @@ -247,3 +251,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());
}
22 changes: 22 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,21 @@ 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();
});
}