Skip to content

Commit 63b9922

Browse files
authored
Load import-only files through package.json exports (#2772)
See sass/sass#4224
1 parent c7e9947 commit 63b9922

7 files changed

Lines changed: 34 additions & 8 deletions

File tree

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
## 1.101.0
2+
3+
* **Potentially breaking bug fix:** The Node package importer now properly
4+
supports resolving import-only variants of Sass files declared in the
5+
`exports`, `sass`, and `style` fields of `package.json`. Previously, these
6+
files were ignored even when loaded via `@import`, so any code relying on
7+
loading module-system-only files this way may break.
8+
19
## 1.100.0
210

311
* Writing two compound selectors adjacent to one another without any whitespace

lib/src/importer/node_package.dart

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class NodePackageImporter extends Importer {
8080
)
8181
case var resolved?) {
8282
if (_validExtensions.contains(p.extension(resolved))) {
83-
return p.toUri(p.canonicalize(resolved));
83+
return p.toUri(p.canonicalize(_resolveImportOnly(resolved)));
8484
} else {
8585
throw "The export for '${subpath ?? "root"}' in "
8686
"'$packageName' resolved to '${resolved.toString()}', "
@@ -144,18 +144,18 @@ class NodePackageImporter extends Importer {
144144
) {
145145
if (packageManifest['sass'] case String sassValue
146146
when _validExtensions.contains(p.url.extension(sassValue))) {
147-
return p.join(packageRoot, sassValue);
147+
return _resolveImportOnly(p.join(packageRoot, sassValue));
148148
} else if (packageManifest['style'] case String styleValue
149149
when _validExtensions.contains(p.url.extension(styleValue))) {
150-
return p.join(packageRoot, styleValue);
150+
return _resolveImportOnly(p.join(packageRoot, styleValue));
151151
}
152152

153153
var result = resolveImportPath(p.join(packageRoot, 'index'));
154154
return result;
155155
}
156156

157157
/// Returns a file path specified by a `subpath` in the `exports` section of
158-
/// package.json.
158+
/// `packageManifest`.
159159
///
160160
/// `packageName` is used for error reporting.
161161
String? _resolvePackageExports(
@@ -285,6 +285,16 @@ class NodePackageImporter extends Importer {
285285
};
286286
}
287287

288+
/// Returns either [path] or, if necessary, the import-only variant that
289+
/// should be loaded instead.
290+
String _resolveImportOnly(String path) {
291+
if (!fromImport) return path;
292+
var extension = p.extension(path);
293+
assert(_validExtensions.contains(extension));
294+
var importOnly = '${p.withoutExtension(path)}.import$extension';
295+
return fileExists(importOnly) ? importOnly : path;
296+
}
297+
288298
/// Implementation of the `PATTERN_KEY_COMPARE` comparator from
289299
/// https://nodejs.org/api/esm.html#resolution-algorithm-specification.
290300
int _compareExpansionKeys(String keyA, String keyB) {

pkg/sass-parser/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.4.46
2+
3+
* No user-visible changes.
4+
15
## 0.4.45
26

37
* No user-visible changes.

pkg/sass-parser/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sass-parser",
3-
"version": "0.4.45",
3+
"version": "0.4.46",
44
"description": "A PostCSS-compatible wrapper of the official Sass parser",
55
"repository": "sass/dart-sass",
66
"author": "Google Inc.",

pkg/sass_api/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 17.7.0
2+
3+
* No user-visible changes.
4+
15
## 17.6.0
26

37
* Add a `logger` parameter to `SelectorList.parse()`, `ComplexSelector.parse()`,

pkg/sass_api/pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ 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: 17.6.0
5+
version: 17.7.0
66
description: Additional APIs for Dart Sass.
77
homepage: https://github.com/sass/dart-sass
88

99
environment:
1010
sdk: ">=3.6.0 <4.0.0"
1111

1212
dependencies:
13-
sass: 1.100.0
13+
sass: 1.101.0
1414

1515
dev_dependencies:
1616
dartdoc: ">=8.0.14 <10.0.0"

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: sass
2-
version: 1.100.0
2+
version: 1.101.0
33
description: A Sass implementation in Dart.
44
homepage: https://github.com/sass/dart-sass
55

0 commit comments

Comments
 (0)