Skip to content

Commit

Permalink
Merge pull request #16 from Polymer/ignore-remote-files
Browse files Browse the repository at this point in the history
Ignore remote files during build
  • Loading branch information
FredKSchott committed Aug 1, 2016
2 parents 77da275 + 31edd61 commit da0c799
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
44 changes: 41 additions & 3 deletions src/get-dependencies-from-document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,31 @@
* subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
*/

import {parse as urlParse} from 'url';
import {DocumentDescriptor} from 'hydrolysis';
import {posix as posixPath} from 'path';
import {Node, queryAll, predicates, getAttribute} from 'dom5';
import * as logging from 'plylog';

const logger = logging.getLogger('cli.build.get-dependencies');

export interface DocumentDeps {
imports?: Array<string>;
scripts?: Array<string>;
styles?: Array<string>;
}

/**
* Detects if a url is external by checking it's protocol. Also checks if it
* starts with '//', which can be an alias to the page's current protocol
* in the browser.
*/
function isDependencyExternal(url: string) {
// TODO(fks) 08-01-2016: Add additional check for files on current hostname
// but external to this application root. Ignore them.
return urlParse(url).protocol !== null || url.startsWith('//');
}

function collectScriptsAndStyles(tree: DocumentDescriptor): DocumentDeps {
let scripts: string[] = [];
let styles: string[] = [];
Expand All @@ -40,14 +55,37 @@ function collectScriptsAndStyles(tree: DocumentDescriptor): DocumentDeps {
};
}

/**
* Returns a collection of all local dependencies from a DocumentDescriptor
* object, ignoring any external dependencies. Because HTML imports can have
* script and style dependencies of their own, this will recursively call
* itself down the import tree to collect all dependencies.
*/
export function getDependenciesFromDocument(descriptor: DocumentDescriptor, dir: string): DocumentDeps {
let allHtmlDeps: string[] = [];
let allScriptDeps = new Set<string>();
let allStyleDeps = new Set<string>();

let deps: DocumentDeps = collectScriptsAndStyles(descriptor);
deps.scripts.forEach((s) => allScriptDeps.add(posixPath.join(dir, s)));
deps.styles.forEach((s) => allStyleDeps.add(posixPath.join(dir, s)));

// Collect all script dependencies
deps.scripts.forEach((scriptDep) => {
if (isDependencyExternal(scriptDep)) {
logger.debug('external dependency ignored', {dep: scriptDep});
return;
}
allScriptDeps.add(posixPath.join(dir, scriptDep));
});

// Collect all style dependencies
deps.styles.forEach((styleDep) => {
if (isDependencyExternal(styleDep)) {
logger.debug('external dependency ignored', {dep: styleDep});
return;
}
allStyleDeps.add(posixPath.join(dir, styleDep));
});

// Recursively collects and analyzes all HTML imports and their dependencies
if (descriptor.imports) {
let queue = descriptor.imports.slice();
let next: DocumentDescriptor;
Expand Down
2 changes: 2 additions & 0 deletions test/test-project/shell.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<link rel="import" href="bower_components/dep.html">
<!-- remote dependencies should be ignored during build -->
<link rel="stylesheet" href="https://example.com/style.min.css">

<div id="A"></div>

Expand Down

0 comments on commit da0c799

Please sign in to comment.