From c48597651ea5fd51481c42bffbd184941cd8e0a5 Mon Sep 17 00:00:00 2001 From: William Chargin Date: Wed, 5 Sep 2018 12:28:27 -0700 Subject: [PATCH] backend: invoke Webpack directly in package script (#782) Summary: This commit removes the `config/backend.js` script and replaces it with a direct invocation of Webpack. This enables us to use command-line arguments to Webpack, like `--output-path`. Test Plan: Note that `rm -rf bin; yarn backend` still works, and that the resulting applications work (`node bin/sourcecred.js load`). Note that `yarn test` and `yarn test --full` still work. wchargin-branch: backend-webpack-direct --- config/test.js | 15 ++++++- package.json | 2 +- scripts/backend.js | 108 --------------------------------------------- 3 files changed, 15 insertions(+), 110 deletions(-) delete mode 100644 scripts/backend.js diff --git a/config/test.js b/config/test.js index 176a392..1c7be95 100644 --- a/config/test.js +++ b/config/test.js @@ -1,5 +1,7 @@ // @flow +const tmp = require("tmp"); + const execDependencyGraph = require("../src/tools/execDependencyGraph"); main(); @@ -69,7 +71,18 @@ function makeTasks(mode /*: "BASIC" | "FULL" */) { }, { id: "backend", - cmd: ["npm", "run", "--silent", "backend", "--", "--dry-run"], + cmd: [ + "npm", + "run", + "--silent", + "backend", + "--", + "--output-path", + tmp.dirSync({ + unsafeCleanup: true, + prefix: "sourcecred-backend-dry-run-", + }).name, + ], deps: [], }, ]; diff --git a/package.json b/package.json index e9eaf76..bd94753 100644 --- a/package.json +++ b/package.json @@ -72,7 +72,7 @@ "check-pretty": "prettier --list-different '**/*.js'", "start": "NODE_ENV=development webpack-dev-server --config config/webpack.config.web.js", "build": "NODE_ENV=production webpack --config config/webpack.config.web.js", - "backend": "node scripts/backend.js", + "backend": "NODE_ENV=development webpack --config config/webpack.config.backend.js", "test": "node ./config/test.js", "unit": "BABEL_ENV=test NODE_ENV=test jest --env=jsdom", "sharness": "make -sC ./sharness prove PROVE_OPTS=-f TEST_OPTS='--chain-lint'", diff --git a/scripts/backend.js b/scripts/backend.js deleted file mode 100644 index c9b1674..0000000 --- a/scripts/backend.js +++ /dev/null @@ -1,108 +0,0 @@ -// @flow -"use strict"; - -// Do this as the first thing so that any code reading it knows the right env. -process.env.NODE_ENV = process.env.NODE_ENV || "development"; -process.env.BABEL_ENV = process.env.NODE_ENV; -process.env.SOURCECRED_BACKEND = "true"; - -require("../src/tools/entry"); - -// Ensure environment variables are read. -require("../config/env"); - -const cloneDeep = require("lodash.clonedeep"); -const path = require("path"); -const chalk = require("chalk"); -const fs = require("fs-extra"); -const tmp = require("tmp"); -const webpack = require("webpack"); -const paths = require("../config/paths"); -const checkRequiredFiles = require("react-dev-utils/checkRequiredFiles"); -const formatWebpackMessages = require("react-dev-utils/formatWebpackMessages"); -const FileSizeReporter = require("react-dev-utils/FileSizeReporter"); -const printBuildError = require("react-dev-utils/printBuildError"); - -// These sizes are pretty large. We'll warn for bundles exceeding them. -const WARN_AFTER_BUNDLE_GZIP_SIZE = 512 * 1024; -const WARN_AFTER_CHUNK_GZIP_SIZE = 1024 * 1024; - -build().then( - ({stats, outputPath, warnings}) => { - if (warnings.length) { - console.log(chalk.yellow("Compiled with warnings.\n")); - console.log(warnings.join("\n\n")); - console.log( - "\nSearch for the " + - chalk.underline(chalk.yellow("keywords")) + - " to learn more about each warning." - ); - console.log( - "To ignore, add " + - chalk.cyan("// eslint-disable-next-line") + - " to the line before.\n" - ); - } else { - console.log(chalk.green("Compiled successfully.\n")); - } - - const buildFolder = path.relative(process.cwd(), outputPath); - console.log(`Build completed; results in '${buildFolder}'.`); - }, - (err) => { - console.log(chalk.red("Failed to compile.\n")); - printBuildError(err); - process.exit(1); - } -); - -// Create the backend build -function build() { - console.log("Building backend applications..."); - - const config = cloneDeep(require("../config/webpack.config.backend")); - if (process.argv.some((s) => s === "--dry-run" || s === "-n")) { - config.output.path = tmp.dirSync({ - unsafeCleanup: true, - prefix: "sourcecred-", - }).name; - } - const outputPath = config.output.path; - - let compiler = webpack(config); - return new Promise((resolve, reject) => { - compiler.run((err, stats) => { - if (err) { - return reject(err); - } - const messages = formatWebpackMessages(stats.toJson({}, true)); - if (messages.errors.length) { - // Only keep the first error. Others are often indicative - // of the same problem, but confuse the reader with noise. - if (messages.errors.length > 1) { - messages.errors.length = 1; - } - return reject(new Error(messages.errors.join("\n\n"))); - } - if ( - process.env.CI && - (typeof process.env.CI !== "string" || - process.env.CI.toLowerCase() !== "false") && - messages.warnings.length - ) { - console.log( - chalk.yellow( - "\nTreating warnings as errors because process.env.CI = true.\n" + - "Most CI servers set it automatically.\n" - ) - ); - return reject(new Error(messages.warnings.join("\n\n"))); - } - return resolve({ - stats, - outputPath, - warnings: messages.warnings, - }); - }); - }); -}