Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 78883ff

Browse files
piq9117Gitea
piq9117
authored and
Gitea
committed
hot module relaoding (#15)
- added index.js which contains the hot module reloading function
1 parent 00a6602 commit 78883ff

File tree

9 files changed

+65
-9
lines changed

9 files changed

+65
-9
lines changed

‎Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
build:
22
cabal new-build
3+
4+
clean:
5+
rm -rf example dist-newstyle

‎src/UmuReactBasic/Capability/ManageCommand.hs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ baseGeneration mLoc = do
3636
writeSrcDir mLoc
3737
writeAssetsDir mLoc
3838
writeIndexHtml mLoc
39+
writeHotRelodingIndexJs mLoc
3940
writeSrcMainFile mLoc
4041
writeComponentDir mLoc
4142
writeTitleComponentFile mLoc
@@ -46,6 +47,10 @@ baseGeneration mLoc = do
4647
writeMakefile mLoc
4748
writePackageJsonFile mLoc
4849

50+
------------------------------------------
51+
--- DIRECTORY GENERATION
52+
------------------------------------------
53+
4954
writeInitialDir :: ( MonadIO m, LogMessage m ) => Text -> m ()
5055
writeInitialDir loc = do
5156
res <- liftIO
@@ -113,6 +118,9 @@ writeComponentDir mLoc = do
113118
( const $ logInfo "Generating Component..." )
114119
res
115120

121+
------------------------------------------
122+
--- FILE GENERATION
123+
------------------------------------------
116124
writeTitleComponentFile :: ( MonadIO m, LogMessage m ) => Maybe Text -> m ()
117125
writeTitleComponentFile mLoc = do
118126
isExists <- TP.testfile $ Turtle.fromText $ mkPathName mLoc "src/Component/Title.purs"
@@ -178,3 +186,12 @@ writePackageJsonFile mLoc = do
178186
else do
179187
liftIO $ TP.writeTextFile ( Turtle.fromText $ mkPathName mLoc "package.json" ) packageJsonFile
180188
logInfo "Generating package.json..."
189+
190+
writeHotRelodingIndexJs :: ( MonadIO m , LogMessage m ) => Maybe Text -> m ()
191+
writeHotRelodingIndexJs mLoc = do
192+
isExists <- TP.testfile $ Turtle.fromText $ mkPathName mLoc "assets/index.js"
193+
if isExists
194+
then logError "assets/index.js already exists!"
195+
else do
196+
liftIO $ TP.writeTextFile ( Turtle.fromText $ mkPathName mLoc "assets/index.js" ) hotReloadIndexJS
197+
logInfo "Generating assets/index.js..."

‎src/UmuReactBasic/Templates.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module UmuReactBasic.Templates
88
, testMainFile
99
, makeFile
1010
, packageJsonFile
11+
, hotReloadIndexJS
1112
) where
1213

1314
import Import
@@ -36,3 +37,6 @@ makeFile = $(embedFileUtf8 "templates/Makefile")
3637

3738
packageJsonFile :: Text
3839
packageJsonFile = $(embedFileUtf8 "templates/package.json")
40+
41+
hotReloadIndexJS :: Text
42+
hotReloadIndexJS = $(embedFileUtf8 "templates/index.js")

‎templates/Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ build-all:
44
build:
55
spago build
66

7+
build-watch:
8+
spago build --watch
9+
710
bundle:
811
spago bundle-app --main Main --to dist/app.js && ./node_modules/.bin/parcel build assets/index.html
912

@@ -14,4 +17,4 @@ clean:
1417
rm -rf .cache .spago node_modules .psci_modules output dist
1518

1619
start:
17-
./node_modules/.bin/http-server dist
20+
./node_modules/.bin/parcel assets/index.html

‎templates/SrcMain.purs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import Web.HTML ( window )
1313
import Effect ( Effect )
1414
import Effect.Exception ( throw )
1515
-- React
16-
import React.Basic.Hooks ( element, ReactComponent, component )
16+
import React.Basic.Hooks ( element, ReactComponent, component, JSX )
1717
import React.Basic.DOM as R
1818

1919
mkMainComponent :: Effect ( ReactComponent {} )
@@ -25,11 +25,17 @@ mkMainComponent = do
2525
[ element title { text: "Hello, World" }
2626
]
2727

28+
-- This is separated so it can be called in the hot reload function.
29+
mainJSX :: Effect JSX
30+
mainJSX = do
31+
mainComp <- mkMainComponent
32+
pure $ element mainComp {}
33+
2834
main :: Effect Unit
2935
main = do
3036
mApp <- getElementById "app" =<< ( map toNonElementParentNode $ document =<< window )
3137
case mApp of
3238
Nothing -> throw "App element not found."
3339
Just app -> do
34-
mainComponent <- mkMainComponent
35-
R.render ( element mainComponent {} ) app
40+
mainComponent <- mainJSX
41+
R.render mainComponent app

‎templates/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
</head>
77
<body>
88
<div id="app"></div>
9-
<script src="../dist/app.js"></script>
9+
<script src="./index.js"></script>
1010
</body>
1111
</html>

‎templates/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import React from "react";
2+
import ReactDOM from "react-dom";
3+
4+
import Main from "../output/Main";
5+
6+
function main () {
7+
const component = React.createElement(Main.mainJSX, { label: "MainComponent" });
8+
9+
ReactDOM.render(component, document.getElementById("app"));
10+
}
11+
12+
if ( module.hot ) {
13+
module.hot.accept(function () {
14+
console.log("[INFO]: Running main...");
15+
main();
16+
})
17+
}
18+
19+
console.log("[INFO]: Starting...");
20+
main();

‎templates/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
"devDependencies": {
1010
"parcel-bundler": "^1.12.4",
1111
"react": "^16.12.0",
12-
"react-dom": "^16.12.0",
13-
"http-server": "^0.12.1"
12+
"react-dom": "^16.12.0"
1413
}
1514
}

‎umu-react-basic.cabal

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ cabal-version: >=1.10
44
-- http://haskell.org/cabal/users-guide/
55

66
name: umu-react-basic
7-
version: 0.1.1.2
7+
version: 0.1.1.3
88
-- synopsis:
99
-- description:
1010
-- bug-reports:
@@ -48,7 +48,7 @@ library
4848
, OverloadedStrings
4949
ghc-options:
5050
-Wall
51-
51+
-j6
5252

5353
executable umu-react-basic
5454
main-is: Main.hs
@@ -62,3 +62,7 @@ executable umu-react-basic
6262
default-extensions: NoImplicitPrelude
6363
ghc-options:
6464
-Wall
65+
-threaded
66+
-rtsopts
67+
-with-rtsopts=-N
68+
-j6

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /