Refactors rebasing code.
authorJakub Pawlowicz <contact@jakubpawlowicz.com>
Fri, 16 Dec 2016 10:18:32 +0000 (11:18 +0100)
committerJakub Pawlowicz <contact@jakubpawlowicz.com>
Fri, 16 Dec 2016 11:00:34 +0000 (12:00 +0100)
Why:

* By using the same wording / variable names it is now easier to
  understand how rebasing words.
* It's remarkable in how few places we need to rebase paths now. :)

lib/reader/apply-source-maps.js
lib/reader/load-original-sources.js
lib/reader/read-sources.js
lib/reader/rebase-local-map.js

index c65f371..4e2912b 100644 (file)
@@ -87,7 +87,7 @@ function fetchAndApplySourceMap(sourceMapComment, source, singleSourceTokens, ap
 
 function extractInputSourceMapFrom(sourceMapComment, applyContext, whenSourceMapReady) {
   var uri = MAP_MARKER_PATTERN.exec(sourceMapComment)[1];
-  var rebasedToCurrentPath;
+  var absoluteUri;
   var sourceMap;
   var rebasedMap;
 
@@ -110,11 +110,11 @@ function extractInputSourceMapFrom(sourceMapComment, applyContext, whenSourceMap
     // at this point `uri` is already rebased, see lib/reader/rebase.js#rebaseSourceMapComment
     // it is rebased to be consistent with rebasing other URIs
     // however here we need to resolve it back to read it from disk
-    rebasedToCurrentPath = path.resolve(applyContext.rebaseTo, uri);
-    sourceMap = loadInputSourceMapFromLocalUri(rebasedToCurrentPath, applyContext);
+    absoluteUri = path.resolve(applyContext.rebaseTo, uri);
+    sourceMap = loadInputSourceMapFromLocalUri(absoluteUri, applyContext);
 
     if (sourceMap) {
-      rebasedMap = rebaseLocalMap(sourceMap, rebasedToCurrentPath, applyContext.rebaseTo);
+      rebasedMap = rebaseLocalMap(sourceMap, absoluteUri, applyContext.rebaseTo);
       return whenSourceMapReady(rebasedMap);
     } else {
       return whenSourceMapReady(null);
index dba1e54..95a3ebf 100644 (file)
@@ -100,19 +100,19 @@ function loadOriginalSourceFromRemoteUri(uri, loadContext, whenLoaded) {
   });
 }
 
-function loadOriginalSourceFromLocalUri(uri, loadContext) {
-  var isAllowed = isAllowedResource(uri, true, loadContext.processImportFrom);
-  var resolvedUri = path.resolve(loadContext.rebaseTo, uri);
+function loadOriginalSourceFromLocalUri(relativeUri, loadContext) {
+  var isAllowed = isAllowedResource(relativeUri, true, loadContext.processImportFrom);
+  var absoluteUri = path.resolve(loadContext.rebaseTo, relativeUri);
 
-  if (!fs.existsSync(resolvedUri) || !fs.statSync(resolvedUri).isFile()) {
-    loadContext.warnings.push('Ignoring local source map at "' + resolvedUri + '" as resource is missing.');
+  if (!fs.existsSync(absoluteUri) || !fs.statSync(absoluteUri).isFile()) {
+    loadContext.warnings.push('Ignoring local source map at "' + absoluteUri + '" as resource is missing.');
     return null;
   } else if (!isAllowed) {
-    loadContext.warnings.push('Cannot fetch "' + resolvedUri + '" as resource is not allowed.');
+    loadContext.warnings.push('Cannot fetch "' + absoluteUri + '" as resource is not allowed.');
     return null;
   }
 
-  return fs.readFileSync(resolvedUri, 'utf8');
+  return fs.readFileSync(absoluteUri, 'utf8');
 }
 
 module.exports = loadOriginalSources;
index 0531079..7581e78 100644 (file)
@@ -17,7 +17,7 @@ var isAbsoluteResource = require('../utils/is-absolute-resource');
 var isImport = require('../utils/is-import');
 var isRemoteResource = require('../utils/is-remote-resource');
 
-var UNKNOWN_SOURCE = 'unknown-source';
+var UNKNOWN_URI = 'uri:unknown';
 
 function readSources(input, context, callback) {
   return doReadSources(input, context, function (tokens) {
@@ -44,7 +44,7 @@ function doReadSources(input, context, callback) {
 function fromString(input, context, parentInlinerContext, callback) {
   var inputAsHash = {};
 
-  inputAsHash[UNKNOWN_SOURCE] = {
+  inputAsHash[UNKNOWN_URI] = {
     styles: input,
     sourceMap: (typeof context.options.sourceMap === 'string') ? context.options.sourceMap : null
   };
@@ -55,22 +55,21 @@ function fromString(input, context, parentInlinerContext, callback) {
 function fromArray(input, context, parentInlinerContext, callback) {
   var currentPath = path.resolve('');
   var inputAsHash = input.reduce(function (accumulator, uri) {
-    var isRemoteUri = isRemoteResource(uri);
-    var absolutePath = uri[0] == '/' || isRemoteUri ?
+    var absoluteUri = isAbsoluteResource(uri) || isRemoteResource(uri) ?
       uri :
       path.resolve(uri);
     var relativeToCurrentPath;
 
-    if (isRemoteUri) {
+    if (isRemoteResource(uri)) {
       accumulator[uri] = {
         styles: restoreImport(uri, '') + ';'
       };
-    } else if (!fs.existsSync(absolutePath) || !fs.statSync(absolutePath).isFile()) {
-      context.errors.push('Ignoring "' + absolutePath + '" as resource is missing.');
+    } else if (!fs.existsSync(absoluteUri) || !fs.statSync(absoluteUri).isFile()) {
+      context.errors.push('Ignoring "' + absoluteUri + '" as resource is missing.');
     } else {
-      relativeToCurrentPath = path.relative(currentPath, absolutePath);
+      relativeToCurrentPath = path.relative(currentPath, absoluteUri);
       accumulator[relativeToCurrentPath] = {
-        styles: fs.readFileSync(absolutePath, 'utf-8')
+        styles: fs.readFileSync(absoluteUri, 'utf-8')
       };
     }
 
@@ -83,46 +82,42 @@ function fromArray(input, context, parentInlinerContext, callback) {
 function fromHash(input, context, parentInlinerContext, callback) {
   var tokens = [];
   var newTokens = [];
-  var sourcePath;
+  var uri;
   var source;
-  var rebaseFrom;
-  var rebaseTo;
   var parsedMap;
   var rebasedMap;
   var rebaseConfig;
 
-  for (sourcePath in input) {
-    source = input[sourcePath];
-
-    if (sourcePath !== UNKNOWN_SOURCE && isRemoteResource(sourcePath)) {
-      rebaseFrom = sourcePath;
-      rebaseTo = sourcePath;
-    } else if (sourcePath !== UNKNOWN_SOURCE && isAbsoluteResource(sourcePath)) {
-      rebaseFrom = path.dirname(sourcePath);
-      rebaseTo = context.options.rebaseTo;
-    } else {
-      rebaseFrom = sourcePath !== UNKNOWN_SOURCE ?
-        path.dirname(path.resolve(sourcePath)) :
-        path.resolve('');
-      rebaseTo = context.options.rebaseTo;
-    }
-
-    rebaseConfig = {
-      fromBase: rebaseFrom,
-      toBase: rebaseTo
-    };
+  for (uri in input) {
+    source = input[uri];
 
     if (source.sourceMap) {
       parsedMap = JSON.parse(source.sourceMap);
-      rebasedMap = isRemoteResource(sourcePath) ?
-        rebaseRemoteMap(parsedMap, sourcePath) :
-        rebaseLocalMap(parsedMap, sourcePath, context.options.rebaseTo);
-      context.inputSourceMapTracker.track(sourcePath, rebasedMap);
+      rebasedMap = isRemoteResource(uri) ?
+        rebaseRemoteMap(parsedMap, uri) :
+        rebaseLocalMap(parsedMap, uri, context.options.rebaseTo);
+      context.inputSourceMapTracker.track(uri, rebasedMap);
     }
 
-    context.source = sourcePath !== UNKNOWN_SOURCE ? sourcePath : undefined;
+    context.source = uri !== UNKNOWN_URI ? uri : undefined;
     context.sourcesContent[context.source] = source.styles;
 
+    rebaseConfig = {};
+
+    if (uri == UNKNOWN_URI) {
+      rebaseConfig.fromBase = path.resolve('');
+      rebaseConfig.toBase = context.options.rebaseTo;
+    } else if (isRemoteResource(uri)) {
+      rebaseConfig.fromBase = uri;
+      rebaseConfig.toBase = uri;
+    } else if (isAbsoluteResource(uri)) {
+      rebaseConfig.fromBase = path.dirname(uri);
+      rebaseConfig.toBase = context.options.rebaseTo;
+    } else {
+      rebaseConfig.fromBase = path.dirname(path.resolve(uri));
+      rebaseConfig.toBase = context.options.rebaseTo;
+    }
+
     newTokens = tokenize(source.styles, context);
     newTokens = rebase(newTokens, context.options.rebase, context.validator, rebaseConfig);
 
@@ -250,21 +245,18 @@ function inlineRemoteStylesheet(uri, mediaQuery, metadata, inlinerContext) {
 
 function inlineLocalStylesheet(uri, mediaQuery, metadata, inlinerContext) {
   var currentPath = path.resolve('');
-  var relativeTo = uri[0] == '/' ?
-    currentPath :
-    path.resolve(inlinerContext.rebaseTo || '');
-  var absolutePath = uri[0] == '/' ?
-    path.resolve(relativeTo, uri.substring(1)) :
-    path.resolve(relativeTo, uri);
-  var relativeToCurrentPath = path.relative(currentPath, absolutePath);
+  var absoluteUri = isAbsoluteResource(uri) ?
+    path.resolve(currentPath, uri.substring(1)) :
+    path.resolve(inlinerContext.rebaseTo, uri);
+  var relativeToCurrentPath = path.relative(currentPath, absoluteUri);
   var importedStyles;
   var importedTokens;
   var isAllowed = isAllowedResource(uri, false, inlinerContext.processImportFrom);
   var sourceHash = {};
 
-  if (inlinerContext.inlinedStylesheets.indexOf(absolutePath) > -1) {
+  if (inlinerContext.inlinedStylesheets.indexOf(absoluteUri) > -1) {
     inlinerContext.warnings.push('Ignoring local @import of "' + uri + '" as it has already been imported.');
-  } else if (!fs.existsSync(absolutePath) || !fs.statSync(absolutePath).isFile()) {
+  } else if (!fs.existsSync(absoluteUri) || !fs.statSync(absoluteUri).isFile()) {
     inlinerContext.errors.push('Ignoring local @import of "' + uri + '" as resource is missing.');
   } else if (!isAllowed && inlinerContext.afterContent) {
     inlinerContext.warnings.push('Ignoring local @import of "' + uri + '" as resource is not allowed and after other content.');
@@ -274,8 +266,8 @@ function inlineLocalStylesheet(uri, mediaQuery, metadata, inlinerContext) {
     inlinerContext.warnings.push('Skipping local @import of "' + uri + '" as resource is not allowed.');
     inlinerContext.outputTokens = inlinerContext.outputTokens.concat(inlinerContext.sourceTokens.slice(0, 1));
   } else {
-    importedStyles = fs.readFileSync(absolutePath, 'utf-8');
-    inlinerContext.inlinedStylesheets.push(absolutePath);
+    importedStyles = fs.readFileSync(absoluteUri, 'utf-8');
+    inlinerContext.inlinedStylesheets.push(absoluteUri);
 
     sourceHash[relativeToCurrentPath] = {
       styles: importedStyles
index 3ca288d..aec8d23 100644 (file)
@@ -2,11 +2,11 @@ var path = require('path');
 
 function rebaseLocalMap(sourceMap, sourceUri, rebaseTo) {
   var currentPath = path.resolve('');
-  var absolutePath = path.resolve(currentPath, sourceUri);
-  var sourceDirectory = path.dirname(absolutePath);
+  var absoluteUri = path.resolve(currentPath, sourceUri);
+  var absoluteUriDirectory = path.dirname(absoluteUri);
 
   sourceMap.sources = sourceMap.sources.map(function(source) {
-    return path.relative(rebaseTo, path.resolve(sourceDirectory, source));
+    return path.relative(rebaseTo, path.resolve(absoluteUriDirectory, source));
   });
 
   return sourceMap;