Skip to content

Commit 61721b5

Browse files
committed
feat: Itroduced an option of having a config for "homepage" and "proxy"
Now it is possible to add a config file elmapp.config.js which could contain some configuration.
1 parent cdc30b9 commit 61721b5

File tree

3 files changed

+42
-18
lines changed

3 files changed

+42
-18
lines changed

config/paths.js

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,42 @@ function ensureSlash(path, needsSlash) {
2121
return path;
2222
}
2323

24-
const getPublicUrl = appPackageJson =>
25-
envPublicUrl || require(appPackageJson).homepage;
24+
const getPublicUrl = appConfig => {
25+
if (envPublicUrl) {
26+
return envPublicUrl;
27+
}
28+
29+
try {
30+
return require(appConfig).homepage;
31+
} catch (error) {
32+
return;
33+
}
34+
};
2635

2736
// We use `PUBLIC_URL` environment variable or "homepage" field to infer
2837
// "public path" at which the app is served.
2938
// Webpack needs to know it to put the right <script> hrefs into HTML even in
3039
// single-page apps that may serve index.html for nested URLs like /todos/42.
3140
// We can't use a relative path in HTML because we don't want to load something
3241
// like /todos/42/static/js/bundle.7289d.js. We have to know the root.
33-
function getServedPath(appPackageJson) {
34-
const publicUrl = getPublicUrl(appPackageJson);
42+
function getServedPath(appConfig) {
43+
const publicUrl = getPublicUrl(appConfig);
3544
const servedUrl =
3645
envPublicUrl || (publicUrl ? url.parse(publicUrl).pathname : '/');
3746
return ensureSlash(servedUrl, true);
3847
}
3948

49+
function getProxySettings(appConfig) {
50+
try {
51+
return require(appConfig).proxy;
52+
} catch (error) {
53+
return;
54+
}
55+
}
56+
57+
// This file is used to store the configuration such as "homepage" and "proxy"
58+
const appConfig = resolveApp('./elmapp.config.js');
59+
4060
module.exports = {
4161
appPath: resolveApp('.'),
4262
appPublic: resolveApp('./public'),
@@ -48,6 +68,7 @@ module.exports = {
4868
appBuild: resolveApp('./build'),
4969
elmJson: resolveApp('./elm.json'),
5070
elm: require.resolve('elm/bin/elm'),
51-
publicUrl: getPublicUrl(resolveApp('elm.json')),
52-
servedPath: getServedPath(resolveApp('elm.json'))
71+
publicUrl: getPublicUrl(appConfig),
72+
servedPath: getServedPath(appConfig),
73+
proxy: getProxySettings(appConfig)
5374
};

scripts/start.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,7 @@ choosePort(HOST, DEFAULT_PORT)
159159
// Create a webpack compiler that is configured with custom messages.
160160
const compiler = createCompiler(webpack, config, appName, urls);
161161
// Load proxy config
162-
const proxySetting = require(paths.elmJson).proxy;
163-
const proxyConfig = prepareProxy(proxySetting, paths.appPublic);
162+
const proxyConfig = prepareProxy(paths.proxy, paths.appPublic);
164163
// Serve webpack assets generated by the compiler over a web sever.
165164
const serverConfig = createDevServerConfig(
166165
proxyConfig,

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)