Skip to content

Commit 588fe31

Browse files
authored
Merge pull request #310 from halfzebra/introducing-config-file
feat: Itroduced an option of having a config for "homepage" and "proxy"
2 parents cdc30b9 + be8c7bf commit 588fe31

File tree

7 files changed

+71
-19
lines changed

7 files changed

+71
-19
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ Inspired by [create-react-app](https://github.com/facebookincubator/create-react
111111

112112
* **One Dependency:** There is just one build dependency. It uses Elm Platform, Webpack, and other amazing projects, but provides a cohesive curated experience on top of them.
113113

114-
* **Zero Configuration:** There are no configuration files or command line options. Configuring both development and production builds is handled for you so you can focus on writing code.
114+
* **Zero Configuration:** No configuration files or command line options required to start working with Elm. Configuring both development and production builds is handled for you so you can focus on writing code.
115115

116116
* **No Lock-In:** You can “eject” to a custom setup at any time. Run a single command, and all the configuration and build dependencies will be moved directly into your project, so you can pick up right where you left off.
117117

config/paths.js

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,34 @@
33
const path = require('path');
44
const fs = require('fs');
55
const url = require('url');
6+
const cosmiconfig = require('cosmiconfig');
67

78
// Make sure any symlinks in the project folder are resolved:
89
// https://github.com/facebookincubator/create-react-app/issues/637
910
const appDirectory = fs.realpathSync(process.cwd());
1011
const resolveApp = relativePath => path.resolve(appDirectory, relativePath);
1112

13+
// We look for configration in files supported by cosmiconfig by default:
14+
// https://github.com/davidtheclark/cosmiconfig
15+
const explorer = cosmiconfig('elmapp');
16+
const result = explorer.searchSync(appDirectory);
17+
const config = result ? result.config : loadElmJson();
18+
19+
// WARNING:
20+
// We support config in elm.json only for legacy reasons.
21+
// elm-package removes the settings, so this will be removed in the future.
22+
function loadElmJson() {
23+
try {
24+
const elmJson = require(resolveApp('elm.json'));
25+
if (elmJson.homepage || elmJson.proxy) {
26+
return elmJson;
27+
}
28+
} catch (error) {
29+
return {};
30+
}
31+
return {};
32+
}
33+
1234
const envPublicUrl = process.env.PUBLIC_URL;
1335

1436
function ensureSlash(path, needsSlash) {
@@ -21,17 +43,21 @@ function ensureSlash(path, needsSlash) {
2143
return path;
2244
}
2345

24-
const getPublicUrl = appPackageJson =>
25-
envPublicUrl || require(appPackageJson).homepage;
46+
const getPublicUrl = appConfig => {
47+
if (envPublicUrl) {
48+
return envPublicUrl;
49+
}
50+
return appConfig.homepage;
51+
};
2652

2753
// We use `PUBLIC_URL` environment variable or "homepage" field to infer
2854
// "public path" at which the app is served.
2955
// Webpack needs to know it to put the right <script> hrefs into HTML even in
3056
// single-page apps that may serve index.html for nested URLs like /todos/42.
3157
// We can't use a relative path in HTML because we don't want to load something
3258
// like /todos/42/static/js/bundle.7289d.js. We have to know the root.
33-
function getServedPath(appPackageJson) {
34-
const publicUrl = getPublicUrl(appPackageJson);
59+
function getServedPath(appConfig) {
60+
const publicUrl = getPublicUrl(appConfig);
3561
const servedUrl =
3662
envPublicUrl || (publicUrl ? url.parse(publicUrl).pathname : '/');
3763
return ensureSlash(servedUrl, true);
@@ -48,6 +74,7 @@ module.exports = {
4874
appBuild: resolveApp('./build'),
4975
elmJson: resolveApp('./elm.json'),
5076
elm: require.resolve('elm/bin/elm'),
51-
publicUrl: getPublicUrl(resolveApp('elm.json')),
52-
servedPath: getServedPath(resolveApp('elm.json'))
77+
publicUrl: getPublicUrl(config),
78+
servedPath: getServedPath(config),
79+
proxy: config.proxy
5380
};

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"clean-webpack-plugin": "^0.1.18",
3131
"cli-table": "0.3.1",
3232
"connect-history-api-fallback": "^1.5.0",
33+
"cosmiconfig": "^5.0.6",
3334
"cross-spawn": "^6.0.5",
3435
"css-loader": "^0.28.9",
3536
"dotenv": "^5.0.0",

scripts/build.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const checkRequiredFiles = require('react-dev-utils/checkRequiredFiles');
2323
const formatWebpackMessages = require('react-dev-utils/formatWebpackMessages');
2424
const FileSizeReporter = require('react-dev-utils/FileSizeReporter');
2525
const formatElmCompilerErrors = require('./utils/formatElmCompilerErrors');
26+
const warn = require('./utils/warn');
2627

2728
const measureFileSizesBeforeBuild =
2829
FileSizeReporter.measureFileSizesBeforeBuild;
@@ -72,6 +73,7 @@ measureFileSizesBeforeBuild(paths.appBuild)
7273
console.log('File sizes after gzip:\n');
7374
printFileSizesAfterBuild(stats, previousFileSizes, paths.appBuild);
7475
console.log();
76+
warn(paths.elmJson);
7577
},
7678
err => {
7779
console.error(chalk.red('Failed to compile.\n'));
@@ -83,6 +85,7 @@ measureFileSizesBeforeBuild(paths.appBuild)
8385
// Create the production build and print the deployment instructions.
8486
function build(previousFileSizes) {
8587
const withDebugger = process.env.ELM_DEBUGGER === 'true' ? true : false;
88+
console.log();
8689
if (withDebugger) {
8790
console.log(
8891
`Creating a ${process.env.NODE_ENV} build with debugger enabled...`

scripts/start.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const openBrowser = require('react-dev-utils/openBrowser');
3131
const createDevServerConfig = require('../config/webpackDevServer.config');
3232
const formatElmCompilerErrors = require('./utils/formatElmCompilerErrors');
3333
const paths = require('../config/paths');
34+
const warn = require('./utils/warn');
3435

3536
if (fs.existsSync('elm.json') === false) {
3637
console.log('Please, run the build script from project root directory');
@@ -108,8 +109,10 @@ function createCompiler(webpack, config, appName, urls) {
108109
if (isSuccessful) {
109110
console.log(chalk.green('Compiled successfully!'));
110111
}
112+
111113
if (isSuccessful && (isInteractive || isFirstCompile)) {
112114
printInstructions(appName, urls);
115+
warn(paths.elmJson);
113116
}
114117
isFirstCompile = false;
115118

@@ -159,8 +162,7 @@ choosePort(HOST, DEFAULT_PORT)
159162
// Create a webpack compiler that is configured with custom messages.
160163
const compiler = createCompiler(webpack, config, appName, urls);
161164
// Load proxy config
162-
const proxySetting = require(paths.elmJson).proxy;
163-
const proxyConfig = prepareProxy(proxySetting, paths.appPublic);
165+
const proxyConfig = prepareProxy(paths.proxy, paths.appPublic);
164166
// Serve webpack assets generated by the compiler over a web sever.
165167
const serverConfig = createDevServerConfig(
166168
proxyConfig,

scripts/utils/warn.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const chalk = require('chalk');
2+
3+
module.exports = function warn(elmJsonPath) {
4+
const elmJson = require(elmJsonPath);
5+
if (elmJson.homepage || elmJson.proxy) {
6+
console.log();
7+
console.log(chalk.yellow('Warning:'));
8+
console.log();
9+
console.log(
10+
' Using elm.json for configuring "homebage" and "proxy" is deprecated.'
11+
);
12+
console.log(' This feature will be removed in the future versions.');
13+
console.log();
14+
}
15+
};

template/README.md

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ You can find the most recent version of this guide [here](https://github.com/hal
4747
* [Deployment](#deployment)
4848
* [Building for Relative Paths](#building-for-relative-paths)
4949
* [Static Server](#static-server)
50-
* [Netlify](#netlify)
50+
* [Netlify](#netlify)
5151
* [GitHub Pages](#github-pages)
5252
* [IDE setup for Hot Module Replacement](#ide-setup-for-hot-module-replacement)
5353

@@ -645,12 +645,12 @@ for a list of files you can use to declare environment variables.
645645

646646
## Setting up API Proxy
647647

648-
To forward the API ( REST ) calls to backend server, add a proxy to the `elm.json` in the top level json object.
648+
To forward the API ( REST ) calls to backend server, add a proxy to the `elmapp.config.js` in the top level json object.
649649

650-
```json
651-
{
650+
```js
651+
module.exports = {
652652
...
653-
"proxy" : "http://localhost:1313",
653+
proxy: "http://localhost:1313",
654654
...
655655
}
656656
```
@@ -767,10 +767,12 @@ will affect your users' experience.
767767

768768
By default, Create Elm App produces a build assuming your app is hosted at the server root.
769769

770-
To override this, specify the `homepage` in your `elm.json`, for example:
770+
To override this, specify the `homepage` in your `elmapp.config.js`, for example:
771771

772772
```js
773-
"homepage": "http://mywebsite.com/relativepath",
773+
module.exports = {
774+
homepage: "http://mywebsite.com/relativepath"
775+
}
774776
```
775777

776778
This will let Create Elm App correctly infer the root path to use in the generated HTML file.
@@ -809,16 +811,18 @@ This step is important to make sure netlify uses the correct build command.
809811
810812
### GitHub Pages
811813
812-
#### Step 1: Add `homepage` to `elm.json`
814+
#### Step 1: Add `homepage` to `elmapp.config.js`
813815
814816
**The step below is important!**
815817
816818
**If you skip it, your app will not deploy correctly.**
817819
818-
Open your `elm.json` and add a `homepage` field:
820+
Open your `elmapp.config.js` and add a `homepage` field:
819821
820822
```js
821-
"homepage": "https://myusername.github.io/my-app",
823+
module.exports = {
824+
homepage": "https://myusername.github.io/my-app",
825+
}
822826
```
823827

824828
Create Elm App uses the `homepage` field to determine the root URL in the built HTML file.

0 commit comments

Comments
 (0)