2018-07-30 23:19:21 +00:00
|
|
|
// @flow
|
2018-07-31 00:29:34 +00:00
|
|
|
const express = require("express");
|
2018-08-07 18:17:03 +00:00
|
|
|
/*::
|
|
|
|
import type {
|
|
|
|
$Application as ExpressApp,
|
|
|
|
$Response as ExpressResponse,
|
|
|
|
} from "express";
|
|
|
|
*/
|
2018-07-31 00:29:34 +00:00
|
|
|
const os = require("os");
|
2018-07-30 23:15:30 +00:00
|
|
|
const path = require("path");
|
|
|
|
const webpack = require("webpack");
|
2018-07-31 22:27:32 +00:00
|
|
|
const RemoveBuildDirectoryPlugin = require("./RemoveBuildDirectoryPlugin");
|
2018-08-10 20:15:49 +00:00
|
|
|
const CopyPlugin = require("copy-webpack-plugin");
|
2018-07-30 23:15:30 +00:00
|
|
|
const ManifestPlugin = require("webpack-manifest-plugin");
|
|
|
|
const StaticSiteGeneratorPlugin = require("static-site-generator-webpack-plugin");
|
|
|
|
const ModuleScopePlugin = require("react-dev-utils/ModuleScopePlugin");
|
|
|
|
const paths = require("./paths");
|
|
|
|
const getClientEnvironment = require("./env");
|
2019-07-16 18:31:11 +00:00
|
|
|
const _getProjectIds = require("../src/core/_getProjectIds");
|
2018-07-30 23:15:30 +00:00
|
|
|
|
|
|
|
// Source maps are resource heavy and can cause out of memory issue for large source files.
|
|
|
|
const shouldUseSourceMap = process.env.GENERATE_SOURCEMAP !== "false";
|
2018-11-01 19:38:18 +00:00
|
|
|
|
2019-07-16 18:31:11 +00:00
|
|
|
function loadProjectIds() /*: Promise<$ReadOnlyArray<string>> */ {
|
2018-11-01 19:38:18 +00:00
|
|
|
const env = process.env.SOURCECRED_DIRECTORY;
|
|
|
|
// TODO(#945): de-duplicate finding the directory with src/cli/common.js
|
|
|
|
const defaultDirectory = path.join(os.tmpdir(), "sourcecred");
|
|
|
|
const scDirectory = env != null ? env : defaultDirectory;
|
2019-07-16 18:31:11 +00:00
|
|
|
return _getProjectIds(scDirectory);
|
2018-11-01 19:38:18 +00:00
|
|
|
}
|
2018-07-30 23:15:30 +00:00
|
|
|
|
2019-07-16 18:31:11 +00:00
|
|
|
async function makeConfig(
|
|
|
|
mode /*: "production" | "development" */
|
|
|
|
) /*: Promise<mixed> */ {
|
2018-07-30 23:22:55 +00:00
|
|
|
return {
|
|
|
|
// Don't attempt to continue if there are any errors.
|
|
|
|
bail: true,
|
|
|
|
// We generate sourcemaps in production. This is slow but gives good results.
|
|
|
|
// You can exclude the *.map files from the build during deployment.
|
|
|
|
devtool: shouldUseSourceMap ? "source-map" : false,
|
|
|
|
// In production, we only want to load the polyfills and the app code.
|
|
|
|
entry: {
|
|
|
|
main: [require.resolve("./polyfills"), paths.appIndexJs],
|
|
|
|
ssr: [
|
|
|
|
require.resolve("./polyfills"),
|
|
|
|
paths.appServerSideRenderingIndexJs,
|
|
|
|
],
|
2018-07-30 23:15:30 +00:00
|
|
|
},
|
2018-07-30 23:26:13 +00:00
|
|
|
devServer: {
|
|
|
|
inline: false,
|
2018-07-31 00:29:34 +00:00
|
|
|
before: (app /*: ExpressApp */) => {
|
2018-08-07 18:17:03 +00:00
|
|
|
const apiRoot = "/api/v1/data";
|
|
|
|
const rejectCache = (_unused_req, res /*: ExpressResponse */) => {
|
|
|
|
res.status(400).send("Bad Request: Cache unavailable at runtime\n");
|
|
|
|
};
|
|
|
|
app.get(`${apiRoot}/cache`, rejectCache);
|
|
|
|
app.get(`${apiRoot}/cache/*`, rejectCache);
|
2018-07-31 00:29:34 +00:00
|
|
|
app.use(
|
2018-08-07 18:17:03 +00:00
|
|
|
apiRoot,
|
2018-07-31 00:29:34 +00:00
|
|
|
express.static(
|
|
|
|
process.env.SOURCECRED_DIRECTORY ||
|
|
|
|
path.join(os.tmpdir(), "sourcecred")
|
|
|
|
)
|
|
|
|
);
|
|
|
|
},
|
2018-07-30 23:26:13 +00:00
|
|
|
},
|
2018-07-30 23:22:55 +00:00
|
|
|
output: {
|
|
|
|
// The build folder.
|
|
|
|
path: paths.appBuild,
|
|
|
|
// 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.
|
|
|
|
filename: "static/js/[name].[chunkhash:8].js",
|
|
|
|
chunkFilename: "static/js/[name].[chunkhash:8].chunk.js",
|
|
|
|
// Point sourcemap entries to original disk location (format as URL on Windows)
|
|
|
|
devtoolModuleFilenameTemplate: (
|
|
|
|
info /*:
|
|
|
|
{|
|
|
|
|
// https://webpack.js.org/configuration/output/#output-devtoolmodulefilenametemplate
|
|
|
|
+absoluteResourcePath: string,
|
|
|
|
+allLoaders: string,
|
|
|
|
+hash: string,
|
|
|
|
+id: string,
|
|
|
|
+loaders: string,
|
|
|
|
+resource: string,
|
|
|
|
+resourcePath: string,
|
|
|
|
+namespace: string,
|
|
|
|
|}
|
|
|
|
*/
|
|
|
|
) =>
|
|
|
|
path
|
|
|
|
.relative(paths.appSrc, info.absoluteResourcePath)
|
|
|
|
.replace(/\\/g, "/"),
|
|
|
|
// We need to use a UMD module to build the static site.
|
|
|
|
libraryTarget: "umd",
|
2019-07-11 04:41:26 +00:00
|
|
|
globalObject: "this",
|
2018-07-30 23:22:55 +00:00
|
|
|
},
|
|
|
|
resolve: {
|
|
|
|
// This allows you to set a fallback for where Webpack should look for modules.
|
|
|
|
// We placed these paths second because we want `node_modules` to "win"
|
|
|
|
// if there are any conflicts. This matches Node resolution mechanism.
|
|
|
|
// https://github.com/facebookincubator/create-react-app/issues/253
|
|
|
|
modules: [
|
|
|
|
"node_modules",
|
|
|
|
paths.appNodeModules,
|
|
|
|
...(process.env.NODE_PATH || "").split(path.delimiter).filter(Boolean),
|
|
|
|
],
|
|
|
|
// These are the reasonable defaults supported by the Node ecosystem.
|
|
|
|
// We also include JSX as a common component filename extension to support
|
|
|
|
// some tools, although we do not recommend using it, see:
|
|
|
|
// https://github.com/facebookincubator/create-react-app/issues/290
|
|
|
|
// `web` extension prefixes have been added for better support
|
|
|
|
// for React Native Web.
|
|
|
|
extensions: [".web.js", ".mjs", ".js", ".json", ".web.jsx", ".jsx"],
|
|
|
|
alias: {
|
|
|
|
// Support React Native Web
|
|
|
|
// https://www.smashingmagazine.com/2016/08/a-glimpse-into-the-future-with-react-native-for-web/
|
|
|
|
"react-native": "react-native-web",
|
|
|
|
},
|
|
|
|
plugins: [
|
|
|
|
// Prevents users from importing files from outside of src/ (or node_modules/).
|
|
|
|
// This often causes confusion because we only process files within src/ with babel.
|
|
|
|
// To fix this, we prevent you from importing files out of src/ -- if you'd like to,
|
|
|
|
// please link the files into your node_modules/ and let module-resolution kick in.
|
|
|
|
// Make sure your source files are compiled, as they will not be processed in any way.
|
|
|
|
new ModuleScopePlugin(paths.appSrc, [paths.appPackageJson]),
|
|
|
|
],
|
|
|
|
},
|
|
|
|
module: {
|
|
|
|
strictExportPresence: true,
|
|
|
|
rules: [
|
|
|
|
// TODO: Disable require.ensure as it's not a standard language feature.
|
|
|
|
// We are waiting for https://github.com/facebookincubator/create-react-app/issues/2176.
|
|
|
|
// { parser: { requireEnsure: false } },
|
|
|
|
{
|
|
|
|
// "oneOf" will traverse all following loaders until one will
|
|
|
|
// match the requirements. When no loader matches it will fall
|
|
|
|
// back to the "file" loader at the end of the loader list.
|
|
|
|
oneOf: [
|
|
|
|
// "url" loader works just like "file" loader but it also embeds
|
|
|
|
// assets smaller than specified size as data URLs to avoid requests.
|
|
|
|
{
|
|
|
|
test: [/\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/],
|
|
|
|
loader: require.resolve("url-loader"),
|
|
|
|
options: {
|
|
|
|
limit: 10000,
|
|
|
|
name: "static/media/[name].[hash:8].[ext]",
|
|
|
|
},
|
2018-07-30 23:15:30 +00:00
|
|
|
},
|
2018-07-30 23:22:55 +00:00
|
|
|
// Process JS with Babel.
|
|
|
|
{
|
|
|
|
test: /\.(js|jsx|mjs)$/,
|
|
|
|
include: paths.appSrc,
|
|
|
|
loader: require.resolve("babel-loader"),
|
|
|
|
options: {
|
|
|
|
compact: true,
|
|
|
|
},
|
2018-07-30 23:15:30 +00:00
|
|
|
},
|
2018-07-30 23:22:55 +00:00
|
|
|
{
|
|
|
|
test: /\.css$/,
|
|
|
|
loader: "css-loader", // TODO(@wchargin): add csso-loader
|
2018-07-30 23:15:30 +00:00
|
|
|
},
|
Port skeleton of Odyssey frontend (#1132)
This commit integrates an bare skeleton of the odyssey frontend that we
implemented in the [odyssey-hackathon] repository. You can see the
working frontend that we are trying to port over at
[sourcecred.io/odyssey-hackathon/][scio].
The prototype in the other repository has some tooling choices which are
incompatible/redundant with decisions in our codebase (sass vs
aphrodite), and requires some tools not yet present here
(svg-react-loader). This commit includes the build and integration work
needed to port the prototype frontend into mainline SourceCred. The
frontend scaffold isn't yet integrated with any "real" Odyssey data.
One potential issue: right now, every page that is rendered from the
SourceCred homepage is contained within a [homepage/Page], meaning that
it has full SourceCred website styling, along with the SourceCred
website header. The [application][scio] also has a header. Currently, I
work around this by having the Odyssey UI cover up the base header (via
absolute positioning), which works but is hacky. We can consider more
principled solutions:
- Finding a way to specify routes which aren't contained by
[homepage/Page]; maybe by adding a new top-level route
[here][route-alternative].
- Unify the headers for the Odyssey viewer and the page as a whole
(sounds like inappropriate entanglement?)
- Have a website header and also an application header (sounds ugly?)
[homepage/Page]: https://github.com/sourcecred/sourcecred/blob/ee1d2fb996718fe41325711271542a54c197a1ed/src/homepage/Page.js
[route-alternative]: https://github.com/sourcecred/sourcecred/blob/ee1d2fb996718fe41325711271542a54c197a1ed/src/homepage/createRoutes.js#L17
Test plan: Run `yarn start`, and then navigate to
`localhost:8080/odyssey/`. observe that a working website is displayed,
and that the cred logo next to the word "SourceCred" is loaded properly
(i.e. svg-react-loader is integrated properly). Observe that there are
no build/compile errors from either `yarn start` or `yarn build`. Also,
observe that the UI looks passably nice, and that if the number of
elements in the entity lists is larger than can be displayed, the
sidebar pane scrolls independently.
The UI was tested in both Chrome and Firefox.
[odyssey-hackathon]: https://github.com/sourcecred/odyssey-hackathon
[scio]: https://sourcecred.io/odyssey-hackathon/
Thanks to @jmnemo, as the implementation is based on [his work].
[his work]: https://github.com/jmnemo/hackathon-event/
2019-05-06 15:15:39 +00:00
|
|
|
{
|
|
|
|
test: /\.svg$/,
|
|
|
|
exclude: /node_modules/,
|
|
|
|
loader: "svg-react-loader",
|
|
|
|
},
|
2018-07-30 23:22:55 +00:00
|
|
|
// "file" loader makes sure assets end up in the `build` folder.
|
|
|
|
// When you `import` an asset, you get its filename.
|
|
|
|
// This loader doesn't use a "test" so it will catch all modules
|
|
|
|
// that fall through the other loaders.
|
|
|
|
{
|
|
|
|
loader: require.resolve("file-loader"),
|
|
|
|
// Exclude `js` files to keep "css" loader working as it injects
|
|
|
|
// it's runtime that would otherwise processed through "file" loader.
|
|
|
|
// Also exclude `html` and `json` extensions so they get processed
|
|
|
|
// by webpacks internal loaders.
|
|
|
|
exclude: [/\.(js|jsx|mjs)$/, /\.html$/, /\.json$/],
|
|
|
|
options: {
|
|
|
|
name: "static/media/[name].[hash:8].[ext]",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// ** STOP ** Are you adding a new loader?
|
|
|
|
// Make sure to add the new loader(s) before the "file" loader.
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2019-07-16 18:31:11 +00:00
|
|
|
plugins: await plugins(mode),
|
2018-07-30 23:22:55 +00:00
|
|
|
// Some libraries import Node modules but don't use them in the browser.
|
|
|
|
// Tell Webpack to provide empty mocks for them so importing them works.
|
|
|
|
node: {
|
|
|
|
dgram: "empty",
|
|
|
|
fs: "empty",
|
|
|
|
net: "empty",
|
|
|
|
tls: "empty",
|
|
|
|
child_process: "empty",
|
|
|
|
},
|
2019-07-11 04:41:26 +00:00
|
|
|
mode,
|
2018-07-30 23:22:55 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-07-16 18:31:11 +00:00
|
|
|
async function plugins(mode /*: "development" | "production" */) {
|
|
|
|
const projectIds = await loadProjectIds();
|
|
|
|
const env = getClientEnvironment(projectIds);
|
2018-07-31 00:11:30 +00:00
|
|
|
const basePlugins = [
|
|
|
|
new StaticSiteGeneratorPlugin({
|
|
|
|
entry: "ssr",
|
2018-11-01 22:19:52 +00:00
|
|
|
paths: require("../src/homepage/routeData")
|
2019-07-16 18:31:11 +00:00
|
|
|
.makeRouteData(projectIds)
|
2018-11-01 22:19:52 +00:00
|
|
|
.map(({path}) => path),
|
2018-07-31 00:11:30 +00:00
|
|
|
locals: {},
|
|
|
|
}),
|
2018-08-10 20:15:49 +00:00
|
|
|
new CopyPlugin([{from: paths.favicon, to: "favicon.png"}]),
|
2018-07-31 00:11:30 +00:00
|
|
|
// Makes some environment variables available to the JS code, for example:
|
|
|
|
// if (process.env.NODE_ENV === 'production') { ... }. See `./env.js`.
|
|
|
|
// It is absolutely essential that NODE_ENV was set to production here.
|
|
|
|
// Otherwise React will be compiled in the very slow development mode.
|
|
|
|
new webpack.DefinePlugin(env.stringified),
|
|
|
|
// Generate a manifest file which contains a mapping of all asset filenames
|
|
|
|
// to their corresponding output file so that tools can pick it up without
|
|
|
|
// having to parse `index.html`.
|
|
|
|
new ManifestPlugin({
|
|
|
|
fileName: "asset-manifest.json",
|
|
|
|
}),
|
|
|
|
// Moment.js is an extremely popular library that bundles large locale files
|
|
|
|
// by default due to how Webpack interprets its code. This is a practical
|
|
|
|
// solution that requires the user to opt into importing specific locales.
|
|
|
|
// https://github.com/jmblog/how-to-optimize-momentjs-with-webpack
|
|
|
|
// You can remove this if you don't use Moment.js:
|
|
|
|
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
|
|
|
|
];
|
|
|
|
const prodOnlyPlugins = [
|
2018-07-31 01:01:47 +00:00
|
|
|
// Remove the output directory before starting the build.
|
2018-07-31 22:27:32 +00:00
|
|
|
new RemoveBuildDirectoryPlugin(),
|
2018-07-31 00:11:30 +00:00
|
|
|
];
|
|
|
|
switch (mode) {
|
|
|
|
case "development":
|
|
|
|
return basePlugins;
|
|
|
|
case "production":
|
|
|
|
return basePlugins.concat(prodOnlyPlugins);
|
|
|
|
default:
|
|
|
|
throw new Error(/*:: (*/ mode /*: empty) */);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-30 23:22:55 +00:00
|
|
|
function getMode() {
|
|
|
|
const mode = process.env.NODE_ENV;
|
|
|
|
if (mode !== "production" && mode !== "development") {
|
|
|
|
throw new Error("unknown mode: " + String(mode));
|
|
|
|
}
|
|
|
|
return mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = makeConfig(getMode());
|