Skip to content

Commit 2b72e91

Browse files
committed
Eject from create-react-app so we can configure Relay/GraphQL.
See: facebook/create-react-app#462
1 parent ba8a14b commit 2b72e91

11 files changed

+1211
-6
lines changed

web/config/env.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Grab NODE_ENV and REACT_APP_* environment variables and prepare them to be
2+
// injected into the application via DefinePlugin in Webpack configuration.
3+
4+
var REACT_APP = /^REACT_APP_/i;
5+
6+
function getClientEnvironment(publicUrl) {
7+
var processEnv = Object
8+
.keys(process.env)
9+
.filter(key => REACT_APP.test(key))
10+
.reduce((env, key) => {
11+
env[key] = JSON.stringify(process.env[key]);
12+
return env;
13+
}, {
14+
// Useful for determining whether we’re running in production mode.
15+
// Most importantly, it switches React into the correct mode.
16+
'NODE_ENV': JSON.stringify(
17+
process.env.NODE_ENV || 'development'
18+
),
19+
// Useful for resolving the correct path to static assets in `public`.
20+
// For example, <img src={process.env.PUBLIC_URL + '/img/logo.png'} />.
21+
// This should only be used as an escape hatch. Normally you would put
22+
// images into the `src` and `import` them in code to get their paths.
23+
'PUBLIC_URL': JSON.stringify(publicUrl)
24+
});
25+
return {'process.env': processEnv};
26+
}
27+
28+
module.exports = getClientEnvironment;

web/config/jest/cssTransform.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// This is a custom Jest transformer turning style imports into empty objects.
2+
// http://facebook.github.io/jest/docs/tutorial-webpack.html
3+
4+
module.exports = {
5+
process() {
6+
return 'module.exports = {};';
7+
},
8+
getCacheKey(fileData, filename) {
9+
// The output is always the same.
10+
return 'cssTransform';
11+
},
12+
};

web/config/jest/fileTransform.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const path = require('path');
2+
3+
// This is a custom Jest transformer turning file imports into filenames.
4+
// http://facebook.github.io/jest/docs/tutorial-webpack.html
5+
6+
module.exports = {
7+
process(src, filename) {
8+
return 'module.exports = ' + JSON.stringify(path.basename(filename)) + ';';
9+
},
10+
};

web/config/paths.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
var path = require('path');
2+
var fs = require('fs');
3+
4+
// Make sure any symlinks in the project folder are resolved:
5+
// https://github.com/facebookincubator/create-react-app/issues/637
6+
var appDirectory = fs.realpathSync(process.cwd());
7+
function resolveApp(relativePath) {
8+
return path.resolve(appDirectory, relativePath);
9+
}
10+
11+
// We support resolving modules according to `NODE_PATH`.
12+
// This lets you use absolute paths in imports inside large monorepos:
13+
// https://github.com/facebookincubator/create-react-app/issues/253.
14+
15+
// It works similar to `NODE_PATH` in Node itself:
16+
// https://nodejs.org/api/modules.html#modules_loading_from_the_global_folders
17+
18+
// We will export `nodePaths` as an array of absolute paths.
19+
// It will then be used by Webpack configs.
20+
// Jest doesn’t need this because it already handles `NODE_PATH` out of the box.
21+
22+
// Note that unlike in Node, only *relative* paths from `NODE_PATH` are honored.
23+
// Otherwise, we risk importing Node.js core modules into an app instead of Webpack shims.
24+
// https://github.com/facebookincubator/create-react-app/issues/1023#issuecomment-265344421
25+
26+
var nodePaths = (process.env.NODE_PATH || '')
27+
.split(process.platform === 'win32' ? ';' : ':')
28+
.filter(Boolean)
29+
.filter(folder => !path.isAbsolute(folder))
30+
.map(resolveApp);
31+
32+
// config after eject: we're in ./config/
33+
module.exports = {
34+
appBuild: resolveApp('build'),
35+
appPublic: resolveApp('public'),
36+
appHtml: resolveApp('public/index.html'),
37+
appIndexJs: resolveApp('src/index.js'),
38+
appPackageJson: resolveApp('package.json'),
39+
appSrc: resolveApp('src'),
40+
yarnLockFile: resolveApp('yarn.lock'),
41+
testsSetup: resolveApp('src/setupTests.js'),
42+
appNodeModules: resolveApp('node_modules'),
43+
ownNodeModules: resolveApp('node_modules'),
44+
nodePaths: nodePaths
45+
};

