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
This commit is contained in:
William Chargin 2018-09-05 12:28:27 -07:00 committed by GitHub
parent ff2cfd14b2
commit c48597651e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 110 deletions

View File

@ -1,5 +1,7 @@
// @flow // @flow
const tmp = require("tmp");
const execDependencyGraph = require("../src/tools/execDependencyGraph"); const execDependencyGraph = require("../src/tools/execDependencyGraph");
main(); main();
@ -69,7 +71,18 @@ function makeTasks(mode /*: "BASIC" | "FULL" */) {
}, },
{ {
id: "backend", 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: [], deps: [],
}, },
]; ];

View File

@ -72,7 +72,7 @@
"check-pretty": "prettier --list-different '**/*.js'", "check-pretty": "prettier --list-different '**/*.js'",
"start": "NODE_ENV=development webpack-dev-server --config config/webpack.config.web.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", "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", "test": "node ./config/test.js",
"unit": "BABEL_ENV=test NODE_ENV=test jest --env=jsdom", "unit": "BABEL_ENV=test NODE_ENV=test jest --env=jsdom",
"sharness": "make -sC ./sharness prove PROVE_OPTS=-f TEST_OPTS='--chain-lint'", "sharness": "make -sC ./sharness prove PROVE_OPTS=-f TEST_OPTS='--chain-lint'",

View File

@ -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,
});
});
});
}