Skip to content

Commit

Permalink
working with spike-next
Browse files Browse the repository at this point in the history
  • Loading branch information
jescalan committed Feb 27, 2017
1 parent ed23668 commit 87af5c5
Show file tree
Hide file tree
Showing 7 changed files with 920 additions and 834 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ new Contentful({
name: 'press',
id: '4Em9bQeIQxxxxxxxxx'
// No JSON output needed for this content type
}
}
],
// Save all content types data in one file
json: 'alldata.json'
Expand Down
75 changes: 45 additions & 30 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,38 @@ const path = require('path')
const node = require('when/node')
const reshape = require('reshape')
const loader = require('reshape-loader')

const SpikeUtil = require('spike-util')

// This plugin works in almost exactly the same way as spike-records, but has
// been customized specifically for contentful. For a more thoroughly annotated
// source covering the remote fetch, template writing, etc, check out the source
// for spike-records. Many of the annotations would be exactly the same, so I'm
// trying to avoid extra repetition.
// https://github.com/static-dev/spike-records
//
// Anything that is not duplicated in the spike-records source will be clearly
// explained and annotated here!
class Contentful {
constructor (opts) {
const validatedOptions = validate(opts)
Object.assign(this, validatedOptions)
Object.assign(this, validate(opts))
// initialize the contentful api client
this.client = contentful.createClient({
accessToken: this.accessToken,
space: this.spaceId
})
}

apply (compiler) {
compiler.plugin('run', this.run.bind(this, compiler))
compiler.plugin('watch-run', this.run.bind(this, compiler))
this.util = new SpikeUtil(compiler.options)

this.util.runAll(compiler, this.run.bind(this, compiler))

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

compiler.plugin('emit', (compilation, done) => {
if (this.json) {
writeJson(compilation, this.json, this.addDataTo.contentful)
Expand All @@ -33,8 +51,8 @@ class Contentful {
return ct.template
})

W.map(templateContent, (ct) => {
return writeTemplate(ct, compiler, compilation, this.addDataTo, done)
W.map(templateContent, (contentType) => {
return writeTemplate.call(this, compiler, compilation, contentType)
}).done(() => done(), done)
})
}
Expand Down Expand Up @@ -157,30 +175,27 @@ function writeJson (compilation, filename, data) {
}
}

function writeTemplate (ct, compiler, compilation, addDataTo, cb) {
const data = addDataTo.contentful[ct.name]
const filePath = path.join(compiler.options.context, ct.template.path)

return node.call(fs.readFile.bind(fs), filePath, 'utf8')
.then((template) => {
return data.map((item) => {
addDataTo = Object.assign(addDataTo, { item: item })

const mockContext = { resourcePath: filePath, addDependency: (x) => x }
const options = loader.parseOptions.call(mockContext, compiler.options.reshape, {})

return reshape(options)
.process(template)
.then(((locals, res) => {
const html = res.output(locals)
compilation.assets[ct.template.output(item)] = {
source: () => html,
size: () => html.length
}
}).bind(null, Object.assign({}, options.locals)))
.catch(cb)
})
function writeTemplate (compiler, compilation, contentType) {
const data = this.addDataTo.contentful[contentType.name]
const filePath = path.join(compiler.options.context, contentType.template.path)

return node.call(fs.readFile.bind(fs), filePath, 'utf8').then((template) => {
return W.map(data, (item) => {
const newLocals = Object.assign({}, this.addDataTo, { item })

const options = loader.parseOptions.call(this.loaderContext, this.util.getSpikeOptions().reshape, {})

return reshape(options)
.process(template)
.then((res) => {
const html = res.output(newLocals)
compilation.assets[contentType.template.output(item)] = {
source: () => html,
size: () => html.length
}
})
})
})
}

module.exports = Contentful
Expand Down
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@
},
"bugs": "https://github.com/static-dev/spike-contentful/issues",
"dependencies": {
"contentful": "3.8.1",
"contentful": "^3.8.1",
"reshape": "^0.4.1",
"reshape-loader": "^0.4.2",
"when": "^3.7.7"
"reshape-loader": "next",
"when": "^3.7.8"
},
"devDependencies": {
"ava": "^0.18.0",
"coveralls": "^2.11.12",
"ava": "^0.18.2",
"coveralls": "^2.11.16",
"dotenv": "^4.0.0",
"nyc": "^10.1.2",
"reshape-standard": "^0.5.0",
"rimraf": "^2.5.4",
"spike-core": "^0.13.3",
"reshape-standard": "next",
"rimraf": "^2.6.0",
"spike-core": "next",
"standard": "^8.0.0"
},
"engines": {
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/default/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const locals = {}

module.exports = {
matchers: { html: '*(**/)*.sgr' },
reshape: (ctx) => standard({ webpack: ctx, locals }),
reshape: standard({ locals }),
plugins: [new Contentful({
accessToken: process.env.accessToken,
spaceId: process.env.spaceId,
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/json/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const locals = {}

module.exports = {
matchers: { html: '*(**/)*.sgr' },
reshape: (ctx) => htmlStandards({ webpack: ctx, locals }),
reshape: htmlStandards({ locals }),
plugins: [new Contentful({
accessToken: process.env.accessToken,
spaceId: process.env.spaceId,
Expand Down
4 changes: 2 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ test.cb('accepts template object and generates html', (t) => {
const project = new Spike({
root: projectPath,
matchers: { html: '**/*.sgr' },
reshape: (ctx) => standard({ webpack: ctx, locals }),
reshape: standard({ locals }),
entry: { main: [path.join(projectPath, 'main.js')] },
plugins: [contentful]
})
Expand Down Expand Up @@ -293,7 +293,7 @@ test.cb('generates error if template has an error', (t) => {
const project = new Spike({
root: projectPath,
matchers: { html: '**/*.sgr' },
reshape: (ctx) => standard({ webpack: ctx, locals }),
reshape: standard({ locals }),
entry: { main: [path.join(projectPath, 'main.js')] },
plugins: [contentful]
})
Expand Down
Loading

0 comments on commit 87af5c5

Please sign in to comment.