-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.js
32 lines (29 loc) · 940 Bytes
/
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
const through = require('through')
const { transformSync } = require('@babel/core')
const pluginCJS = require('@babel/plugin-transform-modules-commonjs')
const { compile } = require('@riotjs/compiler')
module.exports = function riotify(file, o) {
const opts = o || {}
const ext = opts.ext || 'riot'
const enableSourceMap = Boolean(opts._flags.debug)
const content = []
return !file.match(`.${ext}$`) ? through() : through(
chunk => content.push(chunk.toString()),
function() {
try {
const result = compile(content.join(''), {...opts, file})
const { code } = transformSync(result.code, {
inputSourceMap: result.map,
sourceMaps: enableSourceMap ? 'inline' : false,
babelrc: false,
filename: file,
plugins: [pluginCJS]
})
this.queue(code)
this.emit('end')
} catch (e) {
this.emit('error', e)
}
}
)
}