From 4379462b42700dddc8d9f7cd0ddc7325ceae0a7e Mon Sep 17 00:00:00 2001 From: Natalie Weizenbaum Date: Wed, 6 Oct 2021 11:36:17 -0700 Subject: [PATCH] Replace FileImporterResult with a plain URL --- lib/src/importer/node_to_dart/async_file.dart | 28 ++++-------------- lib/src/importer/node_to_dart/file.dart | 29 ++++--------------- lib/src/node/importer.dart | 10 +------ 3 files changed, 11 insertions(+), 56 deletions(-) diff --git a/lib/src/importer/node_to_dart/async_file.dart b/lib/src/importer/node_to_dart/async_file.dart index 2a79fccae..47949bf81 100644 --- a/lib/src/importer/node_to_dart/async_file.dart +++ b/lib/src/importer/node_to_dart/async_file.dart @@ -6,12 +6,10 @@ import 'dart:async'; import 'package:node_interop/js.dart'; import 'package:node_interop/util.dart'; -import 'package:path/path.dart' as p; -import '../../io.dart' as io; import '../../node/importer.dart'; +import '../../node/url.dart'; import '../../node/utils.dart'; -import '../../syntax.dart'; import '../async.dart'; import '../filesystem.dart'; import '../result.dart'; @@ -29,9 +27,6 @@ class NodeToDartAsyncFileImporter extends AsyncImporter { /// The wrapped `findFileUrl` function. final Object? Function(String, CanonicalizeOptions) _findFileUrl; - /// A map from canonical URLs to the `sourceMapUrl`s associated with them. - final _sourceMapUrls = {}; - NodeToDartAsyncFileImporter(this._findFileUrl); FutureOr canonicalize(Uri url) async { @@ -41,16 +36,11 @@ class NodeToDartAsyncFileImporter extends AsyncImporter { url.toString(), CanonicalizeOptions(fromImport: fromImport)); if (isPromise(result)) result = await promiseToFuture(result as Promise); if (result == null) return null; - - result as NodeFileImporterResult; - var dartUrl = result.url; - var sourceMapUrl = result.sourceMapUrl; - if (dartUrl == null) { - jsThrow(JsError( - "The findFileUrl() method must return an object a url field.")); + if (!isJSUrl(result)) { + jsThrow(JsError("The findFileUrl() method must return a URL.")); } - var resultUrl = jsToDartUrl(dartUrl); + var resultUrl = jsToDartUrl(result as JSUrl); if (resultUrl.scheme != 'file') { jsThrow(JsError( 'The findFileUrl() must return a URL with scheme file://, was ' @@ -59,18 +49,10 @@ class NodeToDartAsyncFileImporter extends AsyncImporter { var canonical = _filesystemImporter.canonicalize(resultUrl); if (canonical == null) return null; - if (sourceMapUrl != null) { - _sourceMapUrls[canonical] = jsToDartUrl(sourceMapUrl); - } - return canonical; } - ImporterResult? load(Uri url) { - var path = p.fromUri(url); - return ImporterResult(io.readFile(path), - sourceMapUrl: _sourceMapUrls[url] ?? url, syntax: Syntax.forPath(path)); - } + ImporterResult? load(Uri url) => _filesystemImporter.load(url); DateTime modificationTime(Uri url) => _filesystemImporter.modificationTime(url); diff --git a/lib/src/importer/node_to_dart/file.dart b/lib/src/importer/node_to_dart/file.dart index 18e194126..e8033e91a 100644 --- a/lib/src/importer/node_to_dart/file.dart +++ b/lib/src/importer/node_to_dart/file.dart @@ -3,13 +3,11 @@ // https://opensource.org/licenses/MIT. import 'package:node_interop/js.dart'; -import 'package:path/path.dart' as p; -import '../../io.dart' as io; import '../../importer.dart'; import '../../node/importer.dart'; +import '../../node/url.dart'; import '../../node/utils.dart'; -import '../../syntax.dart'; import '../filesystem.dart'; import '../result.dart'; import '../utils.dart'; @@ -26,9 +24,6 @@ class NodeToDartFileImporter extends Importer { /// The wrapped `findFileUrl` function. final Object? Function(String, CanonicalizeOptions) _findFileUrl; - /// A map from canonical URLs to the `sourceMapUrl`s associated with them. - final _sourceMapUrls = {}; - NodeToDartFileImporter(this._findFileUrl); Uri? canonicalize(Uri url) { @@ -42,17 +37,11 @@ class NodeToDartFileImporter extends Importer { jsThrow(JsError( "The canonicalize() function can't return a Promise for synchronous " "compile functions.")); + } else if (!isJSUrl(result)) { + jsThrow(JsError("The findFileUrl() method must return a URL.")); } - result as NodeFileImporterResult; - var dartUrl = result.url; - var sourceMapUrl = result.sourceMapUrl; - if (dartUrl == null) { - jsThrow(JsError( - "The findFileUrl() method must return an object a url field.")); - } - - var resultUrl = jsToDartUrl(dartUrl); + var resultUrl = jsToDartUrl(result as JSUrl); if (resultUrl.scheme != 'file') { jsThrow(JsError( 'The findFileUrl() must return a URL with scheme file://, was ' @@ -61,18 +50,10 @@ class NodeToDartFileImporter extends Importer { var canonical = _filesystemImporter.canonicalize(resultUrl); if (canonical == null) return null; - if (sourceMapUrl != null) { - _sourceMapUrls[canonical] = jsToDartUrl(sourceMapUrl); - } - return canonical; } - ImporterResult? load(Uri url) { - var path = p.fromUri(url); - return ImporterResult(io.readFile(path), - sourceMapUrl: _sourceMapUrls[url] ?? url, syntax: Syntax.forPath(path)); - } + ImporterResult? load(Uri url) => _filesystemImporter.load(url); DateTime modificationTime(Uri url) => _filesystemImporter.modificationTime(url); diff --git a/lib/src/node/importer.dart b/lib/src/node/importer.dart index 33a4ecc0d..3be952951 100644 --- a/lib/src/node/importer.dart +++ b/lib/src/node/importer.dart @@ -11,8 +11,7 @@ import 'url.dart'; class NodeImporter { external Object? Function(String, CanonicalizeOptions)? get canonicalize; external Object? Function(JSUrl)? get load; - external NodeFileImporterResult? Function(String, CanonicalizeOptions)? - get findFileUrl; + external Object? Function(String, CanonicalizeOptions)? get findFileUrl; } @JS() @@ -30,10 +29,3 @@ class NodeImporterResult { external String? get syntax; external JSUrl? get sourceMapUrl; } - -@JS() -@anonymous -class NodeFileImporterResult { - external JSUrl? get url; - external JSUrl? get sourceMapUrl; -}