Skip to content
This repository has been archived by the owner on Dec 7, 2021. It is now read-only.

Commit

Permalink
Merge branch 'master' into build-flag-bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
FredKSchott authored Dec 15, 2016
2 parents 8a6fa24 + e5ae7a7 commit 840df15
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 41 deletions.
12 changes: 9 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@

## Upcoming (Potentially Breaking)

- Added `analyze` command
- Upgrade `polymer-build` to `v0.6.0`, which means the build command will now use the new [`polymer-analyzer`](https://github.com/Polymer/polymer-analyzer)! See [the polymer-build changelog](https://github.com/Polymer/polymer-build/blob/v0.5.0/CHANGELOG.md) for more information.
## v0.18.0-alpha.7

- **Added `analyze` command:** Generates a JSON blob of metadata about your element(s). Useful for tooling and analysis.
- **Added `install` command:** Installs "variants" defined in your `bower.json`.
- Upgrade `polyserve` to `v0.6.0-prerelease.6` to handle serving variants
- Upgrade `web-component-tester` to `6.0.0-prerelease.1` to handle testing variants
- Upgrade `polymer-build` to `v0.6.0-alpha.1`, which includes an upgrade to the new [`polymer-analyzer`](https://github.com/Polymer/polymer-analyzer).
- `build`: Build now generated a single build when run. Run `polymer help build` for a list of command-line arguments you can use to customize your build (ex: add the `--bundle` to create a bundled build). Multiple named builds will be reintroduced soon.
- `build`: Rename the `--include-dependencies` flag to `--extra-dependencies`
- `build`: css is now minified
- `build`: Lots of bug fixes due to the new polymer-build library and analyzer.
- `polymer.json`: Rename the `includeDependencies` & `sourceGlobs` fields to `extraDependencies` & `sources`, respectively
- Added an `install` command, which can install "variants" of Bower dependencies. TODO(justinfagnani): document variants.
- Added support for v7.x of Node.js, dropped support for v5.x. Please move to an [actively maintained version of Node.js](https://github.com/nodejs/LTS) for the best experience.
- Upgrade [web-component-tester 6.0](https://github.com/Polymer/web-component-tester/blob/master/CHANGELOG.md) which brings a number of breaking changes to the `test` command.

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "polymer-cli",
"version": "0.18.0-alpha.6",
"version": "0.18.0-alpha.7",
"description": "A commandline tool for Polymer projects",
"main": "lib/polymer-cli.js",
"engines": {
Expand Down
8 changes: 7 additions & 1 deletion src/build/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {dest} from 'vinyl-fs';
import mergeStream = require('merge-stream');
import {PolymerProject, addServiceWorker} from 'polymer-build';

import {JSOptimizeStream, CSSOptimizeStream, HTMLOptimizeStream} from './optimize-streams';
import {InlineCSSOptimizeStream, JSOptimizeStream, CSSOptimizeStream, HTMLOptimizeStream} from './optimize-streams';

import {ProjectConfig} from 'polymer-project-config';
import {PrefetchTransform} from './prefetch';
Expand Down Expand Up @@ -70,6 +70,9 @@ export async function build(
.pipe(polymerProject.splitHtml())
.pipe(gulpif(/\.js$/, new JSOptimizeStream(optimizeOptions.js)))
.pipe(gulpif(/\.css$/, new CSSOptimizeStream(optimizeOptions.css)))
// TODO(fks): Remove this InlineCSSOptimizeStream stream once CSS
// is properly being isolated by splitHtml() & rejoinHtml().
.pipe(gulpif(/\.html$/, new InlineCSSOptimizeStream(optimizeOptions.css)))
.pipe(gulpif(/\.html$/, new HTMLOptimizeStream(optimizeOptions.html)))
.pipe(polymerProject.rejoinHtml());

Expand All @@ -79,6 +82,9 @@ export async function build(
.pipe(polymerProject.splitHtml())
.pipe(gulpif(/\.js$/, new JSOptimizeStream(optimizeOptions.js)))
.pipe(gulpif(/\.css$/, new CSSOptimizeStream(optimizeOptions.css)))
// TODO(fks): Remove this InlineCSSOptimizeStream stream once CSS
// is properly being isolated by splitHtml() & rejoinHtml().
.pipe(gulpif(/\.html$/, new InlineCSSOptimizeStream(optimizeOptions.css)))
.pipe(gulpif(/\.html$/, new HTMLOptimizeStream(optimizeOptions.html)))
.pipe(polymerProject.rejoinHtml());

Expand Down
50 changes: 32 additions & 18 deletions src/build/optimize-streams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* http://polymer.github.io/PATENTS.txt
*/

import {css as cssSlam} from 'css-slam';
import * as cssSlam from 'css-slam';
import {minify as htmlMinify, Options as HTMLMinifierOptions} from 'html-minifier';
import * as logging from 'plylog';
import {Transform} from 'stream';
Expand All @@ -34,32 +34,34 @@ export type CSSOptimizeOptions = {
/**
* GenericOptimizeStream is a generic optimization stream. It can be extended
* to create a new kind of specific file-type optimizer, or it can be used
* directly to create an ad-hoc optimization stream for certain types of files.
* directly to create an ad-hoc optimization stream for different libraries.
* If the transform library throws an exception when run, the file will pass
* through unaffected.
*/
export class GenericOptimizeStream extends Transform {
validExtension: string;
optimizer: (content: string, options: any) => string;
optimizerName: string;
optimizerOptions: any;

constructor(
validExtension: string,
optimizerName: string,
optimizer: (content: string, optimizerOptions: any) => string,
optimizerOptions: any) {
super({objectMode: true});
this.optimizer = optimizer;
this.validExtension = validExtension;
this.optimizerName = optimizerName;
this.optimizerOptions = optimizerOptions || {};
}

_transform(file: File, _encoding: string, callback: FileCB): void {
if (file.contents && file.path.endsWith(`${this.validExtension}`)) {
if (file.contents) {
try {
let contents = file.contents.toString();
contents = this.optimizer(contents, this.optimizerOptions);
file.contents = new Buffer(contents);
} catch (error) {
logger.warn(
`Unable to optimize ${this.validExtension} file ${file.path}`,
`${this.optimizerName}: Unable to optimize ${file.path}`,
{err: error.message || error});
}
}
Expand All @@ -69,8 +71,6 @@ export class GenericOptimizeStream extends Transform {

/**
* JSOptimizeStream optimizes JS files that pass through it (via uglify).
* If a file is not a `.js` file or if uglify throws an exception when run,
* the file will pass through unaffected.
*/
export class JSOptimizeStream extends GenericOptimizeStream {
constructor(options: UglifyOptions) {
Expand All @@ -82,19 +82,16 @@ export class JSOptimizeStream extends GenericOptimizeStream {
};
// We automatically add the fromString option because it is required.
let uglifyOptions = Object.assign({fromString: true}, options);
super('.js', uglifyOptimizer, uglifyOptions);
super('uglify-js', uglifyOptimizer, uglifyOptions);
}
}


/**
* CSSOptimizeStream optimizes CSS files that pass through it (via css-slam).
* If a file is not a `.css` file or if css-slam throws an exception when run,
* the file will pass through unaffected.
* CSSOptimizeStream optimizes CSS that pass through it (via css-slam).
*/
export class CSSOptimizeStream extends GenericOptimizeStream {
constructor(options: CSSOptimizeOptions) {
super('.css', cssSlam, options);
super('css-slam', cssSlam.css, options);
}

_transform(file: File, encoding: string, callback: FileCB): void {
Expand All @@ -107,14 +104,31 @@ export class CSSOptimizeStream extends GenericOptimizeStream {
}
}

/**
* InlineCSSOptimizeStream optimizes inlined CSS (found in HTML files) that
* passes through it (via css-slam).
*/
export class InlineCSSOptimizeStream extends GenericOptimizeStream {
constructor(options: CSSOptimizeOptions) {
super('css-slam', cssSlam.html, options);
}

_transform(file: File, encoding: string, callback: FileCB): void {
// css-slam will only be run if the `stripWhitespace` option is true.
// Because css-slam itself doesn't accept any options, we handle the
// option here before transforming.
if (this.optimizerOptions.stripWhitespace) {
super._transform(file, encoding, callback);
}
}
}

/**
* HTMLOptimizeStream optimizes HTML files that pass through it
* (via html-minifier). If a file is not a `.html` file or if html-minifier
* throws an exception when run, the file will pass through unaffected.
* (via html-minifier).
*/
export class HTMLOptimizeStream extends GenericOptimizeStream {
constructor(options: HTMLMinifierOptions) {
super('.html', htmlMinify, options);
super('html-minify', htmlMinify, options);
}
}
67 changes: 49 additions & 18 deletions test/unit/build/optimize_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const optimizeStreams = require('../../../lib/build/optimize-streams');

const JSOptimizeStream = optimizeStreams.JSOptimizeStream;
const CSSOptimizeStream = optimizeStreams.CSSOptimizeStream;
const InlineCSSOptimizeStream = optimizeStreams.InlineCSSOptimizeStream;
const HTMLOptimizeStream = optimizeStreams.HTMLOptimizeStream;

suite('optimize-streams', () => {
Expand All @@ -27,24 +28,6 @@ suite('optimize-streams', () => {
stream.on('error', cb);
}

test('css', (done) => {
let stream = vfs.src([
{
path: 'foo.css',
contents: '/* comment */ selector { property: value; }',
},
]);
let op = stream.pipe(new CSSOptimizeStream({stripWhitespace: true}));
assert.notEqual(stream, op, 'stream should be wrapped');
testStream(op, (error, f) => {
if (error) {
return done(error);
}
assert.equal(f.contents.toString(), 'selector{property:value;}');
done();
});
});

test('js', (done) => {
let stream = vfs.src([
{
Expand Down Expand Up @@ -100,4 +83,52 @@ suite('optimize-streams', () => {
done();
});
});

test('css', (done) => {
let stream = vfs.src([
{
path: 'foo.css',
contents: '/* comment */ selector { property: value; }',
},
]);
let op = stream.pipe(new CSSOptimizeStream({stripWhitespace: true}));
testStream(op, (error, f) => {
if (error) {
return done(error);
}
assert.equal(f.contents.toString(), 'selector{property:value;}');
done();
});
});

test('inline css', (done) => {
let expected =`<style>foo{background:blue;}</style>`;
let stream = vfs.src([
{
path: 'foo.html',
contents: `
<!doctype html>
<html>
<head>
<style>
foo {
background: blue;
}
</style>
</head>
<body></body>
</html>
`,
},
], {cwdbase: true});
let op = stream.pipe(new InlineCSSOptimizeStream({stripWhitespace: true}));
testStream(op, (error, f) => {
if (error) {
return done(error);
}
assert.include(f.contents.toString(), expected);
done();
});
});

});

0 comments on commit 840df15

Please sign in to comment.