Skip to content

Commit

Permalink
Replace Uglify with GCC
Browse files Browse the repository at this point in the history
  • Loading branch information
gaearon committed Nov 8, 2017
1 parent c932885 commit 6c43c6b
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 116 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
"rollup-plugin-commonjs": "^8.2.6",
"rollup-plugin-inject": "^2.0.0",
"rollup-plugin-node-resolve": "^2.0.0",
"rollup-plugin-prettier": "^0.3.0",
"rollup-plugin-replace": "^1.1.1",
"rollup-plugin-uglify": "^1.0.1",
"run-sequence": "^1.1.4",
Expand Down
152 changes: 45 additions & 107 deletions scripts/rollup/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

const rollup = require('rollup').rollup;
const babel = require('rollup-plugin-babel');
const closure = require('rollup-plugin-closure-compiler-js');
const commonjs = require('rollup-plugin-commonjs');
const alias = require('rollup-plugin-alias');
const uglify = require('rollup-plugin-uglify');
const prettier = require('rollup-plugin-prettier');
const replace = require('rollup-plugin-replace');
const chalk = require('chalk');
const join = require('path').join;
Expand All @@ -24,7 +25,6 @@ const syncReactNativeRT = require('./sync').syncReactNativeRT;
const syncReactNativeCS = require('./sync').syncReactNativeCS;
const Packaging = require('./packaging');
const Header = require('./header');
const closure = require('rollup-plugin-closure-compiler-js');

const UMD_DEV = Bundles.bundleTypes.UMD_DEV;
const UMD_PROD = Bundles.bundleTypes.UMD_PROD;
Expand All @@ -51,31 +51,16 @@ const errorCodeOpts = {
errorMapFilePath: 'scripts/error-codes/codes.json',
};

function getHeaderSanityCheck(bundleType, globalName) {
switch (bundleType) {
case FB_DEV:
case FB_PROD:
case RN_DEV:
case RN_PROD:
let hasteFinalName = globalName;
switch (bundleType) {
case FB_DEV:
case RN_DEV:
hasteFinalName += '-dev';
break;
case FB_PROD:
case RN_PROD:
hasteFinalName += '-prod';
break;
}
return hasteFinalName;
case UMD_DEV:
case UMD_PROD:
return reactVersion;
default:
return null;
}
}
const closureOptions = {
compilationLevel: 'SIMPLE',
languageIn: 'ECMASCRIPT5_STRICT',
languageOut: 'ECMASCRIPT5_STRICT',
env: 'CUSTOM',
warningLevel: 'QUIET',
applyInputSourceMaps: false,
useTypesForOptimization: false,
processCommonJsModules: false,
};

function getBanner(bundleType, globalName, filename, moduleType) {
if (moduleType === RECONCILER) {
Expand Down Expand Up @@ -323,55 +308,6 @@ function getFilename(name, globalName, bundleType) {
}
}

function getUglifyConfig(configs) {
var mangle = configs.mangle;
var preserveVersionHeader = configs.preserveVersionHeader;
var removeComments = configs.removeComments;
var headerSanityCheck = configs.headerSanityCheck;
return {
warnings: false,
compress: {
screw_ie8: true,
dead_code: true,
unused: true,
drop_debugger: true,
// we have a string literal <script> that we don't want to evaluate
// for FB prod bundles (where we disable mangling)
evaluate: mangle,
booleans: true,
// Our www inline transform combined with Jest resetModules is confused
// in some rare cases unless we keep all requires at the top:
hoist_funs: mangle,
},
output: {
beautify: !mangle,
comments(node, comment) {
if (preserveVersionHeader && comment.pos === 0 && comment.col === 0) {
// Keep the very first comment (the bundle header) in prod bundles.
if (
headerSanityCheck &&
comment.value.indexOf(headerSanityCheck) === -1
) {
// Sanity check: this doesn't look like the bundle header!
throw new Error(
'Expected the first comment to be the file header but got: ' +
comment.value
);
}
return true;
}
return !removeComments;
},
},
mangle: mangle
? {
toplevel: true,
screw_ie8: true,
}
: false,
};
}

// FB uses require('React') instead of require('react').
// We can't set up a forwarding module due to case sensitivity issues.
function rewriteFBReactImport() {
Expand Down Expand Up @@ -431,7 +367,6 @@ function getPlugins(
stripUseStrict(),
].filter(Boolean);

const headerSanityCheck = getHeaderSanityCheck(bundleType, globalName);
switch (bundleType) {
case UMD_DEV:
case NODE_DEV:
Expand All @@ -442,58 +377,61 @@ function getPlugins(
plugins.push(
replace(stripEnvVariables(true)),
commonjs(),
closure({
compilationLevel: 'SIMPLE',
languageIn: 'ECMASCRIPT5_STRICT',
languageOut: 'ECMASCRIPT5_STRICT',
env: 'CUSTOM',
warningLevel: 'QUIET',
// Don't let it create global variables in the browser.
// https://github.com/facebook/react/issues/10909
assumeFunctionWrapper: bundleType !== UMD_PROD,
applyInputSourceMaps: false,
useTypesForOptimization: false,
processCommonJsModules: false,
})
closure(
Object.assign({}, closureOptions, {
// Don't let it create global variables in the browser.
// https://github.com/facebook/react/issues/10909
assumeFunctionWrapper: bundleType !== UMD_PROD,
})
)
);
break;
case FB_DEV:
plugins.push(
replace(stripEnvVariables(false)),
commonjs(),
rewriteFBReactImport()
rewriteFBReactImport(),
prettier()
);
break;
case FB_PROD:
plugins.push(
replace(stripEnvVariables(true)),
commonjs(),
uglify(
getUglifyConfig({
mangle: bundleType !== FB_PROD,
preserveVersionHeader: bundleType === UMD_PROD,
// leave comments in for source map debugging purposes
// they will be stripped as part of FB's build process
removeComments: bundleType !== FB_PROD,
headerSanityCheck,
closure(
Object.assign({}, closureOptions, {
assumeFunctionWrapper: true,
// Disable renaming so we have readable FB bundles.
// Works because `google-closure-compiler-js` is forked in the Yarn lockfile.
// We can remove this if GCC merges my PR:
// https://github.com/google/closure-compiler/pull/2707
// and then the compiled version is released via `google-closure-compiler-js`.
renaming: false,
})
),
rewriteFBReactImport()
rewriteFBReactImport(),
prettier()
);
break;
case RN_DEV:
case RN_PROD:
plugins.push(
replace(stripEnvVariables(bundleType === RN_PROD)),
commonjs(),
uglify(
getUglifyConfig({
mangle: false,
preserveVersionHeader: true,
removeComments: true,
headerSanityCheck,
closure(
Object.assign({}, closureOptions, {
assumeFunctionWrapper: true,
// <hack>
// Disable renaming so we have readable FB bundles.
// Works because `google-closure-compiler-js` is forked in the Yarn lockfile.
// We can remove this if GCC merges my PR:
// https://github.com/google/closure-compiler/pull/2707
// and then the compiled version is released via `google-closure-compiler-js`.
renaming: false,
// </hack>
})
)
),
prettier()
);
break;
}
Expand Down
30 changes: 21 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1437,7 +1437,7 @@ detective@^4.3.1:
acorn "^4.0.3"
defined "^1.0.0"

diff@^3.2.0:
diff@3.3.0, diff@^3.2.0:
version "3.3.0"
resolved "https://registry.yarnpkg.com/diff/-/diff-3.3.0.tgz#056695150d7aa93237ca7e378ac3b1682b7963b9"

Expand Down Expand Up @@ -2150,8 +2150,8 @@ glogg@^1.0.0:
sparkles "^1.0.0"

google-closure-compiler-js@>20170000:
version "20170626.0.0"
resolved "https://registry.yarnpkg.com/google-closure-compiler-js/-/google-closure-compiler-js-20170626.0.0.tgz#5df265b277d1ec6fdea12eed131d1491cd8a8d71"
version "20170910.0.1"
resolved "https://registry.yarnpkg.com/@gaearon/google-closure-compiler-js/-/google-closure-compiler-js-20170910.0.1.tgz#b0cf7ef408219a19c390375a76bf0a82ccf65eea"
dependencies:
minimist "^1.2.0"
vinyl "^2.0.1"
Expand Down Expand Up @@ -3208,6 +3208,12 @@ lru-cache@^4.0.1:
pseudomap "^1.0.2"
yallist "^2.1.2"

magic-string@0.22.4, magic-string@^0.22.4:
version "0.22.4"
resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.22.4.tgz#31039b4e40366395618c1d6cf8193c53917475ff"
dependencies:
vlq "^0.2.1"

magic-string@^0.15.2:
version "0.15.2"
resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.15.2.tgz#0681d7388741bbc3addaa65060992624c6c09e9c"
Expand All @@ -3220,12 +3226,6 @@ magic-string@^0.16.0:
dependencies:
vlq "^0.2.1"

magic-string@^0.22.4:
version "0.22.4"
resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.22.4.tgz#31039b4e40366395618c1d6cf8193c53917475ff"
dependencies:
vlq "^0.2.1"

makeerror@1.0.x:
version "1.0.11"
resolved "https://registry.yarnpkg.com/makeerror/-/makeerror-1.0.11.tgz#e01a5c9109f2af79660e4e8b9587790184f5a96c"
Expand Down Expand Up @@ -3675,6 +3675,10 @@ prettier@1.8.1:
version "1.8.1"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.8.1.tgz#91064d778c08c85ac1cbe6b23195c34310d039f9"

prettier@^1.0.0:
version "1.8.1"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.8.1.tgz#91064d778c08c85ac1cbe6b23195c34310d039f9"

pretty-format@21.3.0-beta.4:
version "21.3.0-beta.4"
resolved "https://registry.yarnpkg.com/pretty-format/-/pretty-format-21.3.0-beta.4.tgz#6b3b8aba0b7097f821156e5fd1e3bc0fa923b17f"
Expand Down Expand Up @@ -4104,6 +4108,14 @@ rollup-plugin-node-resolve@^2.0.0:
builtin-modules "^1.1.0"
resolve "^1.1.6"

rollup-plugin-prettier@^0.3.0:
version "0.3.0"
resolved "https://registry.yarnpkg.com/rollup-plugin-prettier/-/rollup-plugin-prettier-0.3.0.tgz#1000df8a2914d367b79507c1caa582fdbed8b0f3"
dependencies:
diff "3.3.0"
magic-string "0.22.4"
prettier "^1.0.0"

rollup-plugin-replace@^1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/rollup-plugin-replace/-/rollup-plugin-replace-1.1.1.tgz#396315ded050a6ce43b9518a886a3f60efb1ea33"
Expand Down

0 comments on commit 6c43c6b

Please sign in to comment.