Repository: Polymer/vulcanize
Pull request #220 (2015-07-22 22:59:41)

Treat all internal paths as URLs until hitting hydrolysis


Patch:
diff --git a/bin/vulcanize b/bin/vulcanize
index e2e7252..006f18b 100755
--- a/bin/vulcanize
+++ b/bin/vulcanize
@@ -129,9 +129,8 @@ args.inlineCss = args['inline-css'];
 
 (new vulcan(args)).process(target, function(err, content) {
   if (err) {
-    console.error(err);
-    console.error(err.stack);
+    process.stderr.write(require('util').inspect(err));
     process.exit(1);
   }
-  console.log(content);
+  process.stdout.write(content);
 });
diff --git a/lib/pathresolver.js b/lib/pathresolver.js
index 52ad340..a34eb3a 100644
--- a/lib/pathresolver.js
+++ b/lib/pathresolver.js
@@ -122,6 +122,24 @@ PathResolver.prototype = {
         });
       }
     }
+  },
+
+  pathToUrl: function pathToUrl(filePath) {
+    var absolutePath = path.resolve(filePath);
+    if (process.platform === 'win32') {
+      // encode C:\foo\ as C:/foo/
+      return absolutePath.split('\\').join('/');
+    } else {
+      return absolutePath;
+    }
+  },
+  urlToPath: function urlToPath(uri) {
+    var parsed = url.parse(uri);
+    if (process.platform === 'win32') {
+      return parsed.protocol + parsed.pathname.split('/').join('\\');
+    } else {
+      return (parsed.protocol || '') + parsed.pathname;
+    }
   }
 };
 
diff --git a/lib/vulcan.js b/lib/vulcan.js
index 6847c1e..af6ac82 100644
--- a/lib/vulcan.js
+++ b/lib/vulcan.js
@@ -12,6 +12,7 @@
 'use strict';
 
 var path = require('path');
+var url = require('url');
 var pathPosix = path.posix || require('path-posix');
 var hyd = require('hydrolysis');
 var dom5 = require('dom5');
@@ -81,6 +82,9 @@ Vulcan.prototype = {
   },
 
   isExcludedHref: function isExcludedHref(href) {
+    if (constants.EXTERNAL_URL.test(href)) {
+      return true;
+    }
     if (!this.excludes) {
       return false;
     }
@@ -145,7 +149,7 @@ Vulcan.prototype = {
     var importNodes = tree.html.import;
     // early check for old polymer versions
     if (this.hasOldPolymer(doc)) {
-      throw new Error(constants.OLD_POLYMER + ' File: ' + tree.href);
+      throw new Error(constants.OLD_POLYMER + ' File: ' + this.pathResolver.urlToPath(tree.href));
     }
     this.pathResolver.acid(doc, tree.href);
     if (imports) {
@@ -209,16 +213,16 @@ Vulcan.prototype = {
 
   // inline scripts into document, returns a promise resolving to document.
   inlineScripts: function inlineScripts(doc, href) {
-    var URL = require('url');
     var scripts = dom5.queryAll(doc, matchers.JS_SRC);
     var scriptPromises = scripts.map(function(script) {
       var src = dom5.getAttribute(script, 'src');
-      var uri = URL.resolve(href, src);
+      var uri = url.resolve(href, src);
       // let the loader handle the requests
       if (this.isExcludedHref(src)) {
         return Promise.resolve(true);
       }
-      return this.loader.request(uri).then(function(content) {
+      var filePath = path.resolve(this.pathResolver.urlToPath(uri));
+      return this.loader.request(filePath).then(function(content) {
         if (content) {
           dom5.removeAttribute(script, 'src');
           dom5.setTextContent(script, content);
@@ -226,24 +230,24 @@ Vulcan.prototype = {
       });
     }.bind(this));
     // When all scripts are read, return the document
-    return Promise.all(scriptPromises).then(function(){ return doc; });
+    return Promise.all(scriptPromises).then(function(){ return {doc: doc, href: href}; });
   },
 
 
   // inline scripts into document, returns a promise resolving to document.
   inlineCss: function inlineCss(doc, href) {
-    var URL = require('url');
     var css_links = dom5.queryAll(doc, matchers.ALL_CSS_LINK);
     var cssPromises = css_links.map(function(link) {
       var tag = link;
       var src = dom5.getAttribute(tag, 'href');
-      var uri = URL.resolve(href, src);
+      var uri = url.resolve(href, src);
       // let the loader handle the requests
       if (this.isExcludedHref(src)) {
         return Promise.resolve(true);
       }
       // let the loader handle the requests
-      return this.loader.request(uri).then(function(content) {
+      var filePath = path.resolve(this.pathResolver.urlToPath(uri));
+      return this.loader.request(filePath).then(function(content) {
         if (content) {
           content = this.pathResolver.rewriteURL(uri, href, content);
           var style = dom5.constructors.element('style');
@@ -253,7 +257,7 @@ Vulcan.prototype = {
       }.bind(this));
     }.bind(this));
     // When all style imports are read, return the document
-    return Promise.all(cssPromises).then(function(){ return doc; });
+    return Promise.all(cssPromises).then(function(){ return {doc: doc, href: href}; });
   },
 
   getImplicitExcludes: function getImplicitExcludes(excludes) {
@@ -305,31 +309,34 @@ Vulcan.prototype = {
         var head = dom5.query(flatDoc, matchers.head);
         this.prepend(head, meta);
       }
-      return flatDoc;
+      return {doc: flatDoc, href: tree.href};
     }.bind(this));
     if (this.enableScriptInlining) {
-      chain = chain.then(function(doc) {
-        return this.inlineScripts(doc, target);
+      chain = chain.then(function(docObj) {
+        return this.inlineScripts(docObj.doc, docObj.href);
       }.bind(this));
     }
     if (this.enableCssInlining) {
-      chain = chain.then(function(doc) {
-        return this.inlineCss(doc, target);
+      chain = chain.then(function(docObj) {
+        return this.inlineCss(docObj.doc, docObj.href);
       }.bind(this));
     }
-    chain.then(function(flatDoc) {
-      cb(null, dom5.serialize(flatDoc));
+    chain.then(function(docObj) {
+      cb(null, dom5.serialize(docObj.doc));
     }).catch(cb);
   },
 
   process: function process(target, cb) {
     if (this.inputUrl) {
-      target = this.inputUrl;
-    }
-    if (this.abspath) {
-      target = pathPosix.resolve('/', target);
+      this._process(this.inputUrl, cb);
+    } else {
+      if (this.abspath) {
+        target = pathPosix.resolve('/', target);
+      } else {
+        target = this.pathResolver.pathToUrl(path.resolve(target));
+      }
+      this._process(target, cb);
     }
-    this._process(target, cb);
   }
 };
 


Test patch:
diff --git a/test/test.js b/test/test.js
index 9d4b366..8f9ee07 100644
--- a/test/test.js
+++ b/test/test.js
@@ -385,7 +385,7 @@ suite('Vulcan', function() {
 
     test('Old Polymer is detected and warns', function(done) {
       var constants = require('../lib/constants');
-      var input = 'test/html/old-polymer.html';
+      var input = path.resolve('test/html/old-polymer.html');
       process(input, function(err, doc) {
         if (err) {
           try {