Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
astronomersiva committed Apr 14, 2019
1 parent ab3a7b8 commit 6b5ecf2
Show file tree
Hide file tree
Showing 20 changed files with 600 additions and 65 deletions.
6 changes: 5 additions & 1 deletion .eslintrc.js
@@ -1,5 +1,9 @@
module.exports = {
extends: "standard",
extends: 'standard',
env: {
node: true,
mocha: true
},
rules: {
'space-before-function-paren': 'off',
'semi': 'off'
Expand Down
4 changes: 4 additions & 0 deletions .gitignore
Expand Up @@ -84,3 +84,7 @@ benchmarks/fixtures/lego/build
benchmarks/fixtures/lego/.lego
benchmarks/fixtures/jekyll/_site
benchmarks/fixtures/jekyll/.sass-cache

# tests
test/fixtures/dummy/.lego
test/fixtures/dummy/build
3 changes: 3 additions & 0 deletions .npmignore
@@ -0,0 +1,3 @@
tests/
benchmarks/
docs/
62 changes: 34 additions & 28 deletions README.md
Expand Up @@ -18,7 +18,8 @@ This was a great learning experience to be honest.
* Supports Liquid templates.
* Supports minification and uglification of JS and CSS files(using the provided `browserslist` to determine transpilation targets).
* Does asset revisioning of CSS, JS and image files.
* JPG and PNG images under `static` will be optimised with `imageoptim`.
* Supports PostCSS plugins.
* Images under `static` will be optimised with `imageoptim`.
* Code highlighting at build time using [highlight.js](https://highlightjs.org/).
* Automatic sitemap and RSS feeds generation.
* Supports extracting and inlining critical CSS with [critical](https://github.com/addyosmani/critical).
Expand All @@ -28,6 +29,29 @@ This was a great learning experience to be honest.
* Supports including html in md by implementing a custom md syntax. `::: include table.html :::`.
* Live-reload during development.
* Copies CNAME to `build` directory, so will work with GH Pages.
* Implements a cache resulting in faster builds on subsequent runs.

### Installation

* Run `npm i -g @astronomersiva/lego`.

### Usage

* Run `lego g siteName` to create a new site.
* Run `lego s` / `lego serve` to run a server.
* Run `lego b` / `lego build` to create an optimised build.
* To include an HTML in a markdown file, use ::: include table.html :::.
* To automatically generate images for various resolutions,
```
::: lego-image src="static/images/${IMAGE}" res="1080,500,320" alt="alternate text" class="img-responsive center-block" :::
```
* lego also exposes an `isDevelopment` variable that you can use to disable certain stuff in development. For example, analytics.

```
{% unless isDevelopment %}
<!-- analytics code -->
{% endunless %}
```

### Directory structure

Expand All @@ -37,7 +61,7 @@ This was a great learning experience to be honest.
├── README.md
├── layouts
│   ├── post.html // will be used for markdown posts
│   └── tags.html // will be used to generate tag wise listing of posts
│   └── tags.html // will be used to generate tag wise listing of posts
├── pages
│   ├── 404.html
│   └── about.html // each of these will be put under a separate folder in build
Expand Down Expand Up @@ -145,28 +169,6 @@ that are already included in lego.
* `rss`: Options to pass to the RSS feeds generator. Refer [rss feedOptions](https://www.npmjs.com/package/rss#feedoptions).
Categories and Publishing Date will be automatically populated.

### Installation

* Install `npm` and `Node`.
* Run `npm i -g @astronomersiva/lego`.

### Usage

* Run `lego g` to create a new site.
* Run `lego s` to run a server.
* Run `lego` to create an optimised build.
* To include an HTML in a markdown file, use ::: include table.html :::.
* To automatically generate images for various resolutions,
```
::: lego-image src="static/images/${IMAGE}" res="1080,500,320" alt="alternate text" class="img-responsive center-block" :::
```
* lego also exposes an `isDevelopment` variable that you can use to disable certain stuff in development. For example, analytics.

```
{% unless isDevelopment %}
<!-- analytics code -->
{% endunless %}
```

### Benchmarks

Expand All @@ -175,7 +177,7 @@ To run benchmarks, run
$ cd benchmarks
$ yarn
$ node generator.js
$ node index.js
$ node --max-old-space-size=4096 index.js
```

It will run benchmarks against `jekyll` the following data:
Expand All @@ -185,13 +187,17 @@ It will run benchmarks against `jekyll` the following data:
* Each paragraph contains 150 random words.
* The size of each post is about 150kb.
* lego will be run with its cache disabled.
* No static files are present.

**While jekyll produces only a build, lego does HTML minification as well.**

Results:

```bash
jekyll x 0.06 ops/sec ±0.79% (5 runs sampled)
lego x 0.10 ops/sec ±0.86% (5 runs sampled)
Fastest is lego
jekyll x 0.04 ops/sec ±6.71% (5 runs sampled)
lego without cache x 0.06 ops/sec ±3.98% (5 runs sampled)
lego with cache x 0.06 ops/sec ±12.94% (5 runs sampled)
Fastest is lego without cache
```

### License
Expand Down
1 change: 0 additions & 1 deletion benchmarks/generator.js
Expand Up @@ -8,7 +8,6 @@ const writeFile = promisify(fs.writeFile);
async function generator(outputBasePath, layout) {
let promises = [];
for (let i = 0; i < 500; i++) {
promises.push()
let contents = `---\ntitle: Post ${i}\n${layout ? `layout: ${layout}\n` : ''}---\n`;

let paras = 150;
Expand Down
6 changes: 4 additions & 2 deletions benchmarks/index.js
Expand Up @@ -5,9 +5,11 @@ const suite = new Benchmark.Suite; // eslint-ignore-line new-parens

// add tests
suite.add('jekyll', function() {
execSync(`cd fixtures/jekyll && rm -rf _site && jekyll build`)
}).add('lego', function() {
execSync(`cd fixtures/jekyll && rm -rf _site && bundle exec jekyll build`)
}).add('lego without cache', function() {
execSync(`cd fixtures/lego && rm -rf build && SKIP_CACHE=t lego`)
}).add('lego with cache', function() {
execSync(`cd fixtures/lego && rm -rf build && lego`)
}).on('cycle', function(event) {
console.log(String(event.target));
}).on('complete', function() {
Expand Down
4 changes: 2 additions & 2 deletions lib/lego.js
Expand Up @@ -17,7 +17,7 @@ module.exports = async function(args) {
process.exit();
}

const [task = ''] = args;
const [task = 'b'] = args;

if (['g', 'generate'].includes(task)) {
const siteName = args[1];
Expand Down Expand Up @@ -64,9 +64,9 @@ module.exports = async function(args) {
...build,
'revisionAssets',
'optimiseImages',
'inlineAssets',
[
'extractCritical',
'inlineAssets',
'generateSiteMap',
'generateRSSFeeds'
]
Expand Down
23 changes: 21 additions & 2 deletions lib/tasks/generateSite.js
Expand Up @@ -121,8 +121,27 @@ module.exports = async function(site) {
`;
fs.writeFileSync(`${sitePath}/${STATIC}/css/styles.css`, dummyCss);

let configFile = `module.exports = {\n name: '${siteName}',\n url: ''\n}\n`;
fs.writeFileSync(`${sitePath}/lego.js`, configFile);
let configJson = {
name: siteName,
url: '',
inlineSource: true,
critical: {
inline: true,
dimensions: [
{
height: 800,
width: 470
}, {
height: 900,
width: 1200
}
],
penthouse: {
timeout: 150000
}
}
};
fs.writeFileSync(`${sitePath}/lego.js`, `module.exports = ${JSON.stringify(configJson, null, 2)}`);

let packageJson = {
name: siteName,
Expand Down
4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -4,7 +4,7 @@
"description": "A custom built static site generator that will one day power [sivasubramanyam.me](https://sivasubramanyam.me) 🏋️‍",
"main": "index.js",
"scripts": {
"test": "eslint lib"
"test": "eslint lib && mocha"
},
"bin": {
"lego": "bin/lego"
Expand Down Expand Up @@ -36,6 +36,8 @@
"eslint-plugin-node": "^7.0.1",
"eslint-plugin-promise": "^3.8.0",
"eslint-plugin-standard": "^3.1.0",
"fixturify": "^1.2.0",
"mocha": "^6.1.3",
"time-require": "^0.1.2"
},
"dependencies": {
Expand Down
Empty file added test/fixtures/dummy/CNAME
Empty file.
1 change: 1 addition & 0 deletions test/fixtures/dummy/expected-output.json
@@ -0,0 +1 @@
{"404.html":"","CNAME":"","index.html":"<!DOCTYPE html><html><head><title>Your awesome site</title><style>\nhtml{box-sizing:border-box;font-size:.625}*{margin:0;padding:0}*,:after,:before{box-sizing:inherit}body,h1,p{margin:0;padding:0;font-weight:400}h1{font-weight:700;font-size:5rem;margin:2% 0}p{margin-bottom:20px}code{font-family:monospace;background-color:#ffc336;color:#000;font-size:1.1rem}a,a:link,a:visited{color:#fff;text-decoration:none;border-bottom:2px solid #fff}body{height:100vh;display:flex;align-items:center;justify-content:center;flex-direction:column;font-family:Segoe UI,Tahoma,Geneva,Verdana,sans-serif;color:#fff;font-size:1rem;padding:0 30px;background:linear-gradient(25deg,#6356dd,#ac499f,#d23b62,#ec2a22)}\n</style>\n<link href=\"/static/css/styles-709642c2d4.css\" rel=\"preload\" as=\"style\" onload=\"this.onload=null;this.rel='stylesheet'\">\n<noscript><link href=\"/static/css/styles-709642c2d4.css\" rel=\"stylesheet\"></noscript>\n<script>!function(n){\"use strict\";n.loadCSS||(n.loadCSS=function(){});var o=loadCSS.relpreload={};if(o.support=function(){var e;try{e=n.document.createElement(\"link\").relList.supports(\"preload\")}catch(t){e=!1}return function(){return e}}(),o.bindMediaToggle=function(t){var e=t.media||\"all\";function a(){t.addEventListener?t.removeEventListener(\"load\",a):t.attachEvent&&t.detachEvent(\"onload\",a),t.setAttribute(\"onload\",null),t.media=e}t.addEventListener?t.addEventListener(\"load\",a):t.attachEvent&&t.attachEvent(\"onload\",a),setTimeout(function(){t.rel=\"stylesheet\",t.media=\"only x\"}),setTimeout(a,3e3)},o.poly=function(){if(!o.support())for(var t=n.document.getElementsByTagName(\"link\"),e=0;e<t.length;e++){var a=t[e];\"preload\"!==a.rel||\"style\"!==a.getAttribute(\"as\")||a.getAttribute(\"data-loadcss\")||(a.setAttribute(\"data-loadcss\",!0),o.bindMediaToggle(a))}},!o.support()){o.poly();var t=n.setInterval(o.poly,500);n.addEventListener?n.addEventListener(\"load\",function(){o.poly(),n.clearInterval(t)}):n.attachEvent&&n.attachEvent(\"onload\",function(){o.poly(),n.clearInterval(t)})}\"undefined\"!=typeof exports?exports.loadCSS=loadCSS:n.loadCSS=loadCSS}(\"undefined\"!=typeof global?global:this);</script></head><body><h1>Your awesome site!</h1><div><p>To generate a production build of this site, run <code>lego build</code>.</p><p>Facing trouble? File an issue on <a href=\"https://github.com/astronomersiva/lego/\">GitHub!</a></p></div></body></html>","sitemap.xml":"","static":{"assetMap.json":"{\n \"/static/css/styles.css\": \"/static/css/styles-709642c2d4.css\"\n}","css":{"styles-709642c2d4.css":"html{box-sizing:border-box;font-size:.625}*{margin:0;padding:0}*,:after,:before{box-sizing:inherit}body,h1,h2,h3,h4,h5,h6,ol,p,ul{margin:0;padding:0;font-weight:400}img{max-width:100%;height:auto}h1{font-weight:700;font-size:5rem;margin:2% 0}p{margin-bottom:20px}code{font-family:monospace;background-color:#ffc336;color:#000;font-size:1.1rem}a,a:active,a:hover,a:link,a:visited{color:#fff;text-decoration:none;border-bottom:2px solid #fff}body{height:100vh;display:flex;align-items:center;justify-content:center;flex-direction:column;font-family:Segoe UI,Tahoma,Geneva,Verdana,sans-serif;color:#fff;font-size:1rem;padding:0 30px;background:linear-gradient(25deg,#6356dd,#ac499f,#d23b62,#ec2a22)}"}}}
Empty file.
Empty file.
20 changes: 20 additions & 0 deletions test/fixtures/dummy/lego.js
@@ -0,0 +1,20 @@
module.exports = {
name: 'dummy',
inlineSource: true,
critical: {
inline: true,
dimensions: [
{
height: 800,
width: 470
},
{
height: 900,
width: 1200
}
],
penthouse: {
timeout: 150000
}
}
}
16 changes: 16 additions & 0 deletions test/fixtures/dummy/package.json
@@ -0,0 +1,16 @@
{
"name": "dummy",
"scripts": {
"start": "lego s",
"build": "lego"
},
"browserslist": [
"> 1%",
"last 1 versions",
"not ie 11",
"not dead"
],
"devDependencies": {
"@astronomersiva/lego": "^1.9.0"
}
}
Empty file.
19 changes: 19 additions & 0 deletions test/fixtures/dummy/pages/index.html
@@ -0,0 +1,19 @@

<!DOCTYPE html>
<html>
<head>
<title>Your awesome site</title>
<link href="/static/css/styles.css" rel="stylesheet">
</head>
<body>
<h1>Your awesome site!</h1>
<div>
<p>To generate a production build of this site, run <code>lego build</code>.</p>
<p>
Facing trouble? File an issue on
<a href="https://github.com/astronomersiva/lego/">GitHub!</a>
</p>
</div>
</body>
</html>

73 changes: 73 additions & 0 deletions test/fixtures/dummy/static/css/styles.css
@@ -0,0 +1,73 @@

html {
box-sizing: border-box;
font-size: 0.625;
}

* {
margin: 0;
padding: 0;
}

*,
*:before,
*:after {
box-sizing: inherit;
}

body,
h1,
h2,
h3,
h4,
h5,
h6,
p,
ol,
ul {
margin: 0;
padding: 0;
font-weight: normal;
}

img {
max-width: 100%;
height: auto;
}

h1 {
font-weight: 700;
font-size: 5rem;
margin: 2% 0px;
}

p {
margin-bottom: 20px;
}

code {
font-family: monospace;
background-color: rgb(255, 195, 54);
color: black;
font-size: 1.1rem;
}

a, a:link, a:visited, a:hover, a:active {
color: white;
text-decoration: none;
border-bottom: 2px solid white;
}

body {
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
color: white;
font-size: 1rem;
padding: 0px 30px;
background: linear-gradient(25deg, rgb(99, 86, 221), rgb(172, 73, 159), rgb(210, 59, 98), rgb(236, 42, 34));
}

0 comments on commit 6b5ecf2

Please sign in to comment.