Skip to content

Commit

Permalink
update dependency reporting (#12)
Browse files Browse the repository at this point in the history
* update dependency reporting
* fix an issue with options being mutated across multiple compiles
* update deps
  • Loading branch information
jescalan authored Apr 5, 2017
1 parent 92e3319 commit edf7714
Show file tree
Hide file tree
Showing 8 changed files with 4,142 additions and 23 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# This file is for unifying the coding style for different editors and IDEs
# editorconfig.org

root = true

[*]
end_of_line = lf
charset = utf-8
insert_final_newline = true
trim_trailing_whitespace = true
indent_style = space
indent_size = 2
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
.nyc_output
3 changes: 3 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
.travis.yml
test
yarn.lock
contributing.md
.editorconfig
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,24 @@ All options are optional, none are required.
| ---- | ----------- | ------- |
| **root** | root to resolve layout paths from | reshape `filename` option |
| **encoding** | encoding with which to read layout files | `utf8` |
| **addDependencyTo** | pass webpack loader context to correctly specify dependencies in watch mode | |

### Reporting Dependencies

This plugin will report its dependencies in the standard format as dictated by [reshape-loader](https://github.com/reshape/loader) if you pass `dependencies: []` as an option to reshape when it runs. Dependencies will be available on the output object under the `dependencies` key. For example:

```js
const reshape = require('reshape')
const include = require('reshape-layouts')

reshape({ plugins: [layouts()], dependencies: []})
.process(someHtml)
.then((res) => {
console.log(res.dependencies)
console.log(res.output())
})
```

If you are using this with webpack, reshape-loader takes care of the dependency reporting and you don't have to do anything 😁

### License & Contributing

Expand Down
23 changes: 13 additions & 10 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ const path = require('path')

module.exports = function reshapeLayouts (options = {}) {
return function layoutsPlugin (tree, ctx) {
options.encoding = options.encoding || 'utf8'
if (!options.root && ctx.filename) {
options.root = path.dirname(ctx.filename)
}
options.root = options.root || './'
if (options.addDependencyTo && !(typeof options.addDependencyTo.addDependency === 'function')) {
throw new Error('[reshape-layouts] "addDependencyTo" does not have an "addDependency" method')
// options cannot be mutated per-compile
const opts = Object.assign({}, options)

opts.encoding = opts.encoding || 'utf8'
if (!opts.root && ctx.filename) {
opts.root = path.dirname(ctx.filename)
}
opts.root = opts.root || './'

// primary logic
tree = handleExtendsNodes(tree, options, ctx)
tree = handleExtendsNodes(tree, opts, ctx)

// remove block wrapper around contents
tree = unwrapBlocks(tree)
Expand Down Expand Up @@ -43,8 +43,11 @@ function handleExtendsNodes (tree, options, ctx) {
const layoutTree = handleExtendsNodes(parsedLayout, options, ctx)

// add dependency if applicable
if (options.addDependencyTo) {
options.addDependencyTo.addDependency(layoutPath)
if (ctx.dependencies) {
ctx.dependencies.push({
file: layoutPath,
parent: ctx.filename
})
}

// merge the contents of the current node into the layout
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "reshape-layouts",
"description": "flexible layouts using 'extend' and 'block' tags",
"version": "0.2.1",
"version": "0.3.0-1",
"author": "Jeff Escalante",
"ava": {
"verbose": "true",
Expand Down
17 changes: 6 additions & 11 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,16 @@ test('resolves correctly with reshape filename option', (t) => {
})
})

test.cb('addDependencyTo option', (t) => {
test('reports dependencies correctly', (t) => {
const p = path.join(fixtures, 'basic.html')
const html = fs.readFileSync(p, 'utf8')
const ctx = {
addDependency: (dep) => {
t.truthy(dep.match(/\/test\/fixtures\/layout\.html/))
t.end()
}
}

reshape({
plugins: layouts({ addDependencyTo: ctx }),
filename: p
}).process(html)
return reshape({ plugins: layouts(), dependencies: [], filename: p })
.process(html)
.then((res) => {
t.truthy(res.dependencies)
t.regex(res.dependencies[0].file, /layout\.html/)
t.regex(res.dependencies[0].parent, /basic\.html/)
t.truthy(cleanHtml(res.output()) === '<div class="container"><p>hello!</p></div>')
})
})
Expand Down
Loading

0 comments on commit edf7714

Please sign in to comment.