Skip to content

Commit

Permalink
Re-enable support for default exports (#2009)
Browse files Browse the repository at this point in the history
See #2008
  • Loading branch information
nex3 committed Jun 13, 2023
1 parent ce58d87 commit 3de612e
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
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
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
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();
});
}

0 comments on commit 3de612e

Please sign in to comment.