Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
start preparing for release
  • Loading branch information
jonschlinkert committed Nov 13, 2018
1 parent 4b4df2e commit 8b8cad7
Show file tree
Hide file tree
Showing 80 changed files with 589 additions and 1,654 deletions.
67 changes: 33 additions & 34 deletions bench/index.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,57 @@
'use strict';

const bench = require('./setup/bench');
const engine = require('engine-handlebars');
const handlebars = require('handlebars');
const engine = require('../examples/support/engine');
const bench = require('./setup/bench');

const Templates = require('..');
const Collection = Templates.Collection;
const View = Templates.View;
const { Collection, File } = Templates;

/**
* Layouts
*/

// let initialized = false;

// app = new Templates({ asyncHelpers: false });
// app.engine('hbs', engine(handlebars));
let initialized = false;
let app = new Templates({ asyncHelpers: false });
app.engine('hbs', engine(handlebars));

// app.create('pages');
// app.create('layouts', { kind: 'layout' });
// app.layouts.set({ path: 'foo', contents: Buffer.from('before {% body %} after') });
// app.layouts.set({ path: 'base', contents: Buffer.from('before {% body %} after'), layout: 'foo' });
// app.layouts.set({ path: 'default', contents: Buffer.from('before {% body %} after'), layout: 'base' });
app.create('pages');
app.create('layouts', { kind: 'layout' });
app.layouts.set({ path: 'foo', contents: Buffer.from('before {% body %} after') });
app.layouts.set({ path: 'base', contents: Buffer.from('before {% body %} after'), layout: 'foo' });
app.layouts.set({ path: 'default', contents: Buffer.from('before {% body %} after'), layout: 'base' });

// const view = app.pages.set('foo', {
// contents: Buffer.from('Name: {{name}}, {{description}}'),
// data: { name: 'Brian' },
// layout: 'default'
// });
const file = app.pages.set('foo', {
contents: Buffer.from('Name: {{name}}, {{description}}'),
data: { name: 'Brian' },
layout: 'default'
});

// // console.log(app)
// console.log(app)

// bench('layouts')
// // .add('collection.renderLayout()', () => {
// // const collection = new Collection('layouts', { kind: 'layout '});
// // collection.set({ path: 'default', contents: 'before {{name}} after' });
// // const view = new View({ path: 'foo/bar', contents: 'before {{name}} after' });
// // })
// .add('collection.renderLayout()', async() => {
bench('layouts')
.add('collection.renderLayout()', () => {
const collection = new Collection('layouts', { kind: 'layout '});
collection.set({ path: 'default', contents: 'before {{name}} after' });
const file = new File({ path: 'foo/bar', contents: 'before {{name}} after' });
})
.add('collection.renderLayout()', async() => {

// // await app.render(view, { description: 'This is a page' });
// // console.log(view.contents.toString())
// await app.render(file, { description: 'This is a page' });
// console.log(file.contents.toString())

// })
// .run({ async: true });
})
.run({ async: true });

// bench('rendering')
// .add('app.render()', () => {
// let app = new Templates();
// const view = new View({ path: 'foo/bar', contents: 'before {{name}} after' });
// app = new Templates();
// const file = new File({ path: 'foo/bar', contents: 'before {{name}} after' });

// })
// .run();

// const app = new App({
// const app = new Templates({
// handlers: [
// 'onLoad',
// 'preCompile',
Expand All @@ -69,4 +68,4 @@ const View = Templates.View;
// layouts.set({ path: 'default.hbs', contents: Buffer.from('before {% body %} after') });

// app.engine('hbs', engine(handlebars));

// console.log(app);
2 changes: 2 additions & 0 deletions bench/init.js → bench/load.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// require('time-require');
console.time('# templates');
require('templates');
console.timeEnd('# templates');
// ~8.45ms - 2018-08-17
// ~8.707ms - 2018-11-11
8 changes: 2 additions & 6 deletions bench/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
"name": "benchmarks",
"private": true,
"dependencies": {
"ansi": "^0.3.1",
"ansi-colors": "^2.0.1",
"benchmark": "^2.1.4",
"engine-handlebars": "^1.0.0",
"get-value": "^3.0.1",
"handlebars": "^4.0.11",
"lorem-ipsum": "^1.0.4",
Expand All @@ -16,14 +15,11 @@
"templates": "file:..",
"write": "^1.0.3"
},
"devDependencies": {
"time-require": "github:jonschlinkert/time-require"
},
"lintDeps": {
"dependencies": {
"files": {
"patterns": [
"generate/*.js",
"rendering/**/*.js",
"*.js"
]
}
Expand Down
2 changes: 1 addition & 1 deletion bench/rendering/app-layouts-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const runner = require('setup/runner');
await run(100);
await run(1000);
await run(10000);
// await run(100000);
await run(100000);
// await run(1000000); // 1m
})();

Expand Down
2 changes: 1 addition & 1 deletion bench/rendering/app-layouts-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@ run(10);
run(100);
run(1000);
run(10000);
// run(100000);
run(100000);
// run(1000000);
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
const fs = require('fs');
const path = require('path');
const util = require('util');
const mkdir = require('@folder/mkdir');
const mkdir = require('mkdirp');
const rimraf = require('rimraf');
const write = util.promisify(fs.writeFile);
const loremipsum = require('lorem-ipsum');
const defaults = {
startDate: 'December 17, 2010',
dest: 'blog',
dest: 'content',
count: 1600,
units: 'words',
pages: 1000
Expand All @@ -35,8 +35,7 @@ async function generate(options = {}) {
{{#each (filter site.tags.tags tags) as |tag|}}
<a href="{{path}}">{{items.length}}</a>
{{/each}}
`
+ contents + `
${contents}
<h2>Pages</h2>
<a href="{{pagerFirst pager "path"}}">First</a>
<a href="{{pagerPrev pager "path"}}">Prev</a>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const fs = require('fs');
const path = require('path');
const util = require('util');
const rimraf = require('rimraf');
const mkdir = require('@folder/mkdir');
const mkdir = require('mkdirp');
const pretty = require('pretty-time');
const colors = require('ansi-colors');
const write = require('write');
Expand Down Expand Up @@ -56,45 +56,45 @@ function render(destDir) {

// collection
starting('assemble - create collection');
const collection = new Collection('pages');
const collection = new Collection('pages', { sync: true });
collection.option('engine', 'noop');
finished('assemble - create collection', diff());

// parse front matter and add views
starting('assemble - add views and parse front-matter', diff());
// parse front matter and add files
starting('assemble - add files and parse front-matter', diff());
for (const filename of files) {
if (/\.md$/.test(filename)) {
const view = collection.set(parse({ path: cwd(filename), cwd: cwd() }));
view[symbol] = {};
rename(view);
const file = collection.set(parse({ path: cwd(filename), cwd: cwd() }));
file[symbol] = {};
rename(file);
}
}
finished('assemble - add views and parse front-matter', diff());
finished('assemble - add files and parse front-matter', diff());

// render views
starting('assemble - render views');
for (const [key, view] of collection.views) {
collection.render(view, { site: { paths: { root: destBase() } }});
// render files
starting('assemble - render files');
for (const [key, file] of collection.files) {
collection.render(file, { site: { paths: { root: destBase() } }});
}
finished('assemble - render views', diff());
finished('assemble - render files', diff());

// write files
starting('assemble - write rendered views to fs');
starting('assemble - write rendered files to fs');
let i = 0;
for (const [key, view] of collection.views) {
if (!dirs.has(view.dirname)) {
dirs.add(view.dirname);
mkdir(view.dirname);
for (const [key, file] of collection.files) {
if (!dirs.has(file.dirname)) {
dirs.add(file.dirname);
mkdir(file.dirname);
}
rename(view);
write.sync(view.path, view.contents);
rename(file);
write.sync(file.path, file.contents);
i++;
}

finished('assemble - write rendered views to fs', diff());
finished('assemble - write rendered files to fs', diff());
finished('assemble - build', assembled());
finished(`total build (generated ${i} pages)`, total());
console.log('per view:', colors.green(pretty(ns(process.hrtime(grand)) / i, 2)));
console.log('per file:', colors.green(pretty(ns(process.hrtime(grand)) / i, 2)));
}

function parse(file) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ const rimraf = require('rimraf');
const pretty = require('pretty-time');
const colors = require('ansi-colors');
const write = require('write');
const Collection = require('templatestemplateslib/collection');
const engines = require('templatestemplateslib/engines');
const hbs = require('handlebars');
const engine = require('engine-handlebars');
const Collection = require('../../../lib/collection');
const cwd = path.join.bind(path, __dirname, 'content');
const destBase = path.join.bind(path, __dirname, 'blog');
const dirs = new Set();
Expand Down Expand Up @@ -58,10 +59,9 @@ function render(destDir) {

// collection
starting('assemble - create collection');
const collection = new Collection('pages');
const hbs = require('handlebars');
const collection = new Collection('pages', { sync: true });

collection.engine('hbs', engines(hbs));
collection.engine('hbs', engine(hbs));
collection.option('engine', 'hbs');

collection.helper('array', function(arr, i) {
Expand Down Expand Up @@ -142,33 +142,33 @@ function render(destDir) {

finished('assemble - create collection', diff());

// parse front matter and add views
starting('assemble - add views and parse front-matter', diff());
// parse front matter and add files
starting('assemble - add files and parse front-matter', diff());
for (const filename of files) {
if (/\.md$/.test(filename)) {
const view = collection.set(parse({ path: cwd(filename), cwd: cwd() }));
view[symbol] = {};
rename(view);
const file = collection.set(parse({ path: cwd(filename), cwd: cwd() }));
file[symbol] = {};
rename(file);
}
}
finished('assemble - add views and parse front-matter', diff());
finished('assemble - add files and parse front-matter', diff());

const tags = collection.collect('tags', { singular: 'tag' });

// paginate views
starting('assemble - paginate views');
// paginate files
starting('assemble - paginate files');
collection.pager({
sort(items) {
return items.sort((a, b) => a.path.localeCompare(b.path));
}
});

collection.paginate(page => {
const view = collection.set(page);
view.path = cwd(view.relative);
view.base = cwd();
view.cwd = cwd();
view.contents = Buffer.from(`
const file = collection.set(page);
file.path = cwd(file.relative);
file.base = cwd();
file.cwd = cwd();
file.contents = Buffer.from(`
<h1>Posts</h1>
<ul>
{{#each pagination.items}}
Expand All @@ -182,10 +182,10 @@ function render(destDir) {
<a href="{{pagerNext pager "path"}}">Next</a>
<a href="{{pagerLast pager "path"}}">Last</a>
`);
rename(view);
return view;
rename(file);
return file;
});
finished('assemble - paginate views', diff());
finished('assemble - paginate files', diff());
tags.items.forEach(item => {
item.key = item.path;
collection.set(item);
Expand Down Expand Up @@ -214,30 +214,30 @@ function render(destDir) {
tags.paths[key] = destBase(tags.paths[key]);
}

// render views
starting('assemble - render views');
for (const [key, view] of collection.views) {
collection.render(view, { site: { paths: { root: destBase() }, tags }});
// render files
starting('assemble - render files');
for (const [key, file] of collection.files) {
collection.render(file, { site: { paths: { root: destBase() }, tags }});
}
finished('assemble - render views', diff());
finished('assemble - render files', diff());

// write files
starting('assemble - write rendered views to fs');
starting('assemble - write rendered files to fs');
let i = 0;
for (const [key, view] of collection.views) {
if (!dirs.has(view.dirname)) {
dirs.add(view.dirname);
mkdir(view.dirname);
for (const [key, file] of collection.files) {
if (!dirs.has(file.dirname)) {
dirs.add(file.dirname);
mkdir(file.dirname);
}
rename(view);
write.sync(view.path, view.contents);
rename(file);
write.sync(file.path, file.contents);
i++;
}

finished('assemble - write rendered views to fs', diff());
finished('assemble - write rendered files to fs', diff());
finished('assemble - build', assembled());
finished(`total build (generated ${i} pages)`, total());
console.log('per view:', colors.green(pretty(ns(process.hrtime(grand)) / i, 2)));
console.log('per file:', colors.green(pretty(ns(process.hrtime(grand)) / i, 2)));
}

function parse(file) {
Expand Down
Loading

0 comments on commit 8b8cad7

Please sign in to comment.