Browse Source

views write to json file (vite plugin)

master
Marek Piasecki 2 years ago
parent
commit
3452fa6cc5
  1. 17
      bin/pro-router.imba
  2. 23
      vite.config.js

17
bin/pro-router.imba

@ -47,6 +47,19 @@ def generateRouterFile()
catch
return status: false, message: 'unable to copy router.imba template'
}
def initializeViewsFile()
const path = 'src/views.json'
const viewsFile = file path
return {
desc: "Initializing views list file - src/views.json"
run: do
if nfs.existsSync(path)
return status: true, message: ["file already exists"]
else
nfs.writeFileSync viewsFile, "[]"
return status: true, message: ["initialized successfuly"]
}
def importRouter
const indexFile = file 'src/index.js'
@ -60,7 +73,7 @@ def importRouter
return status: true, message: "import definition already there"
else
try
nfs.writeFileSync indexFile, "import './router.imba'\n" + indexContent + "\nR.url_changed() // the best if you move it right before imba.mount"
nfs.writeFileSync indexFile, "import './router.imba'\n" + indexContent
return status: true, message: "definition added successfuly"
catch
return status: false, message: "failed during writing to file"
@ -69,6 +82,6 @@ def importRouter
}
if process.argv.slice(2) == "init"
run([addProRouterViewsPlugin(), generateRouterFile(), importRouter()])
run([addProRouterViewsPlugin(), generateRouterFile(), initializeViewsFile(), importRouter()])
else
C("For initial configuration of pro router use command: pro-router init")

23
vite.config.js

@ -1,8 +1,13 @@
import np from 'path'
import nfs from 'fs'
import { imba } from 'vite-plugin-imba';
import { resolve } from 'path'
import { defineConfig } from 'vite';
import { name } from './package.json'
function file() { return np.join(__dirname, './src/views.json') }
let viewsList = []
let viewPaths = {} // view: [path]
let pathViews = {} // path: [view]
function duplicate(obj){ return JSON.parse(JSON.stringify(obj)) }
@ -19,6 +24,7 @@ function proRouterViews() {
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 = ""
@ -36,23 +42,28 @@ function proRouterViews() {
paths.push(id)
}
}
if(paths[0] == id){
views_str += "Views.push(\""+ view +"\");"
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)){
views_str += "Views.push(\""+ view +"\");"
} else {
if(!((paths[0] == id) && (paths.length > 1))){
console.log(2)
paths = without(id, paths)
views = without(view, views)
viewsList = without(view, viewsList)
modified = true
}
})
if(modified)
nfs.writeFileSync(file(), JSON.stringify(viewsList))
return { code: views_str+src, map: null }
return { moduleSideEffects: modified }
}
}
}

Loading…
Cancel
Save