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

Support --pkg-importer=node #267

Merged
merged 2 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 2.3.0

* Add a `--pkg-importer` flag to enable loading dependencies from `pkg:` URLs
with the same behavior as the compiler. Currently this only supports the
Node.js package resolution algorithm, via `--pkg-importer=node`. For example,
`@use "pkg:bootstrap"` will load `node_modules/bootstrap/scss/bootstrap.scss`.

## 2.2.1

### Module Migrator
Expand Down
8 changes: 5 additions & 3 deletions lib/src/migrator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,11 @@ abstract class Migrator extends Command<Map<Uri, String>> {
Map<Uri, String> run() {
var allMigrated = <Uri, String>{};
var importer = FilesystemImporter('.');
var importCache = ImportCache(
importers: [NodeModulesImporter()],
loadPaths: globalResults!['load-path']);
var importCache = ImportCache(importers: [
NodeModulesImporter(),
if ((globalResults!['pkg-importer'] as List<String>).contains('node'))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would communicate the likely extensions of this behavior better to make this a for loop.

NodePackageImporter('.')
], loadPaths: globalResults!['load-path']);

var entrypoints = [
for (var argument in argResults!.rest)
Expand Down
4 changes: 2 additions & 2 deletions lib/src/migrators/module/reference_source.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ class ImportSource extends ReferenceSource {
/// Returns the preferred namespace to use for this module, based on
/// [originalRuleUrl].
String get preferredNamespace {
var path = url.path;
var basename = p.url.basenameWithoutExtension(url.path);
var path = (originalRuleUrl ?? url).path;
var basename = p.url.basenameWithoutExtension(path);
if (basename == 'index' || basename == '_index') path = p.url.dirname(path);
return namespaceForPath(path);
}
Expand Down
8 changes: 7 additions & 1 deletion lib/src/runner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import 'dart:isolate';
import 'package:args/args.dart';
import 'package:args/command_runner.dart';
import 'package:path/path.dart' as p;
import 'package:sass_migrator/src/migrators/color.dart';
import 'package:source_span/source_span.dart';
import 'package:term_glyph/term_glyph.dart' as glyph;

import 'io.dart';
import 'migrators/calc_interpolation.dart';
import 'migrators/color.dart';
import 'migrators/division.dart';
import 'migrators/module.dart';
import 'migrators/namespace.dart';
Expand Down Expand Up @@ -42,6 +42,12 @@ class MigratorRunner extends CommandRunner<Map<Uri, String>> {
abbr: 'd',
help: 'Migrate dependencies in addition to entrypoints.',
negatable: false)
..addMultiOption('pkg-importer',
abbr: 'p',
valueHelp: 'TYPE',
allowed: ['node'],
help: 'Built-in importer(s) to use for pkg: URLs.',
allowedHelp: {'node': 'Load files like Node.js package resolution.'})
..addFlag('dry-run',
abbr: 'n',
help: 'Show which files would be migrated but make no changes.',
Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: sass_migrator
version: 2.2.1
version: 2.3.0
description: A tool for running migrations on Sass files
homepage: https://github.com/sass/migrator

Expand All @@ -17,7 +17,7 @@ dependencies:
node_interop: ^2.0.2
node_io: ^2.3.0
path: ^1.8.0
sass_api: ^14.1.0
sass_api: ^14.4.0
source_span: ^1.8.1
stack_trace: ^1.10.0
string_scanner: ^1.1.0
Expand Down
26 changes: 26 additions & 0 deletions test/migrators/module/paths/pkg_node_modules_default.hrx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<==> arguments
--migrate-deps --pkg-importer=node

<==> input/entrypoint.scss
@import "pkg:module";

a {
color: $variable;
}

<==> input/node_modules/module/styles/index.scss
$variable: green;

<==> input/node_modules/module/package.json
{
"exports": {
"sass": "./styles/index.scss"
}
}

<==> output/entrypoint.scss
@use "pkg:module";

a {
color: module.$variable;
}
28 changes: 28 additions & 0 deletions test/migrators/module/paths/pkg_node_modules_subpath.hrx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<==> arguments
--migrate-deps --pkg-importer=node

<==> input/entrypoint.scss
@import "pkg:module/foo";

a {
color: $variable;
}

<==> input/node_modules/module/styles/test.scss
$variable: green;

<==> input/node_modules/module/package.json
{
"exports": {
"./foo.scss": {
"sass": "./styles/test.scss"
}
}
}

<==> output/entrypoint.scss
@use "pkg:module/foo";

a {
color: foo.$variable;
}
Loading