Skip to content

Commit

Permalink
Use Java version of Google Closure Compiler (#12800)
Browse files Browse the repository at this point in the history
* makes closure compiler threaded

* Dans PR with a closure compiler java version

* Remove unused dep

* Pin GCC

* Prettier

* Nit rename

* Fix error handling

* Name plugins consistently

* Fix lint

* Maybe this works?

* or this

* AppVeyor

* Fix lint
  • Loading branch information
gaearon committed May 14, 2018
1 parent 37d12e2 commit 7254203
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ jobs:
build:

docker:
- image: circleci/node:8
- image: circleci/openjdk:8-jdk-node-browsers

environment:
TZ: /usr/share/zoneinfo/America/Los_Angeles
Expand Down
4 changes: 2 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ init:
- git config --global core.autocrlf input

environment:
matrix:
- nodejs_version: 8
nodejs_version: 8
JAVA_HOME: C:\Program Files\Java\jdk1.8.0

# Finish on first failed build
matrix:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"git-branch": "^0.3.0",
"glob": "^6.0.4",
"glob-stream": "^6.1.0",
"google-closure-compiler": "20180506.0.0",
"gzip-js": "~0.3.2",
"gzip-size": "^3.0.0",
"jasmine-check": "^1.0.0-rc.0",
Expand All @@ -84,7 +85,6 @@
"rimraf": "^2.6.1",
"rollup": "^0.52.1",
"rollup-plugin-babel": "^3.0.1",
"rollup-plugin-closure-compiler-js": "^1.0.6",
"rollup-plugin-commonjs": "^8.2.6",
"rollup-plugin-node-resolve": "^2.1.1",
"rollup-plugin-prettier": "^0.3.0",
Expand Down
28 changes: 14 additions & 14 deletions scripts/rollup/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const {rollup} = require('rollup');
const babel = require('rollup-plugin-babel');
const closure = require('rollup-plugin-closure-compiler-js');
const closure = require('./plugins/closure-plugin');
const commonjs = require('rollup-plugin-commonjs');
const prettier = require('rollup-plugin-prettier');
const replace = require('rollup-plugin-replace');
Expand Down Expand Up @@ -62,15 +62,15 @@ const errorCodeOpts = {
};

const closureOptions = {
compilationLevel: 'SIMPLE',
languageIn: 'ECMASCRIPT5_STRICT',
languageOut: 'ECMASCRIPT5_STRICT',
compilation_level: 'SIMPLE',
language_in: 'ECMASCRIPT5_STRICT',
language_out: 'ECMASCRIPT5_STRICT',
env: 'CUSTOM',
warningLevel: 'QUIET',
applyInputSourceMaps: false,
useTypesForOptimization: false,
processCommonJsModules: false,
rewritePolyfills: false,
warning_level: 'QUIET',
apply_input_source_maps: false,
use_types_for_optimization: false,
process_common_js_modules: false,
rewrite_polyfills: false,
};

function getBabelConfig(updateBabelOptions, bundleType, filename) {
Expand Down Expand Up @@ -264,7 +264,7 @@ function getPlugins(
Object.assign({}, closureOptions, {
// Don't let it create global variables in the browser.
// https://github.com/facebook/react/issues/10909
assumeFunctionWrapper: !isInGlobalScope,
assume_function_wrapper: !isInGlobalScope,
// Works because `google-closure-compiler-js` is forked in Yarn lockfile.
// We can remove this if GCC merges my PR:
// https://github.com/google/closure-compiler/pull/2707
Expand Down Expand Up @@ -460,9 +460,9 @@ function handleRollupError(error) {
console.error(
`\x1b[31m-- ${error.code}${error.plugin ? ` (${error.plugin})` : ''} --`
);
console.error(error.message);
const {file, line, column} = error.loc;
if (file) {
console.error(error.stack);
if (error.loc && error.loc.file) {
const {file, line, column} = error.loc;
// This looks like an error from Rollup, e.g. missing export.
// We'll use the accurate line numbers provided by Rollup but
// use Babel code frame because it looks nicer.
Expand All @@ -473,7 +473,7 @@ function handleRollupError(error) {
highlightCode: true,
});
console.error(frame);
} else {
} else if (error.codeFrame) {
// This looks like an error from a plugin (e.g. Babel).
// In this case we'll resort to displaying the provided code frame
// because we can't be sure the reported location is accurate.
Expand Down
35 changes: 35 additions & 0 deletions scripts/rollup/plugins/closure-plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';

const ClosureCompiler = require('google-closure-compiler').compiler;
const {promisify} = require('util');
const fs = require('fs');
const tmp = require('tmp');
const writeFileAsync = promisify(fs.writeFile);

function compile(flags) {
return new Promise((resolve, reject) => {
const closureCompiler = new ClosureCompiler(flags);
closureCompiler.run(function(exitCode, stdOut, stdErr) {
if (!stdErr) {
resolve(stdOut);
} else {
reject(new Error(stdErr));
}
});
});
}

module.exports = function closure(flags = {}) {
return {
name: 'scripts/rollup/plugins/closure-plugin',
async transformBundle(code) {
const inputFile = tmp.fileSync();
const tempPath = inputFile.name;
flags = Object.assign({}, flags, {js: tempPath});
await writeFileAsync(tempPath, code, 'utf8');
const compiledCode = await compile(flags);
inputFile.removeCallback();
return {code: compiledCode};
},
};
};
1 change: 1 addition & 0 deletions scripts/rollup/plugins/sizes-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const gzip = require('gzip-size');

module.exports = function sizes(options) {
return {
name: 'scripts/rollup/plugins/sizes-plugin',
ongenerate(bundle, obj) {
const size = Buffer.byteLength(obj.code);
const gzipSize = gzip.sync(obj.code);
Expand Down
1 change: 1 addition & 0 deletions scripts/rollup/plugins/use-forks-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ function useForks(forks) {
);
});
return {
name: 'scripts/rollup/plugins/use-forks-plugin',
resolveId(importee, importer) {
if (!importer || !importee) {
return null;
Expand Down
43 changes: 16 additions & 27 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2548,13 +2548,13 @@ glogg@^1.0.0:
dependencies:
sparkles "^1.0.0"

google-closure-compiler-js@>20170000:
version "20180402.0.0"
resolved "https://registry.yarnpkg.com/google-closure-compiler-js/-/google-closure-compiler-js-20180402.0.0.tgz#b90ee11c597030b90ed1c6a054dd728aba81ab2d"
google-closure-compiler@20180506.0.0:
version "20180506.0.0"
resolved "https://registry.yarnpkg.com/google-closure-compiler/-/google-closure-compiler-20180506.0.0.tgz#f59cc34dbf8c9a4f48fba3ebb2cf098d25e345ab"
dependencies:
minimist "^1.2.0"
chalk "^1.0.0"
vinyl "^2.0.1"
webpack-core "^0.6.8"
vinyl-sourcemaps-apply "^0.2.0"

graceful-fs@^4.1.11, graceful-fs@^4.1.2, graceful-fs@^4.1.4:
version "4.1.11"
Expand Down Expand Up @@ -4814,12 +4814,6 @@ rollup-plugin-babel@^3.0.1:
dependencies:
rollup-pluginutils "^1.5.0"

rollup-plugin-closure-compiler-js@^1.0.6:
version "1.0.6"
resolved "https://registry.yarnpkg.com/rollup-plugin-closure-compiler-js/-/rollup-plugin-closure-compiler-js-1.0.6.tgz#58e3e31297ad1a532d9114108bc06f2756d72c3d"
dependencies:
google-closure-compiler-js ">20170000"

rollup-plugin-commonjs@^8.2.6:
version "8.2.6"
resolved "https://registry.yarnpkg.com/rollup-plugin-commonjs/-/rollup-plugin-commonjs-8.2.6.tgz#27e5b9069ff94005bb01e01bb46a1e4873784677"
Expand Down Expand Up @@ -5007,10 +5001,6 @@ sntp@2.x.x:
dependencies:
hoek "4.x.x"

source-list-map@~0.1.7:
version "0.1.8"
resolved "https://registry.yarnpkg.com/source-list-map/-/source-list-map-0.1.8.tgz#c550b2ab5427f6b3f21f5afead88c4f5587b2106"

source-map-support@^0.2.10:
version "0.2.10"
resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.2.10.tgz#ea5a3900a1c1cb25096a0ae8cc5c2b4b10ded3dc"
Expand All @@ -5035,7 +5025,7 @@ source-map@0.1.32:
dependencies:
amdefine ">=0.0.4"

source-map@^0.4.4, source-map@~0.4.0, source-map@~0.4.1, source-map@~0.4.2:
source-map@^0.4.4, source-map@~0.4.0, source-map@~0.4.2:
version "0.4.4"
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.4.4.tgz#eba4f5da9c0dc999de68032d8b4f76173652036b"
dependencies:
Expand All @@ -5045,14 +5035,14 @@ source-map@^0.5.0, source-map@^0.5.3, source-map@^0.5.6, source-map@~0.5.0, sour
version "0.5.6"
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.5.6.tgz#75ce38f52bf0733c5a7f0c118d81334a2bb5f412"

source-map@^0.5.1, source-map@~0.5.6:
version "0.5.7"
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.5.7.tgz#8a039d2d1021d22d1ea14c80d8ea468ba2ef3fcc"

source-map@^0.6.0:
version "0.6.1"
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263"

source-map@~0.5.6:
version "0.5.7"
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.5.7.tgz#8a039d2d1021d22d1ea14c80d8ea468ba2ef3fcc"

sparkles@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/sparkles/-/sparkles-1.0.0.tgz#1acbbfb592436d10bbe8f785b7cc6f82815012c3"
Expand Down Expand Up @@ -5546,6 +5536,12 @@ verror@1.3.6:
dependencies:
extsprintf "1.0.2"

vinyl-sourcemaps-apply@^0.2.0:
version "0.2.1"
resolved "https://registry.yarnpkg.com/vinyl-sourcemaps-apply/-/vinyl-sourcemaps-apply-0.2.1.tgz#ab6549d61d172c2b1b87be5c508d239c8ef87705"
dependencies:
source-map "^0.5.1"

vinyl@^0.5.0:
version "0.5.3"
resolved "https://registry.yarnpkg.com/vinyl/-/vinyl-0.5.3.tgz#b0455b38fc5e0cf30d4325132e461970c2091cde"
Expand Down Expand Up @@ -5591,13 +5587,6 @@ webidl-conversions@^4.0.1, webidl-conversions@^4.0.2:
version "4.0.2"
resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-4.0.2.tgz#a855980b1f0b6b359ba1d5d9fb39ae941faa63ad"

webpack-core@^0.6.8:
version "0.6.9"
resolved "https://registry.yarnpkg.com/webpack-core/-/webpack-core-0.6.9.tgz#fc571588c8558da77be9efb6debdc5a3b172bdc2"
dependencies:
source-list-map "~0.1.7"
source-map "~0.4.1"

whatwg-encoding@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/whatwg-encoding/-/whatwg-encoding-1.0.1.tgz#3c6c451a198ee7aec55b1ec61d0920c67801a5f4"
Expand Down

0 comments on commit 7254203

Please sign in to comment.