Repository: Polymer/vulcanize
Pull request #248 (2015-09-25 23:16:33)

Add ability to programmatically add imports.


Patch:
diff --git a/bin/vulcanize b/bin/vulcanize
index 006f18b..b996d04 100755
--- a/bin/vulcanize
+++ b/bin/vulcanize
@@ -27,6 +27,7 @@ var help = [
   '  -p <arg>|--abspath <arg>: use <arg> as the "webserver root", make all adjusted urls absolute',
   '  --inline-scripts: Inline external scripts',
   '  --inline-css: Inline external stylesheets',
+  '  --add-import <path>: Add this import to the target HTML before vulcanizing. Can be used multiple times.',
   '  --exclude <path>: exclude a subpath from root. Use multiple times to exclude multiple paths. Tags to excluded paths are kept.',
   '  --strip-exclude: Exclude a subpath and strip the link that includes it.',
   '  --strip-comments: Strips all HTML comments not containing an @license from the document',
@@ -71,6 +72,7 @@ var args = nopt(
     version: Boolean,
     abspath: String,
     exclude: [String, Array],
+    'add-import': [String, Array],
     'strip-exclude': [String, Array],
     'strip-comments': Boolean,
     'no-implicit-strip': Boolean,
@@ -120,6 +122,7 @@ function stringToRegExp(str) {
   return new RegExp(str.replace(/[-\/\\*+?.()|[\]{}]/g, '\\$&'));
 }
 
+args.addedImports = args['add-import'] || [];
 args.excludes = args.exclude || [];
 args.stripExcludes = args['strip-exclude'] || [];
 args.stripComments = args['strip-comments'];
diff --git a/lib/vulcan.js b/lib/vulcan.js
index 613c18b..27dcee8 100644
--- a/lib/vulcan.js
+++ b/lib/vulcan.js
@@ -53,6 +53,7 @@ var Vulcan = function Vulcan(opts) {
   this.implicitStrip = opts.implicitStrip === undefined ? true : Boolean(opts.implicitStrip);
   this.abspath = (String(opts.abspath) === opts.abspath && String(opts.abspath).trim() !== '') ? path.resolve(opts.abspath) : '';
   this.pathResolver = new PathResolver(this.abspath);
+  this.addedImports = Array.isArray(opts.addedImports) ? opts.addedImports : [];
   this.excludes = Array.isArray(opts.excludes) ? opts.excludes : [];
   this.stripExcludes = Array.isArray(opts.stripExcludes) ? opts.stripExcludes : [];
   this.stripComments = Boolean(opts.stripComments);
@@ -313,12 +314,18 @@ Vulcan.prototype = {
       }
       // make sure there's a <meta charset> in the page to force UTF-8
       var meta = dom5.query(flatDoc, matchers.meta);
+      var head = dom5.query(flatDoc, matchers.head);
       if (!meta) {
         meta = dom5.constructors.element('meta');
         dom5.setAttribute(meta, 'charset', 'UTF-8');
-        var head = dom5.query(flatDoc, matchers.head);
         this.prepend(head, meta);
       }
+      for (var i = 0; i < this.addedImports.length; i++) {
+        var newImport = dom5.constructors.element('link');
+        dom5.setAttribute(newImport, 'rel', 'import');
+        dom5.setAttribute(newImport, 'href', this.addedImports[i]);
+        dom5.append(head, newImport);
+      }
       return {doc: flatDoc, href: tree.href};
     }.bind(this));
     if (this.enableScriptInlining) {


Test patch:
diff --git a/test/test.js b/test/test.js
index 6546282..3a847ab 100644
--- a/test/test.js
+++ b/test/test.js
@@ -688,6 +688,23 @@ suite('Vulcan', function() {
     });
   });
 
+  suite('Add import', function(){
+    var options = {
+      addedImports: ['imports/comment-in-import.html']
+    };
+    test('added import is added to vulcanized doc', function(done) {
+      process('test/html/default.html', function(err, doc) {
+      if (err) {
+        return done(err);
+      }
+      assert(doc);
+      var hasAddedImport = preds.hasAttrValue('href', 'imports/comment-in-import.html');
+      assert.equal(dom5.queryAll(doc, hasAddedImport).length, 1);
+      done();
+    }, options);
+    });
+  });
+
   suite('Input URL', function() {
     var options = {
       inputUrl: 'test/html/default.html'