File tree Expand file tree Collapse file tree 6 files changed +59
-35
lines changed Expand file tree Collapse file tree 6 files changed +59
-35
lines changed Original file line number Diff line number Diff line change 3
3
This is a Elm ssr experiment, it uses Razzle to server the http responses.
4
4
` razzle.config.js ` is the equivalent to ` webpack.config.js `
5
5
6
- ### This repository still wip, I need to implement HMR, the "elm-hot-loader" library don't work with razzle
6
+ This repo constains an example of how to make Server side rendering with Elm and Razzle, also has hot code reloading!
7
7
8
8
## To run:
9
9
Original file line number Diff line number Diff line change 7
7
"start" : " razzle start" ,
8
8
"build" : " razzle build" ,
9
9
"test" : " razzle test --env=jsdom" ,
10
- "start:prod" : " NODE_ENV=production node build/server.js"
10
+ "start:prod" : " set NODE_ENV=production&& node build/server.js"
11
11
},
12
12
"dependencies" : {
13
13
"elm-hot-loader" : " ^0.5.4" ,
Original file line number Diff line number Diff line change @@ -6,22 +6,49 @@ module.exports = {
6
6
7
7
appConfig . module . rules [ 2 ] . exclude . push ( / \. ( e l m ) $ / )
8
8
9
+ appConfig . module . noParse = [ / .e l m $ / ]
9
10
appConfig . resolve . extensions = config . resolve . extensions . concat ( [
10
11
'.elm'
11
12
] ) ;
12
- // appConfig.module.noParse = /.elm$/
13
- // appConfig.entry = './src/client.js'
14
- appConfig . module . rules . push (
13
+
14
+ if ( dev ) {
15
+ appConfig . module . rules . push (
15
16
{
16
17
test : / \. e l m $ / ,
17
18
exclude : [ / e l m - s t u f f / , / n o d e _ m o d u l e s / ] ,
18
- loader : require . resolve ( 'elm-webpack-loader' ) ,
19
- options : {
20
- verbose : true ,
21
- warn : true
22
- }
19
+ use : [
20
+ {
21
+ loader : 'elm-hot-loader'
22
+ } ,
23
+ {
24
+ loader : 'elm-webpack-loader' ,
25
+ options : {
26
+ verbose : true ,
27
+ warn : true ,
28
+ // debug: true,
29
+ pathToMake : require ( 'elm/platform' ) . executablePaths [ 'elm-make' ] ,
30
+ forceWatch : true
31
+ }
32
+ }
33
+ ] ,
23
34
} )
24
-
35
+ }
36
+ else { // Production
37
+ appConfig . module . rules . push (
38
+ {
39
+ test : / \. e l m $ / ,
40
+ exclude : [ / e l m - s t u f f / , / n o d e _ m o d u l e s / ] ,
41
+ use : [
42
+ {
43
+ loader : 'elm-webpack-loader' ,
44
+ options : {
45
+ pathToMake : require ( 'elm/platform' ) . executablePaths [ 'elm-make' ] ,
46
+ }
47
+ }
48
+ ] ,
49
+ } )
50
+ }
51
+
25
52
return appConfig ;
26
53
} ,
27
54
} ;
Original file line number Diff line number Diff line change @@ -2,7 +2,7 @@ module Main exposing (main)
2
2
3
3
import Html
4
4
import App exposing (init , update , view )
5
-
5
+
6
6
main : Program Never App .Model App .Msg
7
7
main =
8
8
Html . program
Original file line number Diff line number Diff line change 1
1
var Elm = require ( './Main' ) ;
2
2
3
- // I think is better call this at the end of the body tag, the bundle up and the script down
3
+ // We need embed the Elm app to the div, if we call fullscreen we will have duplicated html
4
4
Elm . Main . embed ( document . getElementById ( "root" ) ) ;
5
5
6
6
if ( module . hot ) {
7
7
module . hot . accept ( ) ;
8
- module . hot . accept ( './Main' , ( ) => {
9
- const nextElm = require ( './Main' ) . Main ;
10
- nextElm . embed ( document . getElementById ( "root" ) ) ;
11
- } )
12
8
}
Original file line number Diff line number Diff line change 1
1
import elmStaticHtml from "elm-static-html-lib" ;
2
2
import express from 'express' ;
3
+ require ( './Main' ) ;
3
4
4
5
const assets = require ( process . env . RAZZLE_ASSETS_MANIFEST ) ;
5
6
@@ -16,24 +17,24 @@ server
16
17
const markup = generatedHtml ;
17
18
res . send (
18
19
`<!doctype html>
19
- <html lang="">
20
- <head>
21
- <meta httpEquiv="X-UA-Compatible" content="IE=edge" />
22
- <meta charSet='utf-8' />
23
- <title>Welcome to Razzle</title>
24
- <meta name="viewport" content="width=device-width, initial-scale=1">
25
- ${ assets . client . css
26
- ? `<link rel="stylesheet" href="${ assets . client . css } ">`
27
- : '' }
28
-
29
- </head>
30
- <body>
31
- <div id="root">${ markup } </div>
32
- </body>
33
- ${ process . env . NODE_ENV === 'production'
34
- ? `<script src="${ assets . client . js } "></script>`
35
- : `<script src="${ assets . client . js } "></script>` }
36
- </html>`
20
+ <html lang="">
21
+ <head>
22
+ <meta httpEquiv="X-UA-Compatible" content="IE=edge" />
23
+ <meta charSet='utf-8' />
24
+ <title>Welcome to Razzle</title>
25
+ <meta name="viewport" content="width=device-width, initial-scale=1">
26
+ ${ assets . client . css
27
+ ? `<link rel="stylesheet" href="${ assets . client . css } ">`
28
+ : '' }
29
+
30
+ </head>
31
+ <body>
32
+ <div id="root">${ markup } </div>
33
+ </body>
34
+ ${ process . env . NODE_ENV === 'production'
35
+ ? `<script src="${ assets . client . js } "></script>`
36
+ : `<script src="${ assets . client . js } "></script>` }
37
+ </html>`
37
38
) ;
38
39
} ) . catch ( ( error ) => {
39
40
console . log ( error ) ;
You can’t perform that action at this time.
0 commit comments