Skip to content

Commit

Permalink
Replace FileImporterResult with a plain URL
Browse files Browse the repository at this point in the history
  • Loading branch information
nex3 committed Oct 6, 2021
1 parent d667843 commit 4379462
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 56 deletions.
28 changes: 5 additions & 23 deletions lib/src/importer/node_to_dart/async_file.dart
Expand Up @@ -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';
Expand All @@ -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 = <Uri, Uri>{};

NodeToDartAsyncFileImporter(this._findFileUrl);

FutureOr<Uri?> canonicalize(Uri url) async {
Expand All @@ -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 '
Expand All @@ -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);
Expand Down
29 changes: 5 additions & 24 deletions lib/src/importer/node_to_dart/file.dart
Expand Up @@ -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';
Expand All @@ -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 = <Uri, Uri>{};

NodeToDartFileImporter(this._findFileUrl);

Uri? canonicalize(Uri url) {
Expand All @@ -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 '
Expand All @@ -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);
Expand Down
10 changes: 1 addition & 9 deletions lib/src/node/importer.dart
Expand Up @@ -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()
Expand All @@ -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;
}

0 comments on commit 4379462

Please sign in to comment.