Skip to content

Commit

Permalink
Use a parser based html minification
Browse files Browse the repository at this point in the history
only test builds in travis

Massive speed up for browser testing

Lint the document processor
  • Loading branch information
dfreedm committed Mar 8, 2016
1 parent 8066919 commit 0536e35
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 12 deletions.
6 changes: 4 additions & 2 deletions .travis.yml
Expand Up @@ -12,12 +12,14 @@ addons:
- google-chrome-stable
- g++-4.8
before_script:
- npm install -g bower
- npm install -g bower gulp-cli
- bower install
- gulp lint
- gulp switch
script:
- xvfb-run wct
- "if [ \"${TRAVIS_PULL_REQUEST}\" = \"false\" ]; then wct -s 'default'; fi"
- "if [ \"${TRAVIS_PULL_REQUEST}\" = \"false\" ]; then wct -s default; fi"

env:
global:
- secure: eFrp9xwSYG579rAR9/XyXYKh+UtIQJ1xS5q5PABu4ndYFckdJb8o3m7oXqRSikbiPWbCSd3Fkd6+ZKXlcQFdjJ+nx9gFitGthtH93qkvZCO3XhWEEBPj65igIo3hrRSOB6dIWyiZcnoH9IXLLQtKXY9uL1NCqCcyVHg1omPKr9w=
Expand Down
7 changes: 6 additions & 1 deletion bower.json
Expand Up @@ -10,7 +10,12 @@
"ignore": [
"/.*",
"/test/",
"gen-changelog.sh"
"/util/",
"/explainer/",
"gulpfile.js",
"PRIMER.md",
"CONTRIBUTING.md",
"CHANGELOG.md"
],
"authors": [
"The Polymer Authors (http://polymer.github.io/AUTHORS.txt)"
Expand Down
15 changes: 6 additions & 9 deletions gulpfile.js
Expand Up @@ -24,6 +24,8 @@ var eslint = require('gulp-eslint');

var path = require('path');

var minimalDocument = require('./util/minimalDocument');

var micro = "polymer-micro.html";
var mini = "polymer-mini.html";
var max = "polymer.html";
Expand All @@ -36,17 +38,12 @@ var distMax = path.join(workdir, max);
var pkg = require('./package.json');

var cleanupPipe = lazypipe()
// Reduce script tags
.pipe(replace, /<\/script>\s*<script>/g, '\n\n')
// Add real version number
.pipe(replace, /(Polymer.version = )'master'/, '$1"' + pkg.version + '"')
// remove leading whitespace and comments
.pipe(polyclean.leftAlignJs)
// remove html wrapper
.pipe(replace, '<html><head>', '')
.pipe(replace, '<meta charset="UTF-8">', '')
.pipe(replace, '</head><body><div hidden="" by-vulcanize="">', '')
.pipe(replace, '</div></body></html>', '')
.pipe(minimalDocument)
// Add real version number
.pipe(replace, /(Polymer.version = )'master'/, '$1"' + pkg.version + '"')
;

function vulcanizeWithExcludes(target, excludes) {
Expand Down Expand Up @@ -127,7 +124,7 @@ gulp.task('release', function(cb) {
});

gulp.task('lint', function() {
return gulp.src(['src/**/*.html', 'test/unit/*.html'])
return gulp.src(['src/**/*.html', 'test/unit/*.html', 'util/*.js'])
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError());
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -19,6 +19,7 @@
"lazypipe": "^1.0.1",
"polyclean": "^1.2.0",
"run-sequence": "^1.1.0",
"through2": "^2.0.0",
"web-component-tester": "^4"
},
"scripts": {
Expand Down
5 changes: 5 additions & 0 deletions util/.eslintrc.json
@@ -0,0 +1,5 @@
{
"env": {
"node": true
}
}
File renamed without changes.
78 changes: 78 additions & 0 deletions util/minimalDocument.js
@@ -0,0 +1,78 @@
/**
* @license
* Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
* This code may only be used under the BSD style license found at http:polymer.github.io/LICENSE.txt
* The complete set of authors may be found at http:polymer.github.io/AUTHORS.txt
* The complete set of contributors may be found at http:polymer.github.io/CONTRIBUTORS.txt
* Code distributed by Google as part of the polymer project is also
* subject to an additional IP rights grant found at http:polymer.github.io/PATENTS.txt
*/

// jshint node: true
'use strict';

var dom5 = require('dom5');
var through2 = require('through2');

var p = dom5.predicates;

function isBlankTextNode(node) {
return node && dom5.isTextNode(node) && !/\S/.test(dom5.getTextContent(node));
}

function replaceWithChildren(node) {
if (!node) {
return;
}
var parent = node.parentNode;
var idx = parent.childNodes.indexOf(node);
var children = node.childNodes;
children.forEach(function(n) {
n.parentNode = parent;
});
var til = idx + 1;
var next = parent.childNodes[til];
// remove newline text node as well
while (isBlankTextNode(next)) {
til++;
next = parent.childNodes[til];
}
parent.childNodes = parent.childNodes.slice(0, idx).concat(children, parent.childNodes.slice(til));
}

module.exports = function() {
return through2.obj(function(file, enc, cb) {
var doc = dom5.parse(String(file.contents));
var head = dom5.query(doc, p.hasTagName('head'));
var body = dom5.query(doc, p.hasTagName('body'));
var vulc = dom5.query(body, p.AND(p.hasTagName('div'), p.hasAttr('by-vulcanize'), p.hasAttr('hidden')));
var charset = dom5.query(doc, p.AND(p.hasTagName('meta'), p.hasAttrValue('charset', 'UTF-8')));

if (charset) {
dom5.remove(charset);
}

replaceWithChildren(head);
replaceWithChildren(vulc);
replaceWithChildren(body);

var scripts = dom5.queryAll(doc, p.hasTagName('script'));
var collector = scripts[0];
var contents = [];
for (var i = 0, s; i < scripts.length; i++) {
s = scripts[i];
if (i > 0) {
dom5.remove(s);
}
contents.push(dom5.getTextContent(s));
}
dom5.setTextContent(collector, contents.join(''));

var html = dom5.query(doc, p.hasTagName('html'));
replaceWithChildren(html);

file.contents = new Buffer(dom5.serialize(doc));

cb(null, file);
});
};

0 comments on commit 0536e35

Please sign in to comment.