-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
52 lines (39 loc) · 1.3 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
42
43
44
45
46
47
48
49
50
51
52
var loaderUtils = require('loader-utils')
var fs = require('fs')
var path = require('path')
var Vulcanize = require('vulcanize')
var domModuleRegex = /<dom-module.*?id=["'](.*?)["'].*?>/
module.exports = function (content) {
this.cacheable && this.cacheable()
var url = loaderUtils.interpolateName(this, '[path][name].[ext]', {
context: this.options.context
})
var publicPath = this.options.output.publicPath
var meta = { url: path.join(publicPath, url) }
var result = content.match(domModuleRegex)
var emitFile = this.query.emitFile
if (Array.isArray(result) && result[1]) {
meta.tagName = result[1]
if (!emitFile) return 'module.exports = ' + (JSON.stringify(meta))
meta.url = path.join(publicPath, result[1] + '.html')
var callback = this.async()
var _this = this
bundler(url, function(inlinedHtml) {
_this.emitFile(result[1] + '.html', inlinedHtml)
callback(null, 'module.exports = ' + (JSON.stringify(meta)))
})
} else {
throw new Error('This html module dont have any module id')
}
}
// ===== vulcanize =====
function bundler (target, callback) {
new Vulcanize({
inlineScripts: true,
inlineCss: true,
stripComments: true
}).process(target, function (err, inlinedHtml) {
if (err) throw err
callback(inlinedHtml)
})
}