-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
41 lines (35 loc) · 1.05 KB
/
index.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
var path = require('path')
module.exports = function (source, testOpts) {
var STYLE = /<style(\s+[^>]*)?>\n?([^<]*(?:<(?!\/style\s*>)[^<]*)*)<\/style\s*>/gi // from riot/compiler
var PREPROCESSOR = /\stype="([a-z/]*)"/
var opts = testOpts || {
mode: this.query.substring(1),
filename: path.basename(this.resourcePath)
}
var unstyledTag
var loader = ''
var styles = []
if (this.cacheable) this.cacheable()
unstyledTag = source.replace(STYLE, function (_, attrs, style) {
// style mode
if (opts.mode === 'style') {
styles.push(style)
}
// normal mode
if (attrs && PREPROCESSOR.test(attrs)) {
loader = '!' + attrs.match(PREPROCESSOR)[1].replace('text/', '')
}
return ''
})
// style mode
if (opts.mode === 'style') {
return styles.join('\n')
}
// strip mode
if (opts.mode === 'strip') {
return unstyledTag
}
// normal mode
var requireStr = 'require("!!style!css' + loader + '!riotjs-style-plus-loader?style!./' + opts.filename + '")'
return [requireStr, unstyledTag].join('\n')
}