Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix mock context issue #40

Merged
merged 2 commits into from
Nov 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ contributing.md
.editorconfig
.travis.yml
.babelrc
yarn.lock
39 changes: 29 additions & 10 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const node = require('when/node')
const rest = require('rest')
const fs = require('fs')
const path = require('path')
const mkdirp = require('mkdirp')
const reshape = require('reshape')
const loader = require('reshape-loader')

Expand All @@ -17,24 +16,45 @@ module.exports = class Records {
apply (compiler) {
compiler.plugin('run', run.bind(this, compiler))
compiler.plugin('watch-run', run.bind(this, compiler))

compiler.plugin('compilation', (compilation) => {
compilation.plugin('normal-module-loader', (loaderContext) => {
this.loaderContext = loaderContext
})
})

compiler.plugin('emit', (compilation, done) => {
keys.map(this._locals, writeTemplates.bind(this, compilation, compiler))
.done(() => { done() }, done)
})
}
}

function run (compiler, compilation, done) {
const tasks = {}
const templates = []

for (const k in this.opts) {
if (this.opts[k].data) { tasks[k] = renderData(this.opts[k]) }
if (this.opts[k].url) { tasks[k] = renderUrl(this.opts[k]) }
if (this.opts[k].file) {
tasks[k] = renderFile(compiler.options.context, this.opts[k])
}
if (this.opts[k].template && Object.keys(this.opts[k].template).length) {
templates.push(this.opts[k].template.path)
}
}

// templates need to be ignored as they often contain extra variables
templates.map((t) => {
const ignorePath = path.join(compiler.options.context, t)
compiler.options.spike.ignore.push(ignorePath)
})

keys.all(tasks)
.then((tasks) => keys.map(tasks, transformData.bind(this)))
.tap(mergeIntoLocals.bind(this))
.then((locals) => keys.map(locals, writeTemplates.bind(this, compiler)))
.then((locals) => { this._locals = locals })
.done(() => { done() }, done)
}

Expand All @@ -60,10 +80,9 @@ function mergeIntoLocals (data) {
this.opts.addDataTo = Object.assign(this.opts.addDataTo, data)
}

function writeTemplates (compiler, _data, k) {
function writeTemplates (compilation, compiler, _data, k) {
const tpl = this.opts[k].template
const root = compiler.options.context
const publicPath = compiler.options.output.path

if (!tpl) { return _data }
if (!tpl.path) { throw 'missing template.path' } // eslint-disable-line
Expand All @@ -75,21 +94,21 @@ function writeTemplates (compiler, _data, k) {
return node.call(fs.readFile.bind(fs), path.join(root, tpl.path), 'utf8')
.then((template) => {
return W.map(data, (item) => {
this.opts.addDataTo = Object.assign(this.opts.addDataTo, {
Object.assign(this.opts.addDataTo, {
item: item,
filename: path.join(root, tpl.path)
})

const outPath = path.join(publicPath, tpl.output(item))
const mockContext = { resourcePath: outPath, addDependency: (x) => x }
const options = loader.parseOptions.call(mockContext, compiler.options.reshape, {})
const options = loader.parseOptions.call(this.loaderContext, compiler.options.reshape, {})

return reshape(options)
.process(template)
.then(((locals, res) => {
const rendered = res.output(locals)
mkdirp.sync(path.dirname(outPath))
return node.call(fs.writeFile.bind(fs), outPath, rendered)
compilation.assets[tpl.output(item)] = {
source: () => rendered,
size: () => rendered.length
}
}).bind(null, Object.assign({}, options.locals)))
})
})
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@
},
"bugs": "https://github.com/static-dev/spike-records/issues",
"dependencies": {
"mkdirp": "^0.5.1",
"reshape": "^0.3.0",
"reshape-loader": "^0.2.2",
"reshape-loader": "^0.3.0",
"rest": "^2.0.0",
"when": "^3.7.7"
},
"devDependencies": {
"ava": "^0.17.0",
"coveralls": "^2.11.12",
"coveralls": "^2.11.15",
"nyc": "^9.0.1",
"posthtml-exp": "^0.9.0",
"rimraf": "^2.5.4",
Expand Down
6 changes: 4 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ test.cb('single template errors with no "path" param', (t) => {
posts: {
data: [{ title: 'wow' }, { title: 'amaze' }],
template: {}
}, locals
},
locals
})

project.on('warning', t.end)
Expand All @@ -101,7 +102,8 @@ test.cb('single template errors with no "output" param', (t) => {
posts: {
data: [{ title: 'wow' }, { title: 'amaze' }],
template: { path: 'foo' }
}, locals
},
locals
})

project.on('warning', t.end)
Expand Down
Loading