|
|
|
let np = require('path')
|
|
|
|
let nfs = require('fs')
|
|
|
|
|
|
|
|
function file() { return np.resolve(process.cwd(), 'src/views.json') }
|
|
|
|
|
|
|
|
let viewsList = []
|
|
|
|
let viewPaths = {} // view: [path]
|
|
|
|
let pathViews = {} // path: [view]
|
|
|
|
function duplicate(obj){ return JSON.parse(JSON.stringify(obj)) }
|
|
|
|
function without(el, arr) { return arr.filter(function(e){ if(e != el) { return e } }) }
|
|
|
|
|
|
|
|
module.exports = function proRouterViews() {
|
|
|
|
return {
|
|
|
|
name: 'pro-router-views',
|
|
|
|
transformIndexHtml(html) {
|
|
|
|
return html.replace('<head>', '<head><script>window.Views=[];</script>')
|
|
|
|
},
|
|
|
|
|
|
|
|
transform: {
|
|
|
|
order: 'pre',
|
|
|
|
handler(src, id) {
|
|
|
|
if (/\.imba$/.test(id)) {
|
|
|
|
let modified = false
|
|
|
|
let viewRegExp = /tag view-((\w|\S)+)( |\n)/g
|
|
|
|
let result = null
|
|
|
|
let views_str = ""
|
|
|
|
let views = pathViews[id] ||= []
|
|
|
|
let missingDeclarations = duplicate(views)
|
|
|
|
|
|
|
|
while(result = viewRegExp.exec(src)) {
|
|
|
|
let view = result[1]
|
|
|
|
if(view != 'not_found'){
|
|
|
|
let paths = viewPaths[view] ||= []
|
|
|
|
missingDeclarations = without(view, missingDeclarations)
|
|
|
|
if(!views.includes(view)){
|
|
|
|
views.push(view)
|
|
|
|
if(!paths.includes(id)){
|
|
|
|
paths.push(id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if((paths[0] == id) && !viewsList.includes(view)){
|
|
|
|
viewsList.push(view)
|
|
|
|
modified = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
missingDeclarations.forEach(function(view){
|
|
|
|
let paths = viewPaths[view] ||= []
|
|
|
|
if(!((paths[0] == id) && (paths.length > 1))){
|
|
|
|
paths = without(id, paths)
|
|
|
|
views = without(view, views)
|
|
|
|
viewsList = without(view, viewsList)
|
|
|
|
modified = true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if(modified)
|
|
|
|
nfs.writeFileSync(file(), JSON.stringify(viewsList))
|
|
|
|
|
|
|
|
return { moduleSideEffects: modified }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|