web/config/polyfills.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
if (typeof Promise === 'undefined') {
2+
// Rejection tracking prevents a common issue where React gets into an
3+
// inconsistent state due to an error, but it gets swallowed by a Promise,
4+
// and the user has no idea what causes React's erratic future behavior.
5+
require('promise/lib/rejection-tracking').enable();
6+
window.Promise = require('promise/lib/es6-extensions.js');
7+
}
8+
9+
// fetch() polyfill for making API calls.
10+
require('whatwg-fetch');
11+
12+
// Object.assign() is commonly used with React.
13+
// It will use the native implementation if it's present and isn't buggy.
14+
Object.assign = require('object-assign');

web/config/webpack.config.dev.js

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
var autoprefixer = require('autoprefixer');
2+
var webpack = require('webpack');
3+
var HtmlWebpackPlugin = require('html-webpack-plugin');
4+
var CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin');
5+
var InterpolateHtmlPlugin = require('react-dev-utils/InterpolateHtmlPlugin');
6+
var WatchMissingNodeModulesPlugin = require('react-dev-utils/WatchMissingNodeModulesPlugin');
7+
var getClientEnvironment = require('./env');
8+
var paths = require('./paths');
9+
10+
11+
12+
// Webpack uses `publicPath` to determine where the app is being served from.
13+
// In development, we always serve from the root. This makes config easier.
14+
var publicPath = '/';
15+
// `publicUrl` is just like `publicPath`, but we will provide it to our app
16+
// as %PUBLIC_URL% in `index.html` and `process.env.PUBLIC_URL` in JavaScript.
17+
// Omit trailing slash as %PUBLIC_PATH%/xyz looks better than %PUBLIC_PATH%xyz.
18+
var publicUrl = '';
19+
// Get environment variables to inject into our app.
20+
var env = getClientEnvironment(publicUrl);
21+
22+
// This is the development configuration.
23+
// It is focused on developer experience and fast rebuilds.
24+
// The production configuration is different and lives in a separate file.
25+
module.exports = {
26+
// You may want 'eval' instead if you prefer to see the compiled output in DevTools.
27+
// See the discussion in https://github.com/facebookincubator/create-react-app/issues/343.
28+
devtool: 'cheap-module-source-map',
29+
// These are the "entry points" to our application.
30+
// This means they will be the "root" imports that are included in JS bundle.
31+
// The first two entry points enable "hot" CSS and auto-refreshes for JS.
32+
entry: [
33+
// Include an alternative client for WebpackDevServer. A client's job is to
34+
// connect to WebpackDevServer by a socket and get notified about changes.
35+
// When you save a file, the client will either apply hot updates (in case
36+
// of CSS changes), or refresh the page (in case of JS changes). When you
37+
// make a syntax error, this client will display a syntax error overlay.
38+
// Note: instead of the default WebpackDevServer client, we use a custom one
39+
// to bring better experience for Create React App users. You can replace
40+
// the line below with these two lines if you prefer the stock client:
41+
// require.resolve('webpack-dev-server/client') + '?/',
42+
// require.resolve('webpack/hot/dev-server'),
43+
require.resolve('react-dev-utils/webpackHotDevClient'),
44+
// We ship a few polyfills by default:
45+
require.resolve('./polyfills'),
46+
// Finally, this is your app's code:
47+
paths.appIndexJs
48+
// We include the app code last so that if there is a runtime error during
49+
// initialization, it doesn't blow up the WebpackDevServer client, and
50+
// changing JS code would still trigger a refresh.
51+
],
52+
output: {
53+
// Next line is not used in dev but WebpackDevServer crashes without it:
54+
path: paths.appBuild,
55+
// Add /* filename */ comments to generated require()s in the output.
56+
pathinfo: true,
57+
// This does not produce a real file. It's just the virtual path that is
58+
// served by WebpackDevServer in development. This is the JS bundle
59+
// containing code from all our entry points, and the Webpack runtime.
60+
filename: 'static/js/bundle.js',
61+
// This is the URL that app is served from. We use "/" in development.
62+
publicPath: publicPath
63+
},
64+
resolve: {
65+
// This allows you to set a fallback for where Webpack should look for modules.
66+
// We read `NODE_PATH` environment variable in `paths.js` and pass paths here.
67+
// We use `fallback` instead of `root` because we want `node_modules` to "win"
68+
// if there any conflicts. This matches Node resolution mechanism.
69+
// https://github.com/facebookincubator/create-react-app/issues/253
70+
fallback: paths.nodePaths,
71+
// These are the reasonable defaults supported by the Node ecosystem.
72+
// We also include JSX as a common component filename extension to support
73+
// some tools, although we do not recommend using it, see:
74+
// https://github.com/facebookincubator/create-react-app/issues/290
75+
extensions: ['.js', '.json', '.jsx', ''],
76+
alias: {
77+
// Support React Native Web
78+
// https://www.smashingmagazine.com/2016/08/a-glimpse-into-the-future-with-react-native-for-web/
79+
'react-native': 'react-native-web'
80+
}
81+
},
82+
83+
module: {
84+
// First, run the linter.
85+
// It's important to do this before Babel processes the JS.
86+
preLoaders: [
87+
{
88+
test: /\.(js|jsx)$/,
89+
loader: 'eslint',
90+
include: paths.appSrc,
91+
}
92+
],
93+
loaders: [
94+
// Default loader: load all assets that are not handled
95+
// by other loaders with the url loader.
96+
// Note: This list needs to be updated with every change of extensions
97+
// the other loaders match.
98+
// E.g., when adding a loader for a new supported file extension,
99+
// we need to add the supported extension to this loader too.
100+
// Add one new line in `exclude` for each loader.
101+
//
102+
// "file" loader makes sure those assets get served by WebpackDevServer.
103+
// When you `import` an asset, you get its (virtual) filename.
104+
// In production, they would get copied to the `build` folder.
105+
// "url" loader works like "file" loader except that it embeds assets
106+
// smaller than specified limit in bytes as data URLs to avoid requests.
107+
// A missing `test` is equivalent to a match.
108+
{
109+
exclude: [
110+
/\.html$/,
111+
/\.(js|jsx)$/,
112+
/\.css$/,
113+
/\.json$/,
114+
/\.svg$/
115+
],
116+
loader: 'url',
117+
query: {
118+
limit: 10000,
119+
name: 'static/media/[name].[hash:8].[ext]'
120+
}
121+
},
122+
// Process JS with Babel.
123+
{
124+
test: /\.(js|jsx)$/,
125+
include: paths.appSrc,
126+
loader: 'babel',
127+
query: {
128+
129+
// This is a feature of `babel-loader` for webpack (not Babel itself).
130+
// It enables caching results in ./node_modules/.cache/babel-loader/
131+
// directory for faster rebuilds.
132+
cacheDirectory: true
133+
}
134+
},
135+
// "postcss" loader applies autoprefixer to our CSS.
136+
// "css" loader resolves paths in CSS and adds assets as dependencies.
137+
// "style" loader turns CSS into JS modules that inject <style> tags.
138+
// In production, we use a plugin to extract that CSS to a file, but
139+
// in development "style" loader enables hot editing of CSS.
140+
{
141+
test: /\.css$/,
142+
loader: 'style!css?importLoaders=1!postcss'
143+
},
144+
// JSON is not enabled by default in Webpack but both Node and Browserify
145+
// allow it implicitly so we also enable it.
146+
{
147+
test: /\.json$/,
148+
loader: 'json'
149+
},
150+
// "file" loader for svg
151+
{
152+
test: /\.svg$/,
153+
loader: 'file',
154+
query: {
155+
name: 'static/media/[name].[hash:8].[ext]'
156+
}
157+
}
158+
]
159+
},
160+
161+
// We use PostCSS for autoprefixing only.
162+
postcss: function() {
163+
return [
164+
autoprefixer({
165+
browsers: [
166+
'>1%',
167+
'last 4 versions',
168+
'Firefox ESR',
169+
'not ie < 9', // React doesn't support IE8 anyway
170+
]
171+
}),
172+
];
173+
},
174+
plugins: [
175+
// Makes the public URL available as %PUBLIC_URL% in index.html, e.g.:
176+
// <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
177+
// In development, this will be an empty string.
178+
new InterpolateHtmlPlugin({
179+
PUBLIC_URL: publicUrl
180+
}),
181+
// Generates an `index.html` file with the <script> injected.
182+
new HtmlWebpackPlugin({
183+
inject: true,
184+
template: paths.appHtml,
185+
}),
186+
// Makes some environment variables available to the JS code, for example:
187+
// if (process.env.NODE_ENV === 'development') { ... }. See `./env.js`.
188+
new webpack.DefinePlugin(env),
189+
// This is necessary to emit hot updates (currently CSS only):
190+
new webpack.HotModuleReplacementPlugin(),
191+
// Watcher doesn't work well if you mistype casing in a path so we use
192+
// a plugin that prints an error when you attempt to do this.
193+
// See https://github.com/facebookincubator/create-react-app/issues/240
194+
new CaseSensitivePathsPlugin(),
195+
// If you require a missing module and then `npm install` it, you still have
196+
// to restart the development server for Webpack to discover it. This plugin
197+
// makes the discovery automatic so you don't have to restart.
198+
// See https://github.com/facebookincubator/create-react-app/issues/186
199+
new WatchMissingNodeModulesPlugin(paths.appNodeModules)
200+
],
201+
// Some libraries import Node modules but don't use them in the browser.
202+
// Tell Webpack to provide empty mocks for them so importing them works.
203+
node: {
204+
fs: 'empty',
205+
net: 'empty',
206+
tls: 'empty'
207+
}
208+
};

0 commit comments

Comments
 (0)