Skip to content

Commit 850540b

Browse files
committed
Merge branch 'develop'
2 parents d064ef8 + 1e2aa7d commit 850540b

File tree

11 files changed

+70
-37
lines changed

11 files changed

+70
-37
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ WOK is a loosely opinionated boilerplate for web development built with flexibil
2020
## Supported browsers (via [browserslist](https://github.com/browserslist/browserslist))
2121

2222
* last 1 major version
23-
* >= 1%
23+
* \>= 1%
2424
* Chrome >= 45
2525
* Firefox >= 38
2626
* Edge >= 12
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{% extends 'templates/_default.nunj.html' %}
1+
{% extends 'templates/_default.njk' %}
22

33

44
{% block main %}
File renamed without changes.

build/gulp-config/properties.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ module.exports = {
88

99
buildOnly: false, //set to `true` when paired with Phing
1010

11-
viewmatch: '*.{html,njk}' //for php projects use: '*.{html,php,phtml}'
11+
viewsExt: ['html', 'njk'] //for php projects use: '*.{html,php,phtml}'
1212
};

build/gulp-tasks/lib/plugins.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44

55
const through = require('through2');
6-
const gutils = require('gulp-util');
6+
const PluginError = require('plugin-error');
77

88
module.exports.map = function map(fn) {
99

@@ -18,14 +18,14 @@ module.exports.map = function map(fn) {
1818
if (file.isStream()) {
1919
this.emit(
2020
'error',
21-
new gutils.PluginError('gulp-boilerplate', 'Streaming not supported')
21+
new PluginError('wok-map', 'Streaming not supported')
2222
);
2323
}
2424

2525
try {
26-
file.contents = new Buffer(mapFn(file.contents.toString(), file.path, file)); //eslint-disable-line no-param-reassign
26+
file.contents = Buffer.from(mapFn(file.contents.toString(), file.path, file)); //eslint-disable-line no-param-reassign
2727
} catch (err) {
28-
this.emit('error', new gutils.PluginError('gulp-boilerplate', err.toString()));
28+
this.emit('error', new PluginError('wok-map', err.toString()));
2929
}
3030

3131
this.push(file);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const nunjucks = require('./nunjucks');
2+
3+
module.exports = (...args) => {
4+
const entries = [nunjucks].map(({ createRenderer }) => createRenderer(...args));
5+
6+
return {
7+
entries,
8+
match(filepath) {
9+
return entries.find(({ match }) => match(filepath));
10+
}
11+
};
12+
13+
};

build/gulp-tasks/lib/view-helpers.js renamed to build/gulp-tasks/lib/renderers/nunjucks.js

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22
* View Compilation Helpers
33
*/
44

5-
const _ = require('lodash');
65
const random = require('lodash/random');
76
const defaults = require('lodash/defaults');
8-
const nunjucks = require('nunjucks');
9-
const marked = require('marked');
10-
const Markdown = require('nunjucks-markdown/lib/markdown_tag');
117
const loremIpsum = require('lorem-ipsum');
128

139
const nunjucksEnv = (viewPath) => {
10+
11+
const nunjucks = require('nunjucks');
12+
const marked = require('marked');
13+
const Markdown = require('nunjucks-markdown/lib/markdown_tag');
14+
15+
1416
const env = nunjucks.configure(viewPath, {
1517
noCache: true
1618
});
@@ -44,9 +46,13 @@ const nunjucksEnv = (viewPath) => {
4446

4547
};
4648

47-
const defaultHelpers = (/*options*/) => {
4849

49-
return {
50+
51+
module.exports.createRenderer = (viewPaths, options, helpers) => {
52+
53+
const matchRegExp = /\.(njk|html)$/;
54+
55+
const defaultHelpers = {
5056
lorem(min, max, config) {
5157
const count = max ? random(min, max) : min;
5258
const loremDefaults = {
@@ -59,20 +65,22 @@ const defaultHelpers = (/*options*/) => {
5965
}
6066
};
6167

62-
};
63-
64-
const noopHelper = () => ({});
65-
66-
module.exports.createRenderer = (viewPaths, options, helpers = noopHelper) => {
67-
68-
const env = nunjucksEnv(viewPaths, options);
69-
70-
env.addGlobal('helpers', Object.assign(helpers(options), defaultHelpers(options)));
71-
env.addGlobal('_', _);
68+
let env;
7269

7370
return {
74-
env,
71+
get env() {
72+
return env;
73+
},
74+
name: 'nunjucks',
75+
extensions: ['njk', 'html'],
76+
match: (filepath) => matchRegExp.test(filepath),
7577
render(string, locals) {
78+
//lazy load renderer
79+
if (!env) {
80+
env = nunjucksEnv(viewPaths, options);
81+
env.addGlobal('helpers', Object.assign({}, defaultHelpers, helpers && helpers(options)));
82+
env.addGlobal('_', require('lodash'));
83+
}
7684
return env.renderString(string, locals);
7785
}
7886
};

build/gulp-tasks/views.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,21 @@ module.exports = (gulp, $, options) => {
1313
const glob = require('globby');
1414
const through = require('through2');
1515
const { map } = require('./lib/plugins');
16-
const { createRenderer } = require('./lib/view-helpers');
16+
const rendererCreator = require('./lib/renderers');
1717

1818
const baseData = {};
1919
const paths = require('../gulp-config/paths');
2020
const viewPath = paths.toAbsPath('src.views');
2121
const fixturesPath = paths.toAbsPath('src.fixtures');
22-
const { production, viewmatch, banners } = options;
22+
const { production, banners, viewmatch } = options;
2323

2424
let useRef = () => through.obj();
2525

2626

2727
baseData.PRODUCTION = production;
2828
baseData.page = {};
2929

30-
const helpers = () => ({});
31-
32-
const renderer = createRenderer([viewPath, paths.toPath('src.documents')], options, helpers);
30+
const renderer = rendererCreator([viewPath, paths.toPath('src.documents')], options);
3331

3432
if (production) {
3533

@@ -83,11 +81,16 @@ module.exports = (gulp, $, options) => {
8381
.pipe($.plumber({
8482
errorHandler: $.notify.onError('Error: <%= error.message %>')
8583
}))
86-
.pipe(map((code) => renderer.render(code, Object.assign({}, baseData, data || {}))))
84+
.pipe(map((code, filepath) => {
85+
const engine = renderer.match(filepath);
86+
if (engine) {
87+
return engine.render(code, Object.assign({}, baseData, data || {}));
88+
}
89+
return code;
90+
}))
8791
.pipe(useRef())
88-
.pipe($.rename((filepath) => {
89-
filepath.basename = filepath.basename.replace('.nunj', ''); //eslint-disable-line no-param-reassign
90-
filepath.extname = '.html'; //eslint-disable-line no-param-reassign
92+
.pipe($.rename({
93+
extname: '.html'
9194
}))
9295
.pipe(htmlFilter)
9396
.pipe(gulp.dest(paths.toPath('dist.views')))

build/gulp-tasks/watch.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ module.exports = (gulp, $, options) => {
105105
match: `src.views/{,*/}${viewmatch}`,
106106
resolve(filepath) {
107107
let src = path.relative(paths.toPath('src.views'), filepath);
108-
src = src.replace(/\.(nunj\.html|njk)$/, '.html');
108+
const ext = path.extname(filepath);
109+
src = src.replace(ext, '.html');
109110
return path.resolve(paths.toPath('dist.views'), src);
110111
}
111112
}

gulpfile.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const gulp = require('gulp');
1111
const glob = require('globby');
1212
const _ = require('lodash');
1313
const log = require('fancy-log');
14-
const { red } = require('ansi-colors');
14+
const { red, yellow } = require('ansi-colors');
1515
const $ = require('gulp-load-plugins')();
1616
const { argv = {} } = require('yargs');
1717
const pkg = require('./package.json');
@@ -24,7 +24,8 @@ const banners = {
2424
vendors: `/*! ${pkg.description} v${pkg.version} - ${pkg.author.name} - Vendor package */\n`
2525
};
2626

27-
const logError = (...args) => log(red(...args));
27+
const logError = _.flow(red, log);
28+
const warn = _.flow(yellow, log.warn);
2829

2930

3031
pkg.year = new Date().getFullYear();
@@ -60,7 +61,7 @@ if (fs.existsSync(path.join(optionsPath, 'paths.local.js'))) {
6061
}
6162

6263
if (_.has(argv, 'remotehost')) {
63-
logError('WARNING: param `--remotehost` is deprecated use `--target` instead');
64+
warn('WARNING: param `--remotehost` is deprecated use `--target` instead');
6465
argv.target = argv.remotehost;
6566
}
6667

@@ -73,6 +74,12 @@ _.forOwn({
7374
options[key] = _.get(argv, key, value);
7475
});
7576

77+
if (options.viewmatch) {
78+
logError('`viewmatch` property has been removed. Use `viewsExt: [...]`');
79+
process.exit();
80+
}
81+
options.viewmatch = `*.{${options.viewsExt.join(',')}}`;
82+
7683
//force production env
7784
if (options.production) {
7885
process.env.NODE_ENV = 'production';

0 commit comments

Comments
 (0)