58 lines
1.5 KiB
JavaScript
58 lines
1.5 KiB
JavaScript
const Loader = require('./Loader')
|
|
|
|
const wrapProcessor = (preprocessor, path, handle) => async data => {
|
|
let ext = $fs.utils.getExt(path),
|
|
processed = await preprocessor(data, path).catch(console.error),
|
|
blob = new Blob([ processed ], { type: le._get.ext.mime[ext] })
|
|
|
|
handle( URL.createObjectURL(blob) )
|
|
}
|
|
|
|
const handleIframe = async (iframe, path = '/') => {
|
|
const convert = (tag, from, to, preprocessor) => {
|
|
let elements = iframe.contentDocument.querySelectorAll(tag)
|
|
|
|
for(let element of elements) {
|
|
let original = element.getAttribute(from)
|
|
|
|
if(!original)
|
|
continue
|
|
|
|
let handle = url => {
|
|
element[to] = url
|
|
}
|
|
|
|
$bundle.for(path).open(original, preprocessor ? 'String' : 'URL')
|
|
.then(preprocessor ? wrapProcessor(preprocessor, path, handle) : handle)
|
|
}
|
|
}
|
|
|
|
convert('script', 'lsrc', 'src')
|
|
convert('img', 'lsrc', 'src')
|
|
convert('link', 'lhref', 'href', async (stylesheet, path) => {
|
|
let matches = stylesheet.matchAll(/url\(("(.+?)"|'(.+?)'|(.+?))\)/g),
|
|
fromIndex = 0,
|
|
out = []
|
|
|
|
console.log(matches)
|
|
|
|
for(let match of matches) {
|
|
let bundle = $bundle.for( $fs.utils.getFolderPath(path) )
|
|
|
|
console.log(bundle.access(match[2]))
|
|
|
|
out.concat([
|
|
stylesheet.slice(fromIndex, match.index),
|
|
'url("' + await bundle.open(match[2], 'URL') + '")'
|
|
])
|
|
|
|
fromIndex = match.index + match[0].length
|
|
}
|
|
|
|
console.log(out, fromIndex)
|
|
|
|
return out.join('')
|
|
})
|
|
}
|
|
|
|
module.exports = handleIframe |