Skip to content

Commit

Permalink
Refactor handling of inlined and excluded import insertion
Browse files Browse the repository at this point in the history
Remove "stored position" finding, which was the source of many head scratching.
Put inlined imports at the top of <body>
Put "excluded" imports after platform.js, or at the end of <head>

Fixes #10
  • Loading branch information
dfreedm committed Jan 11, 2014
1 parent 22bf857 commit 8117b13
Showing 1 changed file with 14 additions and 23 deletions.
37 changes: 14 additions & 23 deletions lib/vulcan.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var ABS_URL = /(^data:)|(^http[s]?:)|(^\/)/;
var DEFAULT_OUTPUT = 'vulcanized.html';
var ELEMENTS = 'polymer-element';
var IMPORTS = 'link[rel="import"][href]';
var POLYMER = 'script[src $= "polymer.js"], script[src $= "polymer.min.js"]';
var POLYFILLS = 'script[src $= "platform.js"]';
var SCRIPT_SRC = /<script src=["']([^"']+)["']><\/script>/g;
var URL = /url\([^)]*\)/g;
var URL_ATTR = ['href', 'src', 'action', 'style'];
Expand Down Expand Up @@ -201,43 +201,36 @@ function processImports($, prefix) {
}

function findScriptLocation($) {
var pos = $(POLYMER).last().parent();
var pos = $(POLYFILLS).last().parent();
if (!pos.length) {
pos = $('body');
pos = $('body').last();
}
if (!pos.length) {
pos = $.root();
}
return pos;
}

function insertImport($, storedPosition, importText) {
var pos = storedPosition;
var operation = 'before';
if (!pos.length) {
// before polymer script in <head>
pos = $('head ' + POLYMER).last();
}
function insertImport($, importText) {
// before polymer script in <head>
var pos = $('head ' + POLYFILLS).last();
var operation = 'after';
if (!pos.length) {
// at the bottom of head
pos = $('head').last();
operation = 'append';
}
if (!pos.length) {
// at the bottom of <html>
// at the top of top document
pos = $.root();
operation = 'append';
operation = 'prepend';
}
pos[operation](importText);
}

function insertInlinedImports($, storedPosition, importText) {
var pos = storedPosition;
var operation = 'before';
if (!pos.length) {
pos = $('body').last();
operation = 'prepend';
}
function insertInlinedImports($, importText) {
var pos = $('body').last();
var operation = 'prepend';
if (!pos.length) {
pos = $.root();
}
Expand Down Expand Up @@ -277,8 +270,6 @@ function handleMainDocument() {
read = {};
var $ = readDocument(options.input);
var dir = path.dirname(options.input);
// find the position of the last import's nextSibling
var import_pos = $(IMPORTS).last().next();
processImports($, dir);
if (options.inline) {
inlineSheets($, dir, options.outputDir);
Expand Down Expand Up @@ -326,8 +317,8 @@ function handleMainDocument() {
}

imports_before_polymer = deduplicateImports(imports_before_polymer);
insertImport($, import_pos, imports_before_polymer.join(EOL) + EOL);
insertInlinedImports($, import_pos, output);
insertImport($, imports_before_polymer.join(EOL) + EOL);
insertInlinedImports($, output);
if (!options.csp && options.inline) {
inlineScripts($, options.outputDir);
}
Expand Down

0 comments on commit 8117b13

Please sign in to comment.