diff --git a/package.json b/package.json index 053dd8abd..738d8911b 100644 --- a/package.json +++ b/package.json @@ -68,6 +68,7 @@ "express": "^4.15.2", "fs-extra": "^7.0.1", "invariant": "^2.2.2", + "line-replace": "^1.0.2", "lodash.debounce": "^4.0.8", "mkdirp": "^0.5.1", "node-fetch": "^2.3.0", diff --git a/scripts/build-headless.js b/scripts/build-headless.js new file mode 100644 index 000000000..914099e57 --- /dev/null +++ b/scripts/build-headless.js @@ -0,0 +1,48 @@ +/** + * Copyright 2018-present Facebook. + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * @format + */ + +const path = require('path'); +const lineReplace = require('line-replace'); +const fs = require('fs-extra'); +const { + buildFolder, + compile, + compileDefaultPlugins, +} = require('./build-utils.js'); + +function preludeBundle(dir) { + return new Promise((resolve, reject) => + lineReplace({ + file: path.join(dir, 'bundle.js'), + line: 1, + text: + 'var __DEV__=false; global.electronRequire = require; global.performance = require("perf_hooks").performance;', + addNewLine: true, + callback: resolve, + }), + ); +} + +function copyFolder(dir) { + const buildDir = path.join(__dirname, '..', 'dist'); + fs.removeSync(buildDir); + fs.copySync(dir, buildDir); +} + +(async () => { + process.env.BUILD_HEADLESS = 'true'; + const dir = await buildFolder(); + // eslint-disable-next-line no-console + console.log('Created build directory', dir); + await compile(dir, path.join(__dirname, '..', 'headless', 'index.js')); + await preludeBundle(dir); + await compileDefaultPlugins(dir); + copyFolder(dir); + // eslint-disable-next-line no-console + console.log('✨ Done'); + process.exit(); +})(); diff --git a/static/package.json b/static/package.json index b2b19bb92..39a518319 100644 --- a/static/package.json +++ b/static/package.json @@ -16,5 +16,8 @@ "metro": "^0.49.0", "recursive-readdir": "2.2.2" }, - "devDependencies": {} + "devDependencies": {}, + "resolutions": { + "metro/temp": "0.9.0" + } } diff --git a/static/transforms/index.js b/static/transforms/index.js index 3737f812a..eab3bb451 100644 --- a/static/transforms/index.js +++ b/static/transforms/index.js @@ -46,7 +46,10 @@ function transform({filename, options, src}) { plugins.push(require('./fb-stubs.js')); } - if (options.isTestRunner) { + if (process.env.BUILD_HEADLESS) { + plugins.push(require('./electron-stubs.js')); + plugins.push(require('./electron-requires.js')); + } else if (options.isTestRunner) { if (process.env.USE_ELECTRON_STUBS) { plugins.push(require('./electron-stubs.js')); } diff --git a/static/yarn.lock b/static/yarn.lock index 4d65f3ebd..bf03f0e38 100644 --- a/static/yarn.lock +++ b/static/yarn.lock @@ -1389,6 +1389,18 @@ glob@^7.0.5: once "^1.3.0" path-is-absolute "^1.0.0" +glob@^7.1.3: + version "7.1.3" + resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.3.tgz#3960832d3f1574108342dafd3a67b332c0969df1" + integrity sha512-vcfuiIxogLV4DlGBHIUOwI0IbrJ8HWPc4MU7HzviGeNho/UJDfi6B5p3sHeWIQ0KGIU0Jpxi5ZHxemQfLkkAwQ== + dependencies: + fs.realpath "^1.0.0" + inflight "^1.0.4" + inherits "2" + minimatch "^3.0.4" + once "^1.3.0" + path-is-absolute "^1.0.0" + global@^4.3.0: version "4.3.2" resolved "https://registry.yarnpkg.com/global/-/global-4.3.2.tgz#e76989268a6c74c38908b1305b10fc0e394e9d0f" @@ -2642,9 +2654,12 @@ rimraf@^2.5.4, rimraf@^2.6.1: dependencies: glob "^7.0.5" -rimraf@~2.2.6: - version "2.2.8" - resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-2.2.8.tgz#e439be2aaee327321952730f99a8929e4fc50582" +rimraf@~2.6.2: + version "2.6.3" + resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-2.6.3.tgz#b2d104fe0d8fb27cf9e0a1cda8262dd3833c6cab" + integrity sha512-mwqeW5XsA2qAejG46gYdENaxXjx9onRNCfn7L0duuP4hCuTIi/QO7PDK07KJfp1d+izWPrzEJDcSqBa0OZQriA== + dependencies: + glob "^7.1.3" rsvp@^3.3.3: version "3.6.2" @@ -2927,12 +2942,12 @@ tar@^4: safe-buffer "^5.1.2" yallist "^3.0.2" -temp@0.8.3: - version "0.8.3" - resolved "https://registry.yarnpkg.com/temp/-/temp-0.8.3.tgz#e0c6bc4d26b903124410e4fed81103014dfc1f59" +temp@0.8.3, temp@0.9.0: + version "0.9.0" + resolved "https://registry.yarnpkg.com/temp/-/temp-0.9.0.tgz#61391795a11bd9738d4c4d7f55f012cb8f55edaa" + integrity sha512-YfUhPQCJoNQE5N+FJQcdPz63O3x3sdT4Xju69Gj4iZe0lBKOtnAMi0SLj9xKhGkcGhsxThvTJ/usxtFPo438zQ== dependencies: - os-tmpdir "^1.0.0" - rimraf "~2.2.6" + rimraf "~2.6.2" throat@^4.1.0: version "4.1.0" diff --git a/yarn.lock b/yarn.lock index c80206936..408c14b8b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2911,6 +2911,7 @@ fs-extra@^4.0.1: fs-extra@^6.0.0: version "6.0.1" resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-6.0.1.tgz#8abc128f7946e310135ddc93b98bddb410e7a34b" + integrity sha512-GnyIkKhhzXZUWFCaJzvyDLEEgDkPfb4/TPvJCJVuS8MWZgoSsErf++QpiAlDnKFcqhRlm+tIOcencCjyJE6ZCA== dependencies: graceful-fs "^4.1.2" jsonfile "^4.0.0" @@ -4266,6 +4267,11 @@ levn@^0.3.0, levn@~0.3.0: prelude-ls "~1.1.2" type-check "~0.3.2" +line-replace@^1.0.2: + version "1.0.2" + resolved "https://registry.yarnpkg.com/line-replace/-/line-replace-1.0.2.tgz#b1995da54060d671c449196fa228395f0faaea47" + integrity sha1-sZldpUBg1nHESRlvoig5Xw+q6kc= + linked-list@0.1.0: version "0.1.0" resolved "https://registry.yarnpkg.com/linked-list/-/linked-list-0.1.0.tgz#798b0ff97d1b92a4fd08480f55aea4e9d49d37bf"