-
Notifications
You must be signed in to change notification settings - Fork 411
/
styleLoader.js
84 lines (68 loc) · 2.36 KB
/
styleLoader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
'use strict'
const path = require('path')
const fsp = require('fs-promise')
const loaderUtils = require('loader-utils')
const resolver = require('@sanity/resolver')
const promiseProps = require('promise-props-recursive')
const rewriteCss = require('./rewriteCss')
const getPath = item => item.srcPath || item.path
function sanityStyleLoader(input) {
const callback = this.async()
const query = loaderUtils.parseQuery(this.query)
if (!query.basePath) {
callback(new Error('`basePath` property must be passed to style loader'))
return
}
if (!query.style) {
callback(new Error('`style` property must be passed to style loader'))
return
}
this.addDependency(path.join(query.basePath, 'sanity.json'))
resolver
.resolveRoles({basePath: query.basePath})
.then(roles => {
if (this.cacheable) {
this.cacheable()
}
// Also add plugin manifests as dependencies
roles.plugins.forEach(plugin => {
this.addDependency(path.join(plugin.path, 'sanity.json'))
})
const roleName = `style:${query.style}`
// Do we have this role defined?
const fulfillers = roles.fulfilled[roleName]
if (!fulfillers || fulfillers.length === 0) {
callback(new Error(
`No plugins have fulfilled the "${roleName}" role`
))
return
}
// Add CSS files as dependencies
fulfillers.forEach(fulfiller => {
this.addDependency(getPath(fulfiller))
})
// Build CSS header
const css = `/**\n * Role: "${roleName}"\n */\n\n`
// Load all CSS files
Promise.all(fulfillers.map(
fulfiller => promiseProps({
css: fsp.readFile(getPath(fulfiller), {encoding: 'utf8'}),
path: getPath(fulfiller),
plugin: fulfiller.plugin,
relativeTo: __dirname,
role: roleName
}).then(rewriteCss)
)).then(files => {
// Reverse order here, because we want items sorted in the
// same order as they're defined in the Sanity manifest
setImmediate(callback, null, files.reduceRight(reduceCss, css))
}).catch(err => setImmediate(callback, err))
})
.catch(err => setImmediate(callback, err))
}
function reduceCss(currentCss, fulfiller) {
let css = `/* Plugin: ${fulfiller.plugin} */\n`
css += fulfiller.css
return `${currentCss}${css}`
}
module.exports = sanityStyleLoader