diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 00000000..f59309b7 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,6 @@ +root = true + +[*.{js,ts,tsx,ejs,es}] +indent_style = spaces +indent_size = 2 +end_of_line = lf diff --git a/lib/minify.js b/lib/minify.js new file mode 100644 index 00000000..8b4b78ee --- /dev/null +++ b/lib/minify.js @@ -0,0 +1,125 @@ +// Inline for now +import { codeFrameColumns } from "@babel/code-frame"; +import * as Terser from "terser"; +import nameCache from "./name-cache.js"; +let laterOut = 0; +const minifyOpts = { + nameCache, + compress: { + passes: 3, + global_defs: { + // see about preimporting require later on + }, + booleans_as_integers: true + }, + mangle: { + reserved: ["require", "define", "self"], + /* */ + properties: { + builtins: false, + debug: false, + keep_quoted: true, + regex: /^__|^(?:[A-Z_]+|prevState|state|_(?:width|height|mines|playMode|toReveal|flags|stateChange|minedCells|flagged|hasMine|revealed|touching(?:Flags|Mines)|additionalButtonData|canvas|table|currentFocusableBtn|tableContainer|buttons|rendererInit|queryFirstCellRect)|getSurrounding|grid|subscribe|unsubscribe|reset|restart)$/ + /* Unmaintainable? * + reserved: [ + // require.js + "require", + "define", + // promises + "then", + "catch", + // regenerator + "next", + "return", + "throw", + // sw + "skipWaiting", + "respondWith", + "request", + "waitUntil", + // cache + "open", + "addAll", + "ignoreSearch", + // dom + "class", + "inputmode", + "role", + // storage + "mines", + // nav + "deviceMemory", + // idk how this happens + "onSubmit", + // preact? + "_doManualDomHandling", + "_loop", + "_onBackClick", + "_onDangerModeChange", + "_onDangerModeSwitchToggle", + "_onDangerModeTouchStart", + "_onDownClick", + "_onGameChangeSubscribe", + "_onGameChangeUnsubscribe", + "_onKeyUp", + "_onMotionPrefChange", + "_onResize", + "_onSelectChange", + "_onSettingInput", + "_onSettingsClick", + "_onSettingsCloseClick", + "_onStartGame", + "_onTableScroll", + "_onUpClick", + "_onWindowResize", + "_renderCanvas", + "_renderLoop", + "_startGame", + "moveFocusByKey", + "moveFocusWithMouse", + "onCellClick", + "onDblClick", + "onGameChange", + "onKeyDownOnTable", + "onKeyUp", + "onKeyUpOnTable", + "onMouseDown", + "onMouseUp", + "onReset", + "onRestart", + "removeFocusVisual", + "setFocus", + "setFocusVisual", + "simulateClick" + ] /* */ + /* Yeah. Reserved list is unmaintainable. */ + } + }, + sourceMap: true +}; + +export const terser = { + name: "terser", + renderChunk(code, chunk, outputOpts) { + // async to simplify + const result = Terser.minify(code, minifyOpts); + if (laterOut !== -1) { + if (laterOut !== 0) { + clearTimeout(laterOut); + laterOut = 0; + } + laterOut = setTimeout( + cache => console.log("Minify name cache: %O", cache), + 100, + nameCache + ); + } + if (result.error) { + const { line, col: column, message } = result.error; + console.error( + codeFrameColumns(code, { start: { line, column } }, { message }) + ); + throw result.error; + } else return result; + } +}; diff --git a/lib/name-cache.js b/lib/name-cache.js new file mode 100644 index 00000000..2dbb131e --- /dev/null +++ b/lib/name-cache.js @@ -0,0 +1,108 @@ +export default { + vars: { props: {} }, + props: { + props: { + $__assign: "t", + $__extends: "o", + $__rest: "i", + $__decorate: "u", + $__param: "l", + $__metadata: "s", + $__awaiter: "_", + $__generator: "h", + $__exportStar: "m", + $__values: "p", + $__read: "j", + $__spread: "S", + $__await: "O", + $__asyncGenerator: "k", + $__asyncDelegator: "R", + $__asyncValues: "g", + $__makeTemplateObject: "P", + $__importStar: "T", + $__esModule: "D", + $__importDefault: "G", + $_width: "v", + $_height: "M", + $_mines: "A", + $_playMode: "C", + $_toReveal: "q", + $_flags: "I", + $_stateChange: "N", + $_minedCells: "Y", + $_getSurrounding: "F", + $__html: "H", + $__key: "L", + $__ref: "$", + $prevState: "U", + $_grid: "B", + $__state: "J", + $BREAK: "V", + $THREE_CHAR_HEX: "X", + $SIX_CHAR_HEX: "W", + $CSS_RGB: "K", + $CSS_RGBA: "Z", + $HEX: "tt", + $RGB_ARRAY: "nt", + $RGBA_ARRAY: "it", + $RGBA_OBJ: "ot", + $RGB_OBJ: "et", + $HSVA_OBJ: "rt", + $HSV_OBJ: "st", + $COMPONENTS: "ut", + $__onChange: "at", + $__onFinishChange: "ct", + $__prev: "dt", + $__checkbox: "ht", + $__select: "ft", + $__input: "lt", + $__min: "pt", + $__max: "vt", + $__step: "gt", + $__impliedStep: "wt", + $__precision: "bt", + $__truncationSuspended: "xt", + $__background: "yt", + $__foreground: "At", + $__button: "kt", + $__color: "Ot", + $__temp: "Et", + $__selector: "St", + $__saturation_field: "Rt", + $__field_knob: "Bt", + $__field_knob_border: "Ft", + $__hue_knob: "Ct", + $__hue_field: "Gt", + $__input_textShadow: "jt", + $__ul: "Ht", + $__folders: "_t", + $__controllers: "zt", + $__rememberedObjects: "Dt", + $__rememberedObjectIndecesToControllers: "It", + $__listening: "Mt", + $DEFAULT_WIDTH: "Tt", + $__preset_select: "Jt", + $CLASS_CLOSED: "Nt", + $__closeButton: "Vt", + $TEXT_OPEN: "Xt", + $TEXT_CLOSED: "Pt", + $CLASS_MAIN: "Qt", + $CLASS_CLOSE_BUTTON: "Yt", + $CLASS_CLOSE_TOP: "Ut", + $CLASS_CLOSE_BOTTOM: "Wt", + $CLASS_AUTO_PLACE_CONTAINER: "Kt", + $CLASS_AUTO_PLACE: "Lt", + $__resizeHandler: "Zt", + $__li: "$t", + $CLASS_CONTROLLER_ROW: "qt", + $__gui: "tn", + $CLASS_DRAG: "nn", + $__resize_handle: "in", + $__save_row: "on", + $CLASS_TOO_TALL: "en", + $grid: "ti", + $restart: "ii", + $_additionalButtonData: "ei" + } + } +}; diff --git a/package.json b/package.json index a2947181..bf6d1c3d 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,7 @@ "@babel/plugin-syntax-dynamic-import": "^7.2.0", "@babel/polyfill": "^7.4.3", "@babel/preset-env": "^7.4.3", + "@babel/code-frame": "^7.0.0", "characterset": "^1.3.0", "comlink": "^4.0.0-alpha.10", "dat.gui": "^0.7.6", @@ -57,9 +58,9 @@ "rollup-plugin-loadz0r": "^0.7.1", "rollup-plugin-node-resolve": "^4.2.3", "rollup-plugin-postcss": "^2.0.3", - "rollup-plugin-terser": "^4.0.4", "rollup-plugin-typescript2": "^0.21.0", "rollup-pluginutils": "^2.6.0", + "terser": "^3.17.0", "travis-size-report": "^1.0.1", "tslint": "^5.16.0", "typed-css-modules": "^0.4.2", diff --git a/rollup.config.js b/rollup.config.js index e1c6b94d..ac41ff7f 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -13,7 +13,6 @@ import typescript from "rollup-plugin-typescript2"; import nodeResolve from "rollup-plugin-node-resolve"; -import { terser } from "rollup-plugin-terser"; import loadz0r from "rollup-plugin-loadz0r"; import dependencyGraph from "./lib/dependency-graph-plugin.js"; import chunkNamePlugin from "./lib/chunk-name-plugin.js"; @@ -26,6 +25,7 @@ import { readFileSync } from "fs"; import constsPlugin from "./lib/consts-plugin.js"; import ejsAssetPlugin from "./lib/ejs-asset-plugin.js"; import assetTransformPlugin from "./lib/asset-transform-plugin.js"; +import { terser } from "./lib/minify.js"; import postCSSUrl from "postcss-url"; // Delete 'dist' @@ -128,6 +128,6 @@ export default { propList: ["facadeModuleId", "fileName", "imports", "code", "isAsset"] }), resourceListPlugin(), - terser() + terser ] };