Skip to content

Commit 073e1fb

Browse files
Tests (#2)
Added a few simple tests to ensure minification was working and options are correctly set for Closure Compiler given Rollup configuration.
1 parent a34b12a commit 073e1fb

File tree

8 files changed

+148
-13
lines changed

8 files changed

+148
-13
lines changed

dist/index.js

Lines changed: 2 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@ampproject/rollup-plugin-closure-compiler",
3-
"version": "0.3.0",
3+
"version": "0.3.1",
44
"description": "Rollup + Google Closure Compiler",
55
"main": "dist/index.js",
66
"repository": {
@@ -17,6 +17,7 @@
1717
],
1818
"license": "Apache-2.0",
1919
"scripts": {
20+
"pretest": "yarn build",
2021
"test": "ava test/*.js",
2122
"build": "rimraf dist && tsc -p tsconfig.json"
2223
},
@@ -32,7 +33,7 @@
3233
"lint-staged": "^7.2.0",
3334
"prettier": "^1.13.5",
3435
"rimraf": "^2.6.2",
35-
"rollup": "^0.61.1",
36+
"rollup": "^0.62.0",
3637
"sourcemap-validator": "^1.1.0",
3738
"typescript": "^2.9.2"
3839
},
@@ -44,7 +45,8 @@
4445
},
4546
"husky": {
4647
"hooks": {
47-
"pre-commit": "lint-staged"
48+
"pre-commit": "lint-staged",
49+
"pre-push": "yarn test"
4850
}
4951
}
5052
}

src/index.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { sync } from 'temp-write';
1919
import { readFileSync } from 'fs';
2020
import { OutputOptions, RawSourceMap, Plugin } from 'rollup';
2121

22-
function defaultCompileOptions(outputOptions: OutputOptions): CompileOptions {
22+
export function defaultCompileOptions(outputOptions: OutputOptions): CompileOptions {
2323
// Defaults for Rollup Projects are slightly different than Closure Compiler defaults.
2424
// - Users of Rollup tend to transpile their code before handing it to a minifier,
2525
// so no transpile is default.
@@ -77,10 +77,7 @@ export default function closureCompiler(compileOptions: CompileOptions = {}): Pl
7777
return { code: stdOut, map: sourceMap };
7878
},
7979
(error: Error) => {
80-
// The TypeScript definition for Rollup is not correct.
81-
// `this` has context exposing helper methods like 'error'.
82-
// See: https://github.com/rollup/rollup/wiki/Plugins#plugin-context
83-
this.error(error);
80+
throw error;
8481
},
8582
);
8683
},

test/input/es2015.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,17 @@ class Foo {
2626

2727
const x = new Foo('bar');
2828
console.log(x.baz);
29+
30+
class Bar extends Foo {
31+
constructor(happy) {
32+
super('bar');
33+
this.happy = happy;
34+
}
35+
36+
get happyness() {
37+
return this.happy ? 'quite happy' : 'sad';
38+
}
39+
}
40+
41+
const barInstance = new Bar(true);
42+
console.log(barInstance.baz, barInstance.happyness);

test/input/es5.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Copyright 2018 The AMP HTML Authors. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS-IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
function greeting(name) {
18+
return 'hello ' + name;
19+
}
20+
21+
console.log(greeting('superuser'));

test/minifies.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* Copyright 2018 The AMP HTML Authors. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS-IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import test from 'ava';
18+
import compiler from '../dist/index';
19+
import { rollup } from 'rollup';
20+
import { readFileSync } from 'fs';
21+
import { join } from 'path';
22+
23+
test('es2015 does minify', async t => {
24+
const source = readFileSync(join('test/input/es2015.js'), 'utf8');
25+
const compilerBundle = await rollup({
26+
input: 'test/input/es2015.js',
27+
plugins: [
28+
compiler(),
29+
],
30+
});
31+
32+
const compilerResults = await compilerBundle.generate({
33+
format: 'es',
34+
sourcemap: true,
35+
});
36+
37+
t.truthy(compilerResults.code.length < source.length);
38+
});
39+
40+
test('es5 does minify', async t => {
41+
const source = readFileSync(join('test/input/es5.js'), 'utf8');
42+
const compilerBundle = await rollup({
43+
input: 'test/input/es5.js',
44+
plugins: [
45+
compiler(),
46+
],
47+
});
48+
49+
const compilerResults = await compilerBundle.generate({
50+
format: 'es',
51+
sourcemap: true,
52+
});
53+
54+
t.truthy(compilerResults.code.length < source.length);
55+
});

test/rollup-config-to-flags.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Copyright 2018 The AMP HTML Authors. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS-IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import test from 'ava';
18+
import {defaultCompileOptions} from '../dist/index';
19+
import { readFileSync } from 'fs';
20+
21+
test('with no rollup configuration defaults are valid', t => {
22+
const options = defaultCompileOptions({});
23+
t.deepEqual(options, {
24+
language_out: 'NO_TRANSPILE',
25+
assume_function_wrapper: false,
26+
warning_level: 'QUIET',
27+
});
28+
});
29+
30+
test('when rollup configuration specifies format iife with a name, an extern is generated', t => {
31+
const options = defaultCompileOptions({
32+
format: 'iife',
33+
name: 'Wrapper'
34+
});
35+
36+
t.not(options.externs, undefined);
37+
38+
const externs = readFileSync(options.externs, 'utf8');
39+
t.is(externs, `function Wrapper(){}`);
40+
});
41+
42+
test('when rollup configuration specifies format es, assume_function_wrapper is true', t => {
43+
const options = defaultCompileOptions({
44+
format: 'es',
45+
});
46+
47+
t.true(options.assume_function_wrapper);
48+
});

0 commit comments

Comments
 (0)