Skip to content

Commit 69214ec

Browse files
committed
added ops argument to engine
1 parent 5f0848c commit 69214ec

6 files changed

Lines changed: 228 additions & 218 deletions

File tree

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ const express = require('express');
146146
const app = express();
147147
const engine = require('express-engine-jsx');
148148

149-
server.set('views', '/path/to/views');
149+
server.set('views', './path/to/views');
150150
server.set('view engine', 'jsx');
151151
server.engine('jsx', engine);
152152

@@ -167,17 +167,18 @@ const engine = require('express-engine-jsx');
167167
It's a function which takes three arguments:
168168

169169
* `path` - path to jsx file
170-
* `locals` - object with properties which will be local variables in jsx file
170+
* `props` - object with properties which will be local variables in jsx file. `locals` property will be visible for all deep components. Properties of `locals` object will be as local variables in all deep components.
171+
* `options` - optional, object which overwrites any of next three [global options](#options) `doctype`, `replace`, `renderer`
171172
* `callback` - optional, Node style callback which will receive result of `options.renderer` as second argument
172173

173-
If you pass to `engine` only path and locals then it will return html.
174+
If you pass to `engine` arguments without callback then it will return result of `renderer` of [global options](#options).
174175
```js
175176
engine('/path/to/view', {prop: 'value'}, (err, html) => console.log(html));
176177

177178
const html = engine('/path/to/view', {prop: 'value'});
178179
```
179180

180-
Also, it has method `engine.setOptions(options)` which can modify [options](#options)
181+
Also, it has method `engine.setOptions(options)` which can modify [global options](#options)
181182

182183
### options
183184

index.d.ts

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,14 @@ import {ParserOptions} from '@babel/parser/typings/babel-parser';
44
import {BabylonOptions} from 'babylon';
55
import {createElement, Context} from 'react';
66

7-
declare function engine(path: string, params: object, cb: HtmlCallback): void;
7+
declare function engine(path: string, params: Params, ops: Ops, cb: HtmlCallback): void;
8+
declare function engine(path: string, params: Params, cb: HtmlCallback): void;
89
declare function engine(path: string, cb: HtmlCallback): void;
9-
declare function engine(path: string, params?: object): HtmlResult;
10+
declare function engine(path: string, params?: Params, ops?: Ops): HtmlResult;
1011

1112
type HtmlCallback = (err: null|Error, html: HtmlResult) => void;
1213

1314
declare namespace engine {
14-
interface Options {
15-
DEV?: boolean,
16-
doctype?: string,
17-
renderer?: (node: ReturnType<typeof createElement>) => HtmlResult,
18-
replace?: (html: HtmlResult, params: object) => HtmlResult,
19-
templatePath?: string,
20-
parserOptions?: ParserOptions,
21-
sourceMap?: boolean,
22-
addOnChange?: boolean,
23-
}
24-
2515
interface ConvertOptions {
2616
path?: string,
2717
sourceMap?: boolean,
@@ -51,5 +41,25 @@ declare namespace engine {
5141

5242
export = engine;
5343

44+
interface Params {
45+
locals?: object,
46+
_locals?: object,
47+
settings?: object,
48+
[prop: string]: any,
49+
}
50+
5451
type JsxCode = string | Buffer;
55-
type HtmlResult = string | Stream;
52+
type HtmlResult = string | Stream;
53+
54+
interface Options {
55+
DEV?: boolean,
56+
doctype?: string,
57+
renderer?: (node: ReturnType<typeof createElement>) => HtmlResult,
58+
replace?: (html: HtmlResult, params: object) => HtmlResult,
59+
templatePath?: string,
60+
parserOptions?: ParserOptions,
61+
sourceMap?: boolean,
62+
addOnChange?: boolean,
63+
}
64+
65+
type Ops = Pick<Options, 'renderer' | 'replace' | 'doctype'>;

index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,15 @@ const Context = require('./Context');
88

99
module.exports = engine;
1010

11-
function engine(path, params = {}, cb = null) {
11+
function engine(path, params = {}, ops = null, cb = null) {
1212
if (typeof params === 'function') {
1313
cb = params;
1414
params = {};
15+
ops = null;
16+
}
17+
else if (typeof ops === 'function') {
18+
cb = ops;
19+
ops = null;
1520
}
1621

1722
const Component = requireJSX(path.replace(/\.jsx$/, ''));
@@ -21,7 +26,7 @@ function engine(path, params = {}, cb = null) {
2126
settings: params.settings,
2227
};
2328

24-
const {renderer, replace, doctype} = options;
29+
const {renderer, replace, doctype} = Object.assign({}, options, ops);
2530

2631
try {
2732
var html = renderer(

0 commit comments

Comments
 (0)