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-11-02 01:11:48 +00:00
|
|
|
import type {RepoIdRegistry} from "../src/core/repoIdRegistry";
|
2018-08-07 18:17:03 +00:00
|
|
|
*/
|
2018-11-01 19:38:18 +00:00
|
|
|
const fs = require("fs");
|
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");
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
function loadRepoRegistry() /*: RepoIdRegistry */ {
|
|
|
|
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;
|
|
|
|
// TODO(@dandelion): Remove hacks around compat usage here
|
|
|
|
// TODO(@dandelion): Import rather than hardcode the registry file name
|
|
|
|
const registryFile = path.join(scDirectory, "repositoryRegistry.json");
|
|
|
|
|
|
|
|
let jsonString;
|
|
|
|
try {
|
|
|
|
jsonString = fs.readFileSync(registryFile).toString();
|
|
|
|
} catch (e) {
|
|
|
|
if (e.code === "ENOENT") {
|
|
|
|
jsonString = JSON.stringify([
|
2018-11-10 01:28:39 +00:00
|
|
|
{version: "0.2.0", type: "REPO_ID_REGISTRY"},
|
2018-11-01 19:38:18 +00:00
|
|
|
[],
|
|
|
|
]);
|
|
|
|
} else {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const json = JSON.parse(jsonString);
|
|
|
|
const compat = json[0];
|
2018-11-10 01:28:39 +00:00
|
|
|
if (compat.version !== "0.2.0" || compat.type !== "REPO_ID_REGISTRY") {
|
2018-11-01 19:38:18 +00:00
|
|
|
throw new Error("Compat mismatch");
|
|
|
|
}
|
|
|
|
return json[1];
|
|
|
|
}
|
2018-11-01 22:19:52 +00:00
|
|
|
const repoRegistry = loadRepoRegistry();
|
2018-11-01 19:38:18 +00:00
|
|
|
|
2018-07-30 23:15:30 +00:00
|
|
|
// Get environment variables to inject into our app.
|
2018-11-01 22:19:52 +00:00
|
|
|
const env = getClientEnvironment(repoRegistry);
|
2018-07-30 23:15:30 +00:00
|
|
|
|
2018-11-09 17:24:40 +00:00
|
|
|
function makeConfig(mode /*: "production" | "development" */) /*: 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",
|
|
|
|
},
|
|
|
|
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.
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2018-07-31 00:11:30 +00:00
|
|
|
plugins: 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",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-07-31 00:11:30 +00:00
|
|
|
function plugins(mode /*: "development" | "production" */) {
|
|
|
|
const basePlugins = [
|
|
|
|
new StaticSiteGeneratorPlugin({
|
|
|
|
entry: "ssr",
|
2018-11-01 22:19:52 +00:00
|
|
|
paths: require("../src/homepage/routeData")
|
|
|
|
.makeRouteData(repoRegistry)
|
|
|
|
.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
|
|
|
// Minify the code.
|
|
|
|
new webpack.optimize.UglifyJsPlugin({
|
|
|
|
compress: {
|
|
|
|
warnings: false,
|
|
|
|
// Disabled because of an issue with Uglify breaking seemingly valid code:
|
|
|
|
// https://github.com/facebookincubator/create-react-app/issues/2376
|
|
|
|
// Pending further investigation:
|
|
|
|
// https://github.com/mishoo/UglifyJS2/issues/2011
|
|
|
|
comparisons: false,
|
|
|
|
},
|
|
|
|
mangle: {
|
|
|
|
safari10: true,
|
|
|
|
},
|
|
|
|
output: {
|
|
|
|
comments: false,
|
|
|
|
// Turned on because emoji and regex is not minified properly using default
|
|
|
|
// https://github.com/facebookincubator/create-react-app/issues/2488
|
|
|
|
ascii_only: true,
|
|
|
|
},
|
|
|
|
sourceMap: shouldUseSourceMap,
|
|
|
|
}),
|
|
|
|
];
|
|
|
|
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());
|