From 3452fa6cc58ea203661745abf38f64cfad35e8d1 Mon Sep 17 00:00:00 2001 From: Marek Piasecki Date: Tue, 17 Jan 2023 21:13:37 +0100 Subject: [PATCH] views write to json file (vite plugin) --- bin/pro-router.imba | 17 +++++++++++++++-- vite.config.js | 23 +++++++++++++++++------ 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/bin/pro-router.imba b/bin/pro-router.imba index 71ebe2a..c050fa3 100644 --- a/bin/pro-router.imba +++ b/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") \ No newline at end of file diff --git a/vite.config.js b/vite.config.js index b4bad16..b8c311a 100644 --- a/vite.config.js +++ b/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 } } } }