From 947cdf604b40a40d3b945aba1e6dcc90030e6eb5 Mon Sep 17 00:00:00 2001 From: rundis Date: Sat, 27 Feb 2016 21:11:36 +0100 Subject: [PATCH] Add spa option using connect-history-api-fallback --- README.md | 8 ++++++++ bin.js | 4 +++- index.js | 8 +++++++- package.json | 1 + 4 files changed, 19 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a9e12bc..efa0e5a 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,7 @@ $ elm-server path/to/Main.elm -V, --version output the version number -o, --output Path to elm-make output [index.html]. -s, --start-path Initial path when opening browser. + -a. --spa Set to something to enable single page application mode -w, --watch Path to served directory. Defaults to directory of output file path. ``` @@ -59,6 +60,13 @@ $ elm-server src/elm/Main.elm \ --start-path html/index.html ``` +###### spa +When you have a Single Page Application with routing on the client side, this mode is for you. It uses the [connect-history-api-fallback](https://github.com/bripkens/connect-history-api-fallback) library. + +It currently assumes that you are using index.html as the entry point for your app. + + + #### Programmatic Usage In addition to providing a cli, elm-server can also be called programmatically. diff --git a/bin.js b/bin.js index 42a922e..97f3563 100755 --- a/bin.js +++ b/bin.js @@ -9,6 +9,7 @@ commander .version(version) .option('-o, --output ', 'Path to elm-make output [index.html].') .option('-s, --start-path ', 'Initial path when opening browser.') + .option('-a, --spa ', 'Set to whatever to enable spa mode.') .usage('[options] [inputFiles...]'); var descriptionWidth = 35; @@ -34,5 +35,6 @@ if (!commander.args.length) { elmServer(commander.args, { output: commander.output, startPath: commander.startPath, - watch: commander.watch + watch: commander.watch, + spa: commander.spa }); diff --git a/index.js b/index.js index 5632ab2..f3c9e60 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,7 @@ 'use strict'; var browserSync = require('browser-sync'); var chokidar = require('chokidar'); +var historyApiFallback = require('connect-history-api-fallback'); var spawn = require('child_process').spawn; var platform = require('elm/platform'); var path = require('path'); @@ -27,6 +28,8 @@ module.exports = function elmServer(inputFilesArg, optsArg) { opts.output; var watch = opts.watch || path.dirname(outputFile); + var isSpa = opts.spa || false; + var startPath = opts.startPath || (path.extname(outputFile) === '.html' ? @@ -47,7 +50,10 @@ module.exports = function elmServer(inputFilesArg, optsArg) { .on('change', elmMake); return browserSync({ - server: watch, + server: { + baseDir: watch, + middleware: isSpa ? [ historyApiFallback() ] : [] + }, watchOptions: { ignored: /(elm-stuff|\.elm)/ }, diff --git a/package.json b/package.json index 6f243e0..4d1adbb 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ "dependencies": { "browser-sync": "^2.10.1", "chokidar": "^1.4.1", + "connect-history-api-fallback": "^1.1.0", "commander": "2.8.0", "elm": "^0.16.0", "lodash.chunk": "^3.0.1"