This project is bootstrapped with Create Elm App.
Below you will find some information on how to perform basic tasks.
You can find the most recent version of this guide here.
- Sending feedback
- Folder structure
- Installing Elm packages
- Installing JavaScript packages
- Available scripts
- Changing the Page
<title>
- Adding a Stylesheet
- Adding Images and Fonts
- Using the
public
Folder - Setting up API Proxy
- Running tests
- Deployment
- IDE setup for Hot Module Replacement
You are very welcome with any feedback
node elm package install <package-name>
To use JavaScript packages from npm, you'll need to add a package.json
, install the dependencies, and you're ready to go.
npm init -y # Add package.json
npm install --save-dev pouchdb-browser # Install library from npm
// Use in your JS code
import PouchDB from 'pouchdb-browser';
const db = new PouchDB('mydb');
my-app/
.gitignore
README.md
elm-package.json
src/
App.elm
favicon.ico
index.html
index.js
main.css
tests/
elm-package.json
Main.elm
Tests.elm
For the project to build, these files must exist with exact filenames:
src/index.html
is the page template;src/favicon.ico
is the icon you see in the browser tab;src/index.js
is the JavaScript entry point.
You can delete or rename the other files.
You may create subdirectories inside src.
In the project directory you can run:
Builds the app for production to the build
folder.
The build is minified, and the filenames include the hashes.
Your app is ready to be deployed!
Runs the app in the development mode.
Open http://localhost:3000 to view it in the browser.
The page will reload if you make edits.
You will also see any lint errors in the console.
Run tests with node-test-runner
You can make test runner watch project files by running:
node elm test --watch
Note: this is a one-way operation. Once you eject
, you can’t go back!
If you aren’t satisfied with the build tool and configuration choices, you can eject
at any time.
Instead, it will copy all the configuration files and the transitive dependencies (Webpack, Elm Platform, etc.) right into your project, so you have full control over them. All of the commands except eject
will still work, but they will point to the copied scripts so you can tweak them. At this point, you’re on your own.
You don’t have to use 'eject' The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However, we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
Create Elm App does not rely on the global installation of Elm Platform, but you still can use it's local Elm Platform to access default command line tools:
Alias for elm-package
Use it for installing Elm packages from package.elm-lang.org
Alias for elm-repl
Alias for elm-make
Alias for elm-reactor
You can find the source HTML file in the public
folder of the generated project. You may edit the <title>
tag in it to change the title from “Elm App” to anything else.
Note that normally you wouldn’t edit files in the public
folder very often. For example, adding a stylesheet is done without touching the HTML.
If you need to dynamically update the page title based on the content, you can use the browser document.title
API and ports.
This project setup uses Webpack for handling all assets. Webpack offers a custom way of “extending” the concept of import
beyond JavaScript. To express that a JavaScript file depends on a CSS file, you need to import the CSS from the JavaScript file:
body {
padding: 20px;
}
import './main.css'; // Tell Webpack to pick-up the styles from base.css
In development, expressing dependencies this way allows your styles to be reloaded on the fly as you edit them. In production, all CSS files will be concatenated into a single minified .css
file in the build output.
You can put all your CSS right into src/main.css
. It would still be imported from src/index.js
, but you could always remove that import if you later migrate to a different build tool.
With Webpack, using static assets like images and fonts works similarly to CSS.
By requiring an image in JavaScript code, you tell Webpack to add a file to the build of your application. The variable will contain a unique path to the said file.
Here is an example:
import logoPath from './logo.svg'; // Tell Webpack this JS file uses this image
import { Main } from './Main.elm';
Main.embed(
document.getElementById('root'),
logoPath // Pass image path as a flag for Html.programWithFlags
);
Later on, you can use the image path in your view for displaying it in the DOM.
view : Model -> Html Msg
view model =
div []
[ img [ src model.logo ] []
, div [] [ text model.message ]
]
The public
folder contains the HTML file so you can tweak it, for example, to set the page title.
The <script>
tag with the compiled code will be added to it automatically during the build process.
You can also add other assets to the public
folder.
Note that we normally encourage you to import
assets in JavaScript files instead.
For example, see the sections on adding a stylesheet and adding images and fonts.
This mechanism provides a number of benefits:
- Scripts and stylesheets get minified and bundled together to avoid extra network requests.
- Missing files cause compilation errors instead of 404 errors for your users.
- Result filenames include content hashes so you don’t need to worry about browsers caching their old versions.
However there is an escape hatch that you can use to add an asset outside of the module system.
If you put a file into the public
folder, it will not be processed by Webpack. Instead it will be copied into the build folder untouched. To reference assets in the public
folder, you need to use a special variable called PUBLIC_URL
.
Inside index.html
, you can use it like this:
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
Only files inside the public
folder will be accessible by %PUBLIC_URL%
prefix. If you need to use a file from src
or node_modules
, you’ll have to copy it there to explicitly specify your intention to make this file a part of the build.
When you run npm run build
, Create React App will substitute %PUBLIC_URL%
with a correct absolute path so your project works even if you use client-side routing or host it at a non-root URL.
In Elm code, you can use %PUBLIC_URL%
for similar purposes:
// Note: this is an escape hatch and should be used sparingly!
// Normally we recommend using `import` and `Html.programWithFlags` for getting
// asset URLs as described in “Adding Images and Fonts” above this section.
img [ src "%PUBLIC_PATH%/logo.svg" ] []
In JavaScript code, you can use process.env.PUBLIC_URL
for similar purposes:
const logo = `<img src=${process.env.PUBLIC_URL + '/img/logo.svg'} />`;
Keep in mind the downsides of this approach:
- None of the files in
public
folder get post-processed or minified. - Missing files will not be called at compilation time, and will cause 404 errors for your users.
- Result filenames won’t include content hashes so you’ll need to add query arguments or rename them every time they change.
Normally we recommend importing stylesheets, images, and fonts from JavaScript.
The public
folder is useful as a workaround for a number of less common cases:
- You need a file with a specific name in the build output, such as
manifest.webmanifest
. - You have thousands of images and need to dynamically reference their paths.
- You want to include a small script like
pace.js
outside of the bundled code. - Some library may be incompatible with Webpack and you have no other option but to include it as a
<script>
tag.
Note that if you add a <script>
that declares global variables, you also need to read the next section on using them.
To forward the API ( REST ) calls to backend server, add a proxy to the elm-package.json
in the top level json object.
{
...
"proxy" : "http://localhost:1313",
...
}
Make sure the XHR requests set the Content-type: application/json
and Accept: application/json
.
The development server has heuristics, to handle it's own flow, which may interfere with proxying of
other html and javascript content types.
curl -X GET -H "Content-type: application/json" -H "Accept: application/json" http://localhost:3000/api/list
Create Elm App uses elm-test as its test runner.
To use packages in tests, you also need to install them in tests
directory.
node elm test --add-dependencies elm-package.json
node elm build
creates a build
directory with a production build of your app. Set up your favourite HTTP server so that a visitor to your site is served index.html
, and requests to static paths like /static/js/main.<hash>.js
are served with the contents of the /static/js/main.<hash>.js
file.
For environments using Node, the easiest way to handle this would be to install serve and let it handle the rest:
npm install -g serve
serve -s build
The last command shown above will serve your static site on the port 5000. Like many of serve’s internal settings, the port can be adjusted using the -p
or --port
flags.
Run this command to get a full list of the options available:
serve -h
Note: this feature is available with
react-scripts@0.2.0
and higher.
The step below is important!
If you skip it, your app will not deploy correctly.
Open your package.json
and add a homepage
field:
"homepage": "https://myusername.github.io/my-app",
Create React App uses the homepage
field to determine the root URL in the built HTML file.
The predeploy
script will run automatically before deploy
is run.
Then run:
gh-pages -d build
Finally, make sure GitHub Pages option in your GitHub project settings is set to use the gh-pages
branch:
You can configure a custom domain with GitHub Pages by adding a CNAME
file to the public/
folder.
GitHub Pages doesn’t support routers that use the HTML5 pushState
history API under the hood (for example, React Router using browserHistory
). This is because when there is a fresh page load for a url like http://user.github.io/todomvc/todos/42
, where /todos/42
is a frontend route, the GitHub Pages server returns 404 because it knows nothing of /todos/42
. If you want to add a router to a project hosted on GitHub Pages, here are a couple of solutions:
- You could switch from using HTML5 history API to routing with hashes. If you use React Router, you can switch to
hashHistory
for this effect, but the URL will be longer and more verbose (for example,http://user.github.io/todomvc/#/todos/42?_k=yknaj
). Read more about different history implementations in React Router. - Alternatively, you can use a trick to teach GitHub Pages to handle 404 by redirecting to your
index.html
page with a special redirect parameter. You would need to add a404.html
file with the redirection code to thebuild
folder before deploying your project, and you’ll need to add code handling the redirect parameter toindex.html
. You can find a detailed explanation of this technique in this guide.
Remember to disable safe write if you are using VIM or IntelliJ IDE, such as WebStorm.