Init commit
This commit is contained in:
commit
6d7d598290
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
public/
|
||||||
|
package-lock.json
|
||||||
|
node_modules/
|
||||||
30
README.md
Normal file
30
README.md
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
# Test static site generation
|
||||||
|
|
||||||
|
## Introduction
|
||||||
|
This small snippet aims to convince that you can render all static pages at once using SSG-scheme.
|
||||||
|
|
||||||
|
## OK, what should I see?
|
||||||
|
Scheme is as follows:
|
||||||
|
- Aim is to deliver plain ol' HTML pages by web server. No, no application server accepted; This is best for most of services!
|
||||||
|
- Problem: You have redundant content, and you don't want the hassle to maintain HTML files:
|
||||||
|
- No functions, so redundant code;
|
||||||
|
- Can't properly handle modular breakdown (header, footer, sections);
|
||||||
|
- When you made it work once, shall you re-understand everything 6 months later to thange stubs?
|
||||||
|
|
||||||
|
Here is how it works:
|
||||||
|
- We store relevant data in a YAML file, say `settings.yml`.
|
||||||
|
- We Use [Nunjucks](https://mozilla.github.io/nunjucks/templating.html), the millenials way to code, to generate Jinja2-styled webpages on steroids.
|
||||||
|
- We Then compile the whole _once_, and after we got static HTML for basically every use.
|
||||||
|
|
||||||
|
What repo doesn't contain:
|
||||||
|
- All Nunjucks killer features.
|
||||||
|
|
||||||
|
## Dependencies
|
||||||
|
Install NodeJS and npm
|
||||||
|
|
||||||
|
## Build
|
||||||
|
```bash
|
||||||
|
npm run build
|
||||||
|
```
|
||||||
|
|
||||||
|
Serve your webserver to display the `public` folder as webroot.
|
||||||
23
build.js
Normal file
23
build.js
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
const nunjucks = require('nunjucks');
|
||||||
|
const fs = require('fs');
|
||||||
|
const yaml = require('js-yaml');
|
||||||
|
|
||||||
|
const ENTRY_POINT='templates/index.njk';
|
||||||
|
const OUTPUT_POINT='public/index.html';
|
||||||
|
const GLOBAL_SETTINGS='./settings.yml';
|
||||||
|
|
||||||
|
try {
|
||||||
|
let fileContents = fs.readFileSync(GLOBAL_SETTINGS, 'utf8');
|
||||||
|
let settings = yaml.safeLoad(fileContents);
|
||||||
|
|
||||||
|
console.log(data);
|
||||||
|
} catch (e) {
|
||||||
|
console.log(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
var data = nunjucks.render(ENTRY_POINT, settings);
|
||||||
|
|
||||||
|
fs.writeFile(OUTPUT_POINT, data, 'utf-8', function(err){
|
||||||
|
if (err) return console.log(err);
|
||||||
|
});
|
||||||
|
|
||||||
19
package.json
Normal file
19
package.json
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
{
|
||||||
|
"name": "test",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "",
|
||||||
|
"main": "index.js",
|
||||||
|
"scripts": {
|
||||||
|
"prebuild": "rm -rf public/ && mkdir public/",
|
||||||
|
"install": "npm run build",
|
||||||
|
"build": "node build.js",
|
||||||
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
|
},
|
||||||
|
"author": "",
|
||||||
|
"license": "ISC",
|
||||||
|
"dependencies": {
|
||||||
|
"fs": "0.0.1-security",
|
||||||
|
"js-yaml": "^3.14.0",
|
||||||
|
"nunjucks": "^3.2.2"
|
||||||
|
}
|
||||||
|
}
|
||||||
3
settings.json
Normal file
3
settings.json
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"dinner": "ready"
|
||||||
|
}
|
||||||
8
templates/index.njk
Normal file
8
templates/index.njk
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
<p>
|
||||||
|
Dinner is {{ dinner }}
|
||||||
|
</p>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
Loading…
x
Reference in New Issue
Block a user