Path: blob/master/lib/rammerhead/src/build.js
5256 views
require('dotenv-flow').config();12const path = require('path');3const fs = require('fs');4const UglifyJS = require('uglify-js');56// modify unmodifable items that cannot be hooked in rammerhead.js7fs.writeFileSync(8path.join(__dirname, './client/hammerhead.js'),9// part of fix for iframing issue10'window["%is-hammerhead%"] = true;\n' +11fs12.readFileSync(path.join(__dirname, '../node_modules/testcafe-hammerhead/lib/client/hammerhead.js'), 'utf8')13// fix iframing proxy issue14.replace(15/window === window\.top/g,16'((window.parent === window.top && !window.top["%hammerhead%"]) || window === window.top)'17)18.replace(19'isCrossDomainParent = parentLocationWrapper === parentWindow.location',20'isCrossDomainParent = parentLocationWrapper === parentWindow.location || !parentWindow["%hammerhead%"]'21)22.replace(23'!sameOriginCheck(window1Location, window2Location)',24'!(sameOriginCheck(window1Location, window2Location) && (!!window1["%is-hammerhead%"] === !!window2["%is-hammerhead%"]))'25)26// return false when unable to convert properties on other windows to booleans (!)27.replace(28/!(parent|parentWindow|window1|window2|window\.top)\[("%(?:is-)?hammerhead%")]/g,29'!(() => { try{ return $1[$2]; }catch(error){ return true } })()'30)3132// disable saving to localStorage as we are using a completely different implementation33.replace('saveToNativeStorage = function () {', 'saveToNativeStorage = function () {return;')3435// prevent calls to elements on a closed iframe36.replace('dispatchEvent: function () {', '$& if (!window) return null;')37.replace('click: function () {', '$& if (!window) return null;')38.replace('setSelectionRange: function () {', '$& if (!window) return null;')39.replace('select: function () {', '$& if (!window) return null;')40.replace('focus: function () {', '$& if (!window) return null;')41.replace('blur: function () {', '$& if (!window) return null;')42.replace('preventDefault: function () {', '$& if (!window) return null;')4344// expose hooks for rammerhead.js45.replace(46'function parseProxyUrl$1',47'window.overrideParseProxyUrl = function(rewrite) {parseProxyUrl$$1 = rewrite(parseProxyUrl$$1)}; $&'48)49.replace(50'function getProxyUrl$1',51'window.overrideGetProxyUrl = function(rewrite) {getProxyUrl$$1 = rewrite(getProxyUrl$$1)}; $&'52)53.replace('return window.location.search;', 'return (new URL(get$$2())).search;')54.replace('return window.location.hash;', 'return (new URL(get$$2())).hash;')55.replace(56'setter: function (search) {',57'$& var url = new URL(get$$2()); url.search = search; window.location = convertToProxyUrl(url.href); return search;'58)59.replace(60'setter: function (hash) {',61'$& var url = new URL(get$$2()); url.hash = hash; window.location.hash = (new URL(convertToProxyUrl(url.href))).hash; return hash;'62)63);6465const minify = (fileName, newFileName) => {66const minified = UglifyJS.minify(fs.readFileSync(path.join(__dirname, './client', fileName), 'utf8'));67if (minified.error) {68throw minified.error;69}70fs.writeFileSync(path.join(__dirname, './client', newFileName), minified.code, 'utf8');71};7273minify('rammerhead.js', 'rammerhead.min.js');74minify('hammerhead.js', 'hammerhead.min.js');757677