forked from massive-oss/haxe-react
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[samples] add static site generator example
- Loading branch information
Showing
10 changed files
with
176 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#!/usr/bin/env haxe | ||
|
||
common.hxml | ||
|
||
-main StaticGenerator | ||
-js bin/static-gen.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,6 @@ | ||
#!/usr/bin/env haxe | ||
|
||
-L react-next | ||
-L react-css | ||
-L css-types | ||
-L classnames | ||
-L datetime | ||
-L markdown | ||
-L event-types | ||
-L html-entities | ||
-L tink_domspec | ||
-L hxnodejs | ||
-L yaml | ||
common.hxml | ||
|
||
-cp src | ||
-main Main | ||
-js bin/server.js | ||
|
||
# -dce full | ||
-D js-es=6 | ||
# -D analyzer-optimize | ||
-D message.reporting=pretty | ||
|
||
-w -WDeprecatedEnumAbstract | ||
|
||
# Configure react-css | ||
-D react.css.out=bin/styles.css | ||
-D react.css.base=res/base.css | ||
# -D react.css.sourcemap=styles.css.map | ||
|
||
# Configure react-next | ||
-D react-wrap-strict | ||
-D react-check-jsxstatic-type | ||
-D react-disable-dynamic-components | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
-L react-next | ||
-L react-css | ||
-L css-types | ||
-L classnames | ||
-L datetime | ||
-L markdown | ||
-L event-types | ||
-L html-entities | ||
-L tink_domspec | ||
-L hxnodejs | ||
-L yaml | ||
|
||
-cp src | ||
|
||
# -dce full | ||
-D js-es=6 | ||
# -D analyzer-optimize | ||
-D message.reporting=pretty | ||
|
||
-w -WDeprecatedEnumAbstract | ||
|
||
# Configure react-css | ||
-D react.css.out=bin/styles.css | ||
-D react.css.base=res/base.css | ||
# -D react.css.sourcemap=styles.css.map | ||
|
||
# Configure react-next | ||
-D react-wrap-strict | ||
-D react-check-jsxstatic-type | ||
-D react-disable-dynamic-components |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import react.React; | ||
import react.ReactComponent; | ||
import react.ReactContext; | ||
import react.ReactMacro.jsx; | ||
import react.ReactType; | ||
|
||
typedef AppContextData = { | ||
var staticSite:Bool; | ||
} | ||
|
||
typedef AppContextProviderProps = { | ||
var value:AppContextData; | ||
} | ||
|
||
typedef AppContextConsumerProps = { | ||
var children:AppContextData->ReactFragment; | ||
} | ||
|
||
class AppContext { | ||
public static var Context(get, null):ReactContext<AppContextData>; | ||
public static var Provider(get, null):ReactTypeOf<AppContextProviderProps>; | ||
public static var Consumer(get, null):ReactTypeOf<AppContextConsumerProps>; | ||
|
||
static function get_Context() {ensureReady(); return Context;} | ||
static function get_Provider() {ensureReady(); return Provider;} | ||
static function get_Consumer() {ensureReady(); return Consumer;} | ||
|
||
static function ensureReady() @:bypassAccessor { | ||
if (Context == null) { | ||
Context = React.createContext(); | ||
Context.displayName = "AppContext"; | ||
Consumer = Context.Consumer; | ||
Provider = Context.Provider; | ||
} | ||
} | ||
|
||
public static function wrap(Comp:ReactType):ReactType { | ||
return function (props:{}) { | ||
return jsx( | ||
<Consumer> | ||
{value -> | ||
<Comp {...props} staticSite={value.staticSite} /> | ||
} | ||
</Consumer>); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import haxe.io.Path; | ||
import js.lib.Promise; | ||
import js.node.Fs.Fs; | ||
import sys.FileSystem; | ||
import sys.io.File; | ||
|
||
import data.DocChapter; | ||
import react.ReactDOMServer; | ||
import react.ReactMacro.jsx; | ||
|
||
import comp.App; | ||
|
||
class StaticGenerator { | ||
static inline var OUT = "bin/static"; | ||
|
||
static function main() { | ||
FileSystem.createDirectory(OUT); | ||
for (f in FileSystem.readDirectory(OUT)) FileSystem.deleteFile(Path.join([OUT, f])); | ||
|
||
loadChapters().then(chapters -> { | ||
chapters.unshift(loadReadme()); | ||
Promise.all(chapters.map(renderChapter)).then(_ -> { | ||
File.copy('bin/styles.css', '$OUT/styles.css'); | ||
Sys.println('Generated html and css files in $OUT'); | ||
}); | ||
}); | ||
} | ||
|
||
static function renderChapter(chapter:DocChapter):Promise<Void> { | ||
return new Promise((resolve, reject) -> { | ||
var slug = chapter.slug == "/" ? null : chapter.slug; | ||
var path = '$OUT${slug ?? "/index"}.html'; | ||
var fileWriter = Fs.createWriteStream(path); | ||
var stream = ReactDOMServer.renderToStaticNodeStream(jsx(<App chapter={slug} staticSite />)); | ||
stream.on('end', resolve); | ||
stream.on('error', resolve); | ||
stream.pipe(fileWriter); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters