From 1420829d0b3fb1cbc509e961c128c30088df682d Mon Sep 17 00:00:00 2001 From: Marek Piasecki Date: Thu, 19 Jan 2023 12:49:40 +0100 Subject: [PATCH] extract vite-plugin from config file --- src/vite-plugin.js | 66 +++++++++++++++++++++++++++++++++++++++++++ vite.config.js | 70 ++-------------------------------------------- 2 files changed, 68 insertions(+), 68 deletions(-) create mode 100644 src/vite-plugin.js diff --git a/src/vite-plugin.js b/src/vite-plugin.js new file mode 100644 index 0000000..0f3e262 --- /dev/null +++ b/src/vite-plugin.js @@ -0,0 +1,66 @@ +import np from 'path' +import nfs from 'fs' + +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)) } +function without(el, arr) { return arr.filter(function(e){ if(e != el) { return e } }) } + +export function proRouterViews() { + return { + name: 'pro-router-views', + transformIndexHtml(html) { + return html.replace('', '') + }, + + 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 } + } + } + } + } +} \ No newline at end of file diff --git a/vite.config.js b/vite.config.js index ee2d83d..3087980 100644 --- a/vite.config.js +++ b/vite.config.js @@ -1,75 +1,9 @@ -import np from 'path' -import nfs from 'fs' import { imba } from 'vite-plugin-imba'; +import { proRouterViews } from './src/vite-plugin' 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)) } -function without(el, arr) { return arr.filter(function(e){ if(e != el) { return e } }) } - -function proRouterViews() { - return { - name: 'pro-router-views', - transformIndexHtml(html) { - return html.replace('', '') - }, - - 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 } - } - } - } - } -} - - export default defineConfig({ plugins: [proRouterViews(), imba()], build: { @@ -79,4 +13,4 @@ export default defineConfig({ entry: resolve(__dirname, 'src/main.imba'), }, }, -}); +}); \ No newline at end of file