@lomray/vite-ssr-boost 1.2.0 → 1.2.1-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/helpers/is-route-file.js +1 -1
- package/helpers/is-route-file.js.map +1 -1
- package/package.json +3 -3
- package/plugin.d.ts +1 -0
- package/plugin.js +1 -1
- package/plugin.js.map +1 -1
- package/plugins/normalize-route.d.ts +2 -0
- package/plugins/normalize-route.js +1 -1
- package/plugins/normalize-route.js.map +1 -1
package/README.md
CHANGED
|
@@ -237,7 +237,7 @@ ssr-boost -h
|
|
|
237
237
|
Route imports of the following types are supported:
|
|
238
238
|
```typescript jsx
|
|
239
239
|
import { RouteObject } from 'react-router-dom';
|
|
240
|
-
import HomePage from './pages/home';
|
|
240
|
+
import HomePage from './pages/home'; // not lazy imports should be directly in file where it use
|
|
241
241
|
|
|
242
242
|
const importPath = './pages/home';
|
|
243
243
|
|
package/helpers/is-route-file.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const t=t
|
|
1
|
+
const t=t=>/{.*path:.*(lazy:.+import|Component:|element:)|{.*(lazy:.+import|Component:|element:).*path:/s.test(t);export{t as default};
|
|
2
2
|
//# sourceMappingURL=is-route-file.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"is-route-file.js","sources":["../../src/helpers/is-route-file.ts"],"sourcesContent":["/**\n * Detect route file\n */\nconst isRoutesFile = (code: string): boolean
|
|
1
|
+
{"version":3,"file":"is-route-file.js","sources":["../../src/helpers/is-route-file.ts"],"sourcesContent":["/**\n * Detect route file\n */\nconst isRoutesFile = (code: string): boolean =>\n /{.*path:.*(lazy:.+import|Component:|element:)|{.*(lazy:.+import|Component:|element:).*path:/s.test(\n code,\n );\n\nexport default isRoutesFile;\n"],"names":["isRoutesFile","code","test"],"mappings":"AAGA,MAAMA,EAAgBC,GACpB,+FAA+FC,KAC7FD"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lomray/vite-ssr-boost",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.1-beta.2",
|
|
4
4
|
"description": "Vite plugin for create awesome SSR or SPA applications on React.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"keywords": [
|
|
@@ -83,10 +83,10 @@
|
|
|
83
83
|
"typescript": "^5.3.2"
|
|
84
84
|
},
|
|
85
85
|
"peerDependencies": {
|
|
86
|
-
"@types/express": "
|
|
86
|
+
"@types/express": ">=4.17.21",
|
|
87
87
|
"react-dom": ">=18.2.0",
|
|
88
88
|
"react-router-dom": ">=6.12.1",
|
|
89
|
-
"vite": "
|
|
89
|
+
"vite": ">=4.4.6 <= 5"
|
|
90
90
|
},
|
|
91
91
|
"bin": {
|
|
92
92
|
"ssr-boost": "cli.js"
|
package/plugin.d.ts
CHANGED
package/plugin.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import e from"node:path";import i from"./constants/cli-actions.js";import s from"./constants/plugin-name.js";import
|
|
1
|
+
import e from"node:path";import i from"./constants/cli-actions.js";import s from"./constants/plugin-name.js";import t from"./plugins/make-aliases.js";import o from"./plugins/normalize-route.js";const n={indexFile:"index.html",serverFile:"server.ts",clientFile:"client.ts",tsconfigAliases:!0};function r(r={}){const l=new URL(import.meta.url),a=global.viteBoostAction||process.env.SSR_BOOST_ACTION,p={...n,...r},u="1"===process.env.SSR_BOOST_IS_SSR||a===i.dev,m=a===i.build,c=[{name:s,enforce:"pre",pluginOptions:{...p,pluginPath:e.dirname(l.pathname),action:a,isDev:a===i.dev},config:(e,{ssrBuild:i})=>(e.define={...e.define??{},__IS_SSR__:u},e.build={...e.build??{}},i?{...e,...m?{appType:"custom"}:{},publicDir:!1}:(u&&m&&(e.build.manifest=!0),e))}],{tsconfigAliases:d,routesPath:f}=p;return d&&c.push(t("boolean"==typeof d?void 0:d)),c.push(o({isSSR:u,isBuild:m,routesPath:f})),c}export{r as default};
|
|
2
2
|
//# sourceMappingURL=plugin.js.map
|
package/plugin.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.js","sources":["../src/plugin.ts"],"sourcesContent":["import path from 'node:path';\nimport type { Plugin } from 'vite';\nimport CliActions from '@constants/cli-actions';\nimport type { ICliContext } from '@constants/cli-context';\nimport PLUGIN_NAME from '@constants/plugin-name';\nimport type { IPluginOptions as IMakeAliasesPluginOptions } from '@plugins/make-aliases';\nimport ViteMakeAliasesPlugin from '@plugins/make-aliases';\nimport ViteNormalizeRouterPlugin from '@plugins/normalize-route';\n\nexport interface IPluginOptions {\n
|
|
1
|
+
{"version":3,"file":"plugin.js","sources":["../src/plugin.ts"],"sourcesContent":["import path from 'node:path';\nimport type { Plugin } from 'vite';\nimport CliActions from '@constants/cli-actions';\nimport type { ICliContext } from '@constants/cli-context';\nimport PLUGIN_NAME from '@constants/plugin-name';\nimport type { IPluginOptions as IMakeAliasesPluginOptions } from '@plugins/make-aliases';\nimport ViteMakeAliasesPlugin from '@plugins/make-aliases';\nimport ViteNormalizeRouterPlugin from '@plugins/normalize-route';\n\nexport interface IPluginOptions {\n // default: index.html\n indexFile?: string;\n // default: server.ts\n serverFile?: string;\n // default: client.ts\n clientFile?: string;\n // Path contains routes declaration files (need to detect route files). default: undefined, e.g.: /routes/\n routesPath?: string;\n // Read aliases from tsconfig\n tsconfigAliases?: boolean | IMakeAliasesPluginOptions;\n customShortcuts?: {\n key: string;\n description: string;\n action: (cliContext: ICliContext) => Promise<void> | void;\n isOnlyDev?: boolean;\n }[];\n}\n\nconst defaultOptions: IPluginOptions = {\n indexFile: 'index.html',\n serverFile: 'server.ts',\n clientFile: 'client.ts',\n tsconfigAliases: true,\n};\n\n/**\n * Init plugin\n * @constructor\n */\nfunction ViteSsrBoostPlugin(options: IPluginOptions = {}): Plugin[] {\n const dirInfo = new URL(import.meta.url);\n const action = (global.viteBoostAction || process.env.SSR_BOOST_ACTION) as CliActions;\n const mergedOptions: IPluginOptions = { ...defaultOptions, ...options };\n const isSSR = process.env.SSR_BOOST_IS_SSR === '1' || action === CliActions.dev;\n const isBuild = action === CliActions.build;\n\n const plugins: Plugin[] = [\n {\n name: PLUGIN_NAME,\n enforce: 'pre',\n // @ts-ignore save custom options\n pluginOptions: {\n ...mergedOptions,\n pluginPath: path.dirname(dirInfo.pathname),\n action,\n isDev: action === CliActions.dev,\n },\n\n config(config, { ssrBuild }) {\n config.define = {\n ...(config.define ?? {}),\n __IS_SSR__: isSSR,\n };\n\n config.build = {\n ...(config.build ?? {}),\n };\n\n if (!ssrBuild) {\n if (isSSR && isBuild) {\n config.build!.manifest = true;\n }\n\n return config;\n }\n\n return {\n ...config,\n ...(isBuild ? { appType: 'custom' } : {}),\n publicDir: false,\n };\n },\n },\n ];\n\n const { tsconfigAliases, routesPath } = mergedOptions;\n\n if (tsconfigAliases) {\n plugins.push(\n ViteMakeAliasesPlugin(typeof tsconfigAliases === 'boolean' ? undefined : tsconfigAliases),\n );\n }\n\n plugins.push(ViteNormalizeRouterPlugin({ isSSR, isBuild, routesPath }));\n\n return plugins;\n}\n\nexport default ViteSsrBoostPlugin;\n"],"names":["defaultOptions","indexFile","serverFile","clientFile","tsconfigAliases","ViteSsrBoostPlugin","options","dirInfo","URL","url","action","global","viteBoostAction","process","env","SSR_BOOST_ACTION","mergedOptions","isSSR","SSR_BOOST_IS_SSR","CliActions","dev","isBuild","build","plugins","name","PLUGIN_NAME","enforce","pluginOptions","pluginPath","path","dirname","pathname","isDev","config","ssrBuild","define","__IS_SSR__","appType","publicDir","manifest","routesPath","push","ViteMakeAliasesPlugin","undefined","ViteNormalizeRouterPlugin"],"mappings":"kMA4BA,MAAMA,EAAiC,CACrCC,UAAW,aACXC,WAAY,YACZC,WAAY,YACZC,iBAAiB,GAOnB,SAASC,EAAmBC,EAA0B,IACpD,MAAMC,EAAU,IAAIC,gBAAgBC,KAC9BC,EAAUC,OAAOC,iBAAmBC,QAAQC,IAAIC,iBAChDC,EAAgC,IAAKhB,KAAmBM,GACxDW,EAAyC,MAAjCJ,QAAQC,IAAII,kBAA4BR,IAAWS,EAAWC,IACtEC,EAAUX,IAAWS,EAAWG,MAEhCC,EAAoB,CACxB,CACEC,KAAMC,EACNC,QAAS,MAETC,cAAe,IACVX,EACHY,WAAYC,EAAKC,QAAQvB,EAAQwB,UACjCrB,SACAsB,MAAOtB,IAAWS,EAAWC,KAG/Ba,OAAM,CAACA,GAAQC,SAAEA,MACfD,EAAOE,OAAS,IACVF,EAAOE,QAAU,GACrBC,WAAYnB,GAGdgB,EAAOX,MAAQ,IACTW,EAAOX,OAAS,IAGjBY,EAQE,IACFD,KACCZ,EAAU,CAAEgB,QAAS,UAAa,CAAA,EACtCC,WAAW,IAVPrB,GAASI,IACXY,EAAOX,MAAOiB,UAAW,GAGpBN,OAYT7B,gBAAEA,EAAeoC,WAAEA,GAAexB,EAUxC,OARIZ,GACFmB,EAAQkB,KACNC,EAAiD,kBAApBtC,OAAgCuC,EAAYvC,IAI7EmB,EAAQkB,KAAKG,EAA0B,CAAE3B,QAAOI,UAASmB,gBAElDjB,CACT"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{extname as
|
|
1
|
+
import{extname as t}from"node:path";import e from"../constants/plugin-name.js";import s from"../helpers/is-route-file.js";const r=(t,e=!1)=>{if(e)return t;const s=[...t.matchAll(/import\s+(\w+)\sfrom\s+['"]([^'"]+)['"]/g)].reduce(((t,[,e,s])=>({[e]:s,...t})),{});return t.replace(/(.*path:.*(?:Component|element):\s*(\w+)),*(.+)/s,((t,e,r,n)=>{const o=s?.[r];return o?`${e},\npathId: '${o}',${n}`:e+n}))},n=(t,s)=>`import n from '${e}/helpers/import-route';${t}`.replace(/(lazy)(:\s*)(\(\)\s*=>\s*import\(([^)]+)\))/gs,s?"lazy$2()=>n($3,$4)":"lazy$2()=>n($3)");function o(o={}){const{isSSR:m=!1,isBuild:i=!1,routesPath:p}=o;return{name:`${e}-normalize-route`,transform:(e,o)=>{const l=t(o).split("?")[0],a=!p||o.includes(p);if(!o.includes("node_modules")&&[".js",".mjs",".ts",".tsx"].includes(l)&&a&&s(e))return{code:n(r(e,i),m),map:{mappings:""}}}}}export{o as default};
|
|
2
2
|
//# sourceMappingURL=normalize-route.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"normalize-route.js","sources":["../../src/plugins/normalize-route.ts"],"sourcesContent":["import { extname } from 'node:path';\nimport type { Plugin } from 'vite';\nimport PLUGIN_NAME from '@constants/plugin-name';\nimport isRoutesFile from '@helpers/is-route-file';\n\nexport interface IPluginOptions {\n isSSR?: boolean;\n}\n\n/**\n * Add normalize wrapper to lazy imports for client build\n */\nconst
|
|
1
|
+
{"version":3,"file":"normalize-route.js","sources":["../../src/plugins/normalize-route.ts"],"sourcesContent":["import { extname } from 'node:path';\nimport type { Plugin } from 'vite';\nimport PLUGIN_NAME from '@constants/plugin-name';\nimport isRoutesFile from '@helpers/is-route-file';\n\nexport interface IPluginOptions {\n isSSR?: boolean;\n isBuild?: boolean;\n routesPath?: string;\n}\n\n/**\n * Add pathId to route (where pathId - import string)\n * NOTE: only for dev mode\n */\nconst normalizeSyncRoutes = (code: string, isBuild = false): string => {\n if (isBuild) {\n return code;\n }\n\n const imports: Record<string, string> = [\n ...code.matchAll(/import\\s+(\\w+)\\sfrom\\s+['\"]([^'\"]+)['\"]/g),\n ].reduce(\n (res, [, key, value]) => ({\n [key]: value,\n ...res,\n }),\n {},\n );\n\n return code.replace(\n /(.*path:.*(?:Component|element):\\s*(\\w+)),*(.+)/s,\n (_, before, routeName: string, after: string) => {\n const importPath = imports?.[routeName];\n\n if (!importPath) {\n return before + after;\n }\n\n return `${before},\\npathId: '${importPath}',${after}`;\n },\n );\n};\n\n/**\n * Add normalize wrapper to lazy imports for client build\n */\nconst normalizeAsyncRoutes = (code: string, isSSR: boolean): string =>\n `import n from '${PLUGIN_NAME}/helpers/import-route';${code}`.replace(\n /(lazy)(:\\s*)(\\(\\)\\s*=>\\s*import\\(([^)]+)\\))/gs,\n isSSR ? 'lazy$2()=>n($3,$4)' : 'lazy$2()=>n($3)',\n );\n\n/**\n * Add possibility to export route components like FCRoute or FCCRoute\n * Add route path for generating manifest\n * USAGE: { path: '/', lazy: () => import('./pages/home') }\n * @see FCRoute\n * @see FCCRoute\n * @see SsrManifest.getRoutesIds\n * @see importRoute\n * @constructor\n */\nfunction ViteNormalizeRouterPlugin(options: IPluginOptions = {}): Plugin {\n const { isSSR = false, isBuild = false, routesPath } = options;\n\n return {\n name: `${PLUGIN_NAME}-normalize-route`,\n transform: (code, id) => {\n const extName = extname(id).split('?')[0]!;\n const isRoutesPath = !routesPath || id.includes(routesPath);\n\n if (\n id.includes('node_modules') ||\n !['.js', '.mjs', '.ts', '.tsx'].includes(extName) ||\n !isRoutesPath ||\n !isRoutesFile(code)\n ) {\n return;\n }\n\n return {\n code: normalizeAsyncRoutes(normalizeSyncRoutes(code, isBuild), isSSR),\n map: { mappings: '' },\n };\n },\n };\n}\n\nexport default ViteNormalizeRouterPlugin;\n"],"names":["normalizeSyncRoutes","code","isBuild","imports","matchAll","reduce","res","key","value","replace","_","before","routeName","after","importPath","normalizeAsyncRoutes","isSSR","PLUGIN_NAME","ViteNormalizeRouterPlugin","options","routesPath","name","transform","id","extName","extname","split","isRoutesPath","includes","isRoutesFile","map","mappings"],"mappings":"0HAeA,MAAMA,EAAsB,CAACC,EAAcC,GAAU,KACnD,GAAIA,EACF,OAAOD,EAGT,MAAME,EAAkC,IACnCF,EAAKG,SAAS,6CACjBC,QACA,CAACC,GAAK,CAAGC,EAAKC,MAAY,CACxBD,CAACA,GAAMC,KACJF,KAEL,CAAE,GAGJ,OAAOL,EAAKQ,QACV,oDACA,CAACC,EAAGC,EAAQC,EAAmBC,KAC7B,MAAMC,EAAaX,IAAUS,GAE7B,OAAKE,EAIE,GAAGH,gBAAqBG,MAAeD,IAHrCF,EAASE,CAGmC,GAExD,EAMGE,EAAuB,CAACd,EAAce,IAC1C,kBAAkBC,2BAAqChB,IAAOQ,QAC5D,gDACAO,EAAQ,qBAAuB,mBAanC,SAASE,EAA0BC,EAA0B,IAC3D,MAAMH,MAAEA,GAAQ,EAAKd,QAAEA,GAAU,EAAKkB,WAAEA,GAAeD,EAEvD,MAAO,CACLE,KAAM,GAAGJ,oBACTK,UAAW,CAACrB,EAAMsB,KAChB,MAAMC,EAAUC,EAAQF,GAAIG,MAAM,KAAK,GACjCC,GAAgBP,GAAcG,EAAGK,SAASR,GAEhD,IACEG,EAAGK,SAAS,iBACX,CAAC,MAAO,OAAQ,MAAO,QAAQA,SAASJ,IACxCG,GACAE,EAAa5B,GAKhB,MAAO,CACLA,KAAMc,EAAqBf,EAAoBC,EAAMC,GAAUc,GAC/Dc,IAAK,CAAEC,SAAU,IAClB,EAGP"}
|