Skip to content

Commit 171c475

Browse files
committed
Merge branch 'feature/resource-query'
Merges #10
2 parents 5b788aa + 35e7d82 commit 171c475

File tree

5 files changed

+28
-2
lines changed

5 files changed

+28
-2
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ module.exports = {
4040
};
4141
```
4242

43+
Data may also be passed through a resource query. These data take precedence
44+
over any options with the same name.
45+
46+
```js
47+
import "./index.ejs?page=home";
48+
```
49+
4350
## Options
4451

4552
All properties passed as loader options will be available to your

src/index.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {getOptions} from 'loader-utils';
1+
import {getOptions, parseQuery} from 'loader-utils';
22
import {addDependencies, saveDependencies} from './cache';
33
import {render} from './render';
44

@@ -16,14 +16,22 @@ export default function ejsHtmlLoader(src) {
1616
}
1717

1818
function renderTemplate(ctx, src) {
19-
let data = getOptions(ctx);
19+
let data = getData(ctx);
2020
let {rendered, deps} = render(ctx, src, data);
2121

2222
saveDependencies(ctx, deps);
2323

2424
return rendered;
2525
}
2626

27+
function getData(ctx) {
28+
return Object.assign({}, getOptions(ctx), getResourceQuery(ctx));
29+
}
30+
31+
function getResourceQuery(ctx) {
32+
return parseQuery(ctx.resourceQuery || "?");
33+
}
34+
2735
function emitError(ctx, msg) {
2836
addDependencies(ctx);
2937
ctx.emitError(`ejs-html-loader\n${msg}`);

test/fixtures/resource-query.ejs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<%- one %><%- two %><%- three %>

test/fixtures/resource-query.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require("./resource-query.ejs?one=abc&two=xyz");

test/index.coffee

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ describe 'ejs-html-loader', ->
3131
entry: 'simple.js'
3232
query: 'heading=togo'
3333

34+
context 'options passed through resource query', ->
35+
36+
it 'renders template', ->
37+
compile /abcxyz123/,
38+
entry: 'resource-query.js'
39+
ejsHtml:
40+
two: '987'
41+
three: '123'
42+
3443
context 'with include statement', ->
3544

3645
it 'includes partial', ->

0 commit comments

Comments
 (0)