Skip to content

Commit

Permalink
feat: added webcomponent react loader
Browse files Browse the repository at this point in the history
  • Loading branch information
yuluyi authored and vagusX committed Mar 27, 2017
1 parent 68ca396 commit 58a19dc
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions react/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
var loaderUtils = require('loader-utils')
var fs = require('fs')
var path = require('path')
var generateTemplate = require('./generateTemplate')
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 meta = { url: url }

var result = content.match(domModuleRegex)

var emitFile = this.query.emitFile

if (Array.isArray(result) && result[1]) {
meta.tagName = result[1]

if (!emitFile) return generateTemplate(meta)

meta.url = result[1] + '.html'

var callback = this.async()
var _this = this

bundler(url, function(inlinedHtml) {
_this.emitFile(result[1] + '.html', inlinedHtml)
callback(null, generateTemplate(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)
})
}

0 comments on commit 58a19dc

Please sign in to comment.