backend: export a normal Webpack config object (#779)

Summary:
Previously, our `webpack.config.backend.js` file actually exported a
function that could be used to make a Webpack configuration object.
(This is not to be confused with the late `makeWebpackConfig.js`, which
actually exported a configuration object!)

In addition to being confusing nomenclature, this was a sneaky trap for
CLI users. Invoking `webpack --config config/webpack.config.backend.js`
would actually work, but do the wrong thing: Webpack _allows_ your
configuration object to be a function, but with different semantics. In
particular, the result was that Webpack would emit the build output into
your current directory instead of into `bin/`.

This commit fixes that by making `webpack.config.backend.js` export the
Webpack configuration object for the backend JavaScript applications.
The logic to change the path is now handled by the caller, by
overwriting `config.output.path`; this is exactly [the same approach
that the Webpack CLI takes when given an `--output-path`][1], so it’s
okay with me.

[1]: 368e2640e6/bin/convert-argv.js (L406-L409)

Test Plan:
Run `yarn backend` and `yarn backend --dry-run`. Note that each runs
with appropriate output (both emitted files and console logs).

wchargin-branch: backend-webpack-config-object
This commit is contained in:
William Chargin 2018-09-05 11:58:20 -07:00 committed by GitHub
parent 2779770af5
commit 4e8c89abfe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 10 deletions

View File

@ -11,7 +11,7 @@ const env = getClientEnvironment();
// This is the backend configuration. It builds applications that target
// Node and will not run in a browser.
module.exports = (outputPath) => ({
module.exports = {
// Don't attempt to continue if there are any errors.
bail: true,
// Target Node instead of the browser.
@ -19,7 +19,7 @@ module.exports = (outputPath) => ({
entry: paths.backendEntryPoints,
externals: [nodeExternals()],
output: {
path: outputPath,
path: paths.backendBuild,
// Generated JS file names (with nested folders).
// There will be one main bundle, and one file per asynchronous chunk.
// We don't currently advertise code splitting but Webpack supports it.
@ -62,4 +62,4 @@ module.exports = (outputPath) => ({
new RemoveBuildDirectoryPlugin(),
new webpack.DefinePlugin(env.individuallyStringified),
],
});
};

View File

@ -11,12 +11,12 @@ 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 config = require("../config/webpack.config.backend");
const paths = require("../config/paths");
const checkRequiredFiles = require("react-dev-utils/checkRequiredFiles");
const formatWebpackMessages = require("react-dev-utils/formatWebpackMessages");
@ -27,12 +27,8 @@ const printBuildError = require("react-dev-utils/printBuildError");
const WARN_AFTER_BUNDLE_GZIP_SIZE = 512 * 1024;
const WARN_AFTER_CHUNK_GZIP_SIZE = 1024 * 1024;
const outputPath = process.argv.some((s) => s === "--dry-run" || s === "-n")
? tmp.dirSync({unsafeCleanup: true, prefix: "sourcecred-"}).name
: paths.backendBuild;
build().then(
({stats, warnings}) => {
({stats, outputPath, warnings}) => {
if (warnings.length) {
console.log(chalk.yellow("Compiled with warnings.\n"));
console.log(warnings.join("\n\n"));
@ -64,7 +60,16 @@ build().then(
function build() {
console.log("Building backend applications...");
let compiler = webpack(config(outputPath));
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) {
@ -95,6 +100,7 @@ function build() {
}
return resolve({
stats,
outputPath,
warnings: messages.warnings,
});
});