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 catch
return status: false, message: 'unable to copy router.imba template' 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 def importRouter
const indexFile = file 'src/index.js' const indexFile = file 'src/index.js'
@ -60,7 +73,7 @@ def importRouter
return status: true, message: "import definition already there" return status: true, message: "import definition already there"
else else
try 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" return status: true, message: "definition added successfuly"
catch catch
return status: false, message: "failed during writing to file" return status: false, message: "failed during writing to file"
@ -69,6 +82,6 @@ def importRouter
} }
if process.argv.slice(2) == "init" if process.argv.slice(2) == "init"
run([addProRouterViewsPlugin(), generateRouterFile(), importRouter()]) run([addProRouterViewsPlugin(), generateRouterFile(), initializeViewsFile(), importRouter()])
else else
C("For initial configuration of pro router use command: pro-router init") 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 { imba } from 'vite-plugin-imba';
import { resolve } from 'path' import { resolve } from 'path'
import { defineConfig } from 'vite'; import { defineConfig } from 'vite';
import { name } from './package.json' import { name } from './package.json'
function file() { return np.join(__dirname, './src/views.json') }
let viewsList = []
let viewPaths = {} // view: [path] let viewPaths = {} // view: [path]
let pathViews = {} // path: [view] let pathViews = {} // path: [view]
function duplicate(obj){ return JSON.parse(JSON.stringify(obj)) } function duplicate(obj){ return JSON.parse(JSON.stringify(obj)) }
@ -19,6 +24,7 @@ function proRouterViews() {
order: 'pre', order: 'pre',
handler(src, id) { handler(src, id) {
if (/\.imba$/.test(id)) { if (/\.imba$/.test(id)) {
let modified = false
let viewRegExp = /tag view-((\w|\S)+)( |\n)/g let viewRegExp = /tag view-((\w|\S)+)( |\n)/g
let result = null let result = null
let views_str = "" let views_str = ""
@ -36,23 +42,28 @@ function proRouterViews() {
paths.push(id) paths.push(id)
} }
} }
if(paths[0] == id){ if((paths[0] == id) && !viewsList.includes(view)){
views_str += "Views.push(\""+ view +"\");" viewsList.push(view)
modified = true
} }
} }
} }
missingDeclarations.forEach(function(view){ missingDeclarations.forEach(function(view){
let paths = viewPaths[view] ||= [] let paths = viewPaths[view] ||= []
if((paths[0] == id) && (paths.length > 1)){ if(!((paths[0] == id) && (paths.length > 1))){
views_str += "Views.push(\""+ view +"\");" console.log(2)
} else {
paths = without(id, paths) paths = without(id, paths)
views = without(view, views) views = without(view, views)
viewsList = without(view, viewsList)
modified = true
} }
}) })
return { code: views_str+src, map: null } if(modified)
nfs.writeFileSync(file(), JSON.stringify(viewsList))
return { moduleSideEffects: modified }
} }
} }
} }

Loading…
Cancel
Save