Skip to content

Commit

Permalink
resolve templates from their own paths
Browse files Browse the repository at this point in the history
  • Loading branch information
jescalan committed Jul 12, 2017
1 parent a579836 commit d9fd482
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 25 deletions.
8 changes: 6 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ function writeTemplates (compilation, compiler, _data, k) {
if (!Array.isArray(data)) { throw new Error('template data is not an array') }

// First we read the template file
return node.call(fs.readFile.bind(fs), path.join(root, tpl.path), 'utf8')
const tplPath = path.join(root, tpl.path)
return node.call(fs.readFile.bind(fs), tplPath, 'utf8')
.then((template) => {
// Now we go through each item in the data array to render a template
return W.map(data, (item) => {
Expand All @@ -199,7 +200,10 @@ function writeTemplates (compilation, compiler, _data, k) {
const options = loader.parseOptions.call(this.loaderContext, this.util.getSpikeOptions().reshape, {})

// And finally, we run reshape to generate the template!
return reshape(Object.assign(options, { locals: newLocals }))
return reshape(Object.assign(options, {
locals: newLocals,
filename: tplPath
}))
.process(template)
.then((res) => {
const rendered = res.output(newLocals)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"posthtml-exp": "^0.9.0",
"reshape-standard": "^2.0.0",
"rimraf": "^2.5.4",
"spike-core": "^2.0.0",
"spike-core": "^2.0.1",
"standard": "^10.0.2"
},
"engines": {
Expand Down
Empty file.
2 changes: 2 additions & 0 deletions test/fixtures/tpl_resolve/layout.sgr
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
p layout
block(name='content')
3 changes: 3 additions & 0 deletions test/fixtures/tpl_resolve/templates/tpl.sgr
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
extends(src='../layout.sgr')
block(name='content')
p template
35 changes: 29 additions & 6 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ test('loads a file correctly', (t) => {
fixture: 'data',
locals: locals,
config: { addDataTo: locals, test: { file: '../testFile.json' } },
verify: (_, publicPath, cb) => {
verify: (_, publicPath) => {
const out = fs.readFileSync(path.join(publicPath, 'index.html'), 'utf8')
t.is(out.trim(), '<p>true</p>')
}
Expand All @@ -40,7 +40,7 @@ test('loads a url correctly', (t) => {
fixture: 'data',
locals: locals,
config: { addDataTo: locals, test: { url: 'http://api.bycarrot.com/v3/staff' } },
verify: (_, publicPath, cb) => {
verify: (_, publicPath) => {
const out = fs.readFileSync(path.join(publicPath, 'index.html'), 'utf8')
t.is(out.trim(), '<p>true</p>')
}
Expand All @@ -61,7 +61,7 @@ test('loads a graphql endpoint correctly', (t) => {
}
}
},
verify: (_, publicPath, cb) => {
verify: (_, publicPath) => {
const out = fs.readFileSync(path.join(publicPath, 'index.html'), 'utf8')
t.regex(out, /test/)
}
Expand All @@ -80,7 +80,7 @@ test('transform option works', (t) => {
transform: (data) => { return { success: false } }
}
},
verify: (_, publicPath, cb) => {
verify: (_, publicPath) => {
const out = fs.readFileSync(path.join(publicPath, 'index.html'), 'utf8')
t.is(out.trim(), '<p>false</p>')
}
Expand Down Expand Up @@ -165,7 +165,7 @@ test('single template works with "path" and "template" params', (t) => {
}
}
},
verify: (_, publicPath, cb) => {
verify: (_, publicPath) => {
const index = fs.readFileSync(path.join(publicPath, 'index.html'), 'utf8')
const wow = fs.readFileSync(path.join(publicPath, 'posts/wow.html'), 'utf8')
const amaze = fs.readFileSync(path.join(publicPath, 'posts/amaze.html'), 'utf8')
Expand All @@ -192,7 +192,7 @@ test('single template works with "transform" param', (t) => {
}
}
},
verify: (_, publicPath, cb) => {
verify: (_, publicPath) => {
const index = fs.readFileSync(path.join(publicPath, 'index.html'), 'utf8')
const wow = fs.readFileSync(path.join(publicPath, 'posts/wow.html'), 'utf8')
const amaze = fs.readFileSync(path.join(publicPath, 'posts/amaze.html'), 'utf8')
Expand All @@ -203,6 +203,29 @@ test('single template works with "transform" param', (t) => {
})
})

test('template resolves include/layouts from its own path', (t) => {
const locals = {}
return compileAndCheck({
fixture: 'tpl_resolve',
locals: locals,
config: {
addDataTo: locals,
posts: {
data: { response: [{ title: 'wow' }] },
template: {
transform: (data) => data.response,
path: 'templates/tpl.sgr',
output: (item) => `posts/${item.title}.html`
}
}
},
verify: (_, publicPath) => {
const wow = fs.readFileSync(path.join(publicPath, 'posts/wow.html'), 'utf8')
t.is(wow.trim(), '<p>layout</p>\n<p>template</p>')
}
})
})

//
// Utilities
//
Expand Down
32 changes: 16 additions & 16 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1026,11 +1026,11 @@ browser-sync-ui@0.6.3:
stream-throttle "^0.1.3"
weinre "^2.0.0-pre-I0Z7U9OV"

browser-sync-webpack-plugin@^1.1.4:
version "1.1.4"
resolved "https://registry.yarnpkg.com/browser-sync-webpack-plugin/-/browser-sync-webpack-plugin-1.1.4.tgz#ad3c0f7a3bd2d3e7f87cdc378ee93e7be2026d89"
browser-sync-webpack-plugin@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/browser-sync-webpack-plugin/-/browser-sync-webpack-plugin-1.2.0.tgz#33358dde35121f12cbdab46d4453e67b8357726a"
dependencies:
lodash "4.x.x"
lodash "^4"

browser-sync@^2.18.12:
version "2.18.12"
Expand Down Expand Up @@ -3775,14 +3775,14 @@ lodash.uniq@^4.3.0:
version "4.5.0"
resolved "https://registry.yarnpkg.com/lodash.uniq/-/lodash.uniq-4.5.0.tgz#d0225373aeb652adc1bc82e4945339a842754773"

lodash@4.x.x, lodash@^4.0.0, lodash@^4.12.0, lodash@^4.14.0, lodash@^4.2.0, lodash@^4.3.0:
version "4.17.4"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.4.tgz#78203a4d1c328ae1d86dca6460e369b57f4055ae"

lodash@^3.10.1:
version "3.10.1"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-3.10.1.tgz#5bf45e8e49ba4189e17d482789dfd15bd140b7b6"

lodash@^4, lodash@^4.0.0, lodash@^4.12.0, lodash@^4.14.0, lodash@^4.2.0, lodash@^4.3.0:
version "4.17.4"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.4.tgz#78203a4d1c328ae1d86dca6460e369b57f4055ae"

log-driver@1.2.5:
version "1.2.5"
resolved "https://registry.yarnpkg.com/log-driver/-/log-driver-1.2.5.tgz#7ae4ec257302fd790d557cb10c97100d857b0056"
Expand Down Expand Up @@ -5827,14 +5827,14 @@ spdx-license-ids@^1.0.2:
version "1.2.2"
resolved "https://registry.yarnpkg.com/spdx-license-ids/-/spdx-license-ids-1.2.2.tgz#c9df7a3424594ade6bd11900d596696dc06bac57"

spike-core@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/spike-core/-/spike-core-2.0.0.tgz#1e7b99fd37d117a840239e0771e64fe2ac18e610"
spike-core@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/spike-core/-/spike-core-2.0.1.tgz#621311e9885f08fe1e76d2899569ab6ed1a8c271"
dependencies:
babel-core "^6.25.0"
babel-loader "^7.1.1"
browser-sync "^2.18.12"
browser-sync-webpack-plugin "^1.1.4"
browser-sync-webpack-plugin "^1.2.0"
filewrap "^1.0.0"
glob "^7.1.2"
hygienist-middleware "^0.1.3"
Expand All @@ -5850,7 +5850,7 @@ spike-core@^2.0.0:
source-loader "^0.2.0"
spike-util "^1.2.0"
sprout "^1.2.1"
webpack "^3.0.0"
webpack "^3.1.0"
when "^3.7.8"

spike-util@^1.2.0:
Expand Down Expand Up @@ -6528,9 +6528,9 @@ webpack-sources@^1.0.1:
source-list-map "^2.0.0"
source-map "~0.5.3"

webpack@^3.0.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/webpack/-/webpack-3.1.0.tgz#ac0675e500db835f9ab2369d29ba096f51ad0731"
webpack@^3.1.0:
version "3.2.0"
resolved "https://registry.yarnpkg.com/webpack/-/webpack-3.2.0.tgz#8b0cae0e1a9fd76bfbf0eab61a8c2ada848c312f"
dependencies:
acorn "^5.0.0"
acorn-dynamic-import "^2.0.0"
Expand Down

0 comments on commit d9fd482

Please sign in to comment.