diff --git a/Dockerfile b/Dockerfile index 4f227bc..f5f85f4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -26,6 +26,9 @@ RUN rm -rf .static RUN rm -rf .cache RUN rm -rf .git +# Build templates (when that works) +# RUN node build.js + ## 2. Regroup FROM node:10-alpine RUN apk add --no-cache ca-certificates diff --git a/build.js b/build.js new file mode 100644 index 0000000..fcc7314 --- /dev/null +++ b/build.js @@ -0,0 +1,53 @@ +const build = require("./builder") + +// Setup global environment +require("marko/node-require").install() +require("es6-promise").polyfill() + +// Configure lasso +require("lasso").configure({ + plugins: [ + "lasso-marko", + "lasso-less", + ], + outputDir: "./.static", + bundlingEnabled: true, + minify: true, + fingerprintsEnabled: true, + + require: { + builtins: { + fs: require.resolve("empty-module"), + }, + + transforms: [ + { + transform: "lasso-babel-transform", + config: { + extensions: [".js", ".es6"], // Enabled file extensions. Default: [".js", ".es6"] + babelOptions: { + presets: [ "es2015" ] + } + } + } + ] + } +}) + +// Render templates +async function buildAll() { + await build("./marko/page/story/list.marko") + await build("./marko/page/story/tag-list.marko", {tags: []}) + await build("./marko/page/logs/list.marko") + await build("./marko/page/data/channels.marko", {channels: [], user: {}}) + await build("./marko/page/data/characters.marko", {characters: [], user: {}}) + await build("./marko/page/story-content/view.marko", {story: {chapters: []}}) + await build("./marko/page/logs-content/view.marko", {log: {posts: [], channel: {}}}) +} + +buildAll().then(() => { + console.log("Done!") +}).catch(err => { + console.error(err) + process.exit(1) +}) \ No newline at end of file diff --git a/builder.js b/builder.js new file mode 100644 index 0000000..322056b --- /dev/null +++ b/builder.js @@ -0,0 +1,15 @@ +module.exports = function build(path, data) { + console.log("Building", path) + + return new Promise((resolve, reject) => { + const render = require(path).render(data) + render.once("error", err => reject) + + const interval = setInterval(() => { + if (render._remaining === 0) { + clearInterval(interval) + resolve() + } + }, 500) + }) +} diff --git a/package.json b/package.json index dcb6b5d..64cc810 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@gisle/rpdata-frontend", - "version": "1.0.0", + "version": "1.0.1", "description": "RPData (www.aiterp.net) frontend/proxy", "main": "./index.js", "scripts": { diff --git a/server.js b/server.js index a3c2646..7b94f9a 100644 --- a/server.js +++ b/server.js @@ -1,16 +1,19 @@ // Setup global environment require("marko/node-require").install() -require("marko/express") require("es6-promise").polyfill() // Load server config const config = require("./config") +// Load utilities +const build = require("./builder") + // Express depedencies const express = require("express") const proxy = require("express-http-proxy") const lasso = require("lasso") const lassoMiddleware = require("lasso/middleware") +const markoExpress = require("marko/express") const bodyParser = require("body-parser") // Load middleware @@ -30,7 +33,7 @@ lasso.configure({ outputDir: "./.static", // Place all generated JS/CSS/etc. files into the "static" dir bundlingEnabled: isProduction, // Only enable bundling in production minify: isProduction, // Only minify JS and CSS code in production - fingerprintsEnabled: isProduction, // Only add fingerprints to URLs in production + fingerprintsEnabled: true, // Only add fingerprints to URLs in production require: { builtins: { @@ -52,6 +55,7 @@ lasso.configure({ }) // Apply middleware +app.use(markoExpress()) app.use(session) app.use(passport.initialize()) app.use(passport.session()) @@ -90,24 +94,33 @@ app.get("/", function(req, res) { res.redirect("/story/") }) -// Render common templates -require("./marko/page/story/list.marko").render() -require("./marko/page/story/tag-list.marko").render({tags: []}) -require("./marko/page/logs/list.marko").render() -require("./marko/page/data/channels.marko").render({channels: [], user: {}}) -require("./marko/page/data/characters.marko").render({characters: [], user: {}}) -require("./marko/page/story-content/view.marko").render({story: {chapters: []}}) -require("./marko/page/logs-content/view.marko").render({log: {posts: [], channel: {}}}) - -// Start server -app.listen(config.port, function() { - console.log("Server started: http://localhost:" + config.port + "/") - - if (process.send) { - setTimeout(() => process.send("online"), 500) +async function main() { + // Build templates + if (isProduction) { + await build("./marko/page/story/list.marko") + await build("./marko/page/story/tag-list.marko", {tags: []}) + await build("./marko/page/logs/list.marko") + await build("./marko/page/data/channels.marko", {channels: [], user: {}}) + await build("./marko/page/data/characters.marko", {characters: [], user: {}}) + await build("./marko/page/story-content/view.marko", {story: {chapters: []}}) + await build("./marko/page/logs-content/view.marko", {log: {posts: [], channel: {}}}) } -}) + + // Start server + app.listen(config.port, function() { + console.log("Server started: http://localhost:" + config.port + "/") + + if (process.send) { + setTimeout(() => process.send("online"), 100) + } + }) +} // Handle shutdown signals (Docker needs this to shutdown quickly) process.on('SIGINT', () => process.exit(0)) -process.on('SIGTERM', () => process.exit(0)) \ No newline at end of file +process.on('SIGTERM', () => process.exit(0)) + +main().catch(err => { + console.error(err) + process.exit(1) +}) \ No newline at end of file