@lomray/vite-ssr-boost 1.0.0-beta.10 → 1.0.0-beta.11

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.
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Get server state on the client side
3
+ */
4
+ declare const getServerState: (name: string, shouldRemove?: boolean) => Record<string, any>;
5
+ export { getServerState as default };
@@ -0,0 +1,2 @@
1
+ const e=(e,t=!0)=>{const n=window[e];return t&&n&&delete window[e],n??{}};export{e as default};
2
+ //# sourceMappingURL=get-server-state.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"get-server-state.js","sources":["../../src/helpers/get-server-state.ts"],"sourcesContent":["/**\n * Get server state on the client side\n */\nconst getServerState = (name: string, shouldRemove = true): Record<string, any> => {\n const data = window[name] as Record<string, any>;\n\n if (shouldRemove && data) {\n delete window[name];\n }\n\n return data ?? {};\n};\n\nexport default getServerState;\n"],"names":["getServerState","name","shouldRemove","data","window"],"mappings":"AAGM,MAAAA,EAAiB,CAACC,EAAcC,GAAe,KACnD,MAAMC,EAAOC,OAAOH,GAMpB,OAJIC,GAAgBC,UACXC,OAAOH,GAGTE,GAAQ,CAAA,CAAE"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lomray/vite-ssr-boost",
3
- "version": "1.0.0-beta.10",
3
+ "version": "1.0.0-beta.11",
4
4
  "description": "Vite plugin for create awesome SSR or SPA applications on React.",
5
5
  "type": "module",
6
6
  "keywords": [
@@ -35,11 +35,9 @@ declare class PrepareServer {
35
35
  protected html: string;
36
36
  /**
37
37
  * @constructor
38
- * @protected
39
38
  */
40
39
  /**
41
40
  * @constructor
42
- * @protected
43
41
  */
44
42
  protected constructor(config: ServerConfig);
45
43
  /**
@@ -1,2 +1,2 @@
1
- import t from"fs";import e from"path";class r{config;entrypoint;onServerCreated;html;constructor(t){this.config=t}static init(t){return new r(t)}async loadEntrypoint(){if(this.entrypoint&&this.config.isProd)return this.entrypoint;const{root:t,isProd:r,serverFile:o}=this.config.getParams(),n=e.resolve(`${t}/${o}`);let i;i=r?(await import(n)).default:(await this.config.getVite().ssrLoadModule(n,{fixStacktrace:!0})).default;const{render:s,init:a}=i,{onServerCreated:l,onRequest:h,onRouterReady:c,onShellReady:d,onResponse:p,onShellError:f,onError:g,getState:u}=await(a?.({config:this.config}))??{};return this.entrypoint={render:s,onRequest:h,onRouterReady:c,onShellReady:d,onShellError:f,onResponse:p,onError:g,getState:u},this.onServerCreated=l,this.entrypoint}async loadHtml(r){const{isProd:o,root:n,indexFile:i}=this.config.getParams();this.html&&o||(this.html=t.readFileSync(e.resolve(`${n}/${i}`),"utf-8"));let s=this.html;return o||(s=(await this.config.getVite().transformIndexHtml(r.originalUrl,this.html)).replace(/(<script.+)(>[\s\S]+injectIntoGlobalHook.+)/,"$1async$2")),s.split("\x3c!--ssr-outlet--\x3e")}async onAppCreated(){return await this.loadEntrypoint(),await(this.onServerCreated?.(this.config.getApp())),this}}export{r as default};
1
+ import t from"fs";import e from"node:process";import r from"path";import o from"chalk";class i{config;entrypoint;onServerCreated;html;constructor(t){this.config=t}static init(t){return new i(t)}async loadEntrypoint(){if(this.entrypoint&&this.config.isProd)return this.entrypoint;const{root:t,isProd:i,serverFile:n}=this.config.getParams(),s=r.resolve(`${t}/${n}`);let a;try{a=i?(await import(s)).default:(await this.config.getVite().ssrLoadModule(s,{fixStacktrace:!0})).default}catch(t){if(t.message.includes("Cannot find module")&&t.message.includes("/build/"))return this.config.getLogger().error(`Before starting the server, you need to create a build: ${o.yellow("ssr-boost build")}`),e.exit(1);throw t}const{render:l,init:c}=a,{onServerCreated:h,onRequest:d,onRouterReady:f,onShellReady:g,onResponse:p,onShellError:u,onError:m,getState:y}=await(c?.({config:this.config}))??{};return this.entrypoint={render:l,onRequest:d,onRouterReady:f,onShellReady:g,onShellError:u,onResponse:p,onError:m,getState:y},this.onServerCreated=h,this.entrypoint}async loadHtml(e){const{isProd:o,root:i,indexFile:n}=this.config.getParams();this.html&&o||(this.html=t.readFileSync(r.resolve(`${i}/${n}`),"utf-8"));let s=this.html;return o||(s=(await this.config.getVite().transformIndexHtml(e.originalUrl,this.html)).replace(/(<script.+)(>[\s\S]+injectIntoGlobalHook.+)/,"$1async$2")),s.split("\x3c!--ssr-outlet--\x3e")}async onAppCreated(){return await this.loadEntrypoint(),await(this.onServerCreated?.(this.config.getApp())),this}}export{i as default};
2
2
  //# sourceMappingURL=prepare-server.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"prepare-server.js","sources":["../../src/services/prepare-server.ts"],"sourcesContent":["import fs from 'fs';\nimport path from 'path';\nimport type { Request } from 'express';\nimport type { IEntrypointOptions, IPrepareRenderOut } from '@node/entry';\nimport type { TRender } from '@node/render';\nimport type ServerConfig from '@services/server-config';\n\ninterface IPrepareServerEntrypointLoadOut<TAppProps = Record<string, any>> {\n render: TRender;\n onRequest?: IEntrypointOptions<TAppProps>['onRequest'];\n onRouterReady?: IEntrypointOptions<TAppProps>['onRouterReady'];\n onShellReady?: IEntrypointOptions<TAppProps>['onShellReady'];\n onShellError?: IEntrypointOptions<TAppProps>['onShellError'];\n onResponse?: IEntrypointOptions<TAppProps>['onResponse'];\n onError?: IEntrypointOptions<TAppProps>['onError'];\n getState?: IEntrypointOptions<TAppProps>['getState'];\n}\n\n/**\n * Load server entrypoint and template\n * DEV MODE: refresh entrypoint and template\n */\nclass PrepareServer {\n /**\n * Server configuration\n */\n protected readonly config: ServerConfig;\n\n /**\n * Entrypoint resolved params\n */\n protected entrypoint?: IPrepareServerEntrypointLoadOut;\n\n /**\n * Hook which calls after express server created\n */\n protected onServerCreated?: IEntrypointOptions['onServerCreated'];\n\n /**\n * Html shell\n */\n protected html: string;\n\n /**\n * @constructor\n * @protected\n */\n protected constructor(config: ServerConfig) {\n this.config = config;\n }\n\n /**\n * Init service\n */\n public static init(config: ServerConfig): PrepareServer {\n return new PrepareServer(config);\n }\n\n /**\n * Resolve and return entrypoint params\n */\n public async loadEntrypoint(): Promise<IPrepareServerEntrypointLoadOut> {\n // load server entrypoint each time only in development mode (for fast refresh)\n if (this.entrypoint && this.config.isProd) {\n return this.entrypoint;\n }\n\n const { root, isProd, serverFile } = this.config.getParams();\n const entrypointPath = path.resolve(`${root}/${serverFile}`);\n\n let resolvedEntrypoint: IPrepareRenderOut;\n\n if (!isProd) {\n resolvedEntrypoint = (\n await this.config.getVite()!.ssrLoadModule(entrypointPath, {\n fixStacktrace: true,\n })\n ).default;\n } else {\n resolvedEntrypoint = (await import(entrypointPath)).default;\n }\n\n const { render, init } = resolvedEntrypoint;\n const {\n onServerCreated,\n onRequest,\n onRouterReady,\n onShellReady,\n onResponse,\n onShellError,\n onError,\n getState,\n } =\n (await init?.({\n config: this.config,\n })) ?? {};\n\n this.entrypoint = {\n render,\n onRequest,\n onRouterReady,\n onShellReady,\n onShellError,\n onResponse,\n onError,\n getState,\n };\n this.onServerCreated = onServerCreated;\n\n return this.entrypoint;\n }\n\n /**\n * Load and return html shell\n */\n public async loadHtml(req: Request): Promise<[string, string]> {\n const { isProd, root, indexFile } = this.config.getParams();\n\n if (!this.html || !isProd) {\n this.html = fs.readFileSync(path.resolve(`${root}/${indexFile}`), 'utf-8');\n }\n\n let modifiedHtml = this.html;\n\n if (!isProd) {\n // Apply Vite HTML transforms. This injects the Vite HMR client,\n // and also applies HTML transforms from Vite plugins, e.g. global\n // preambles from @vitejs/plugin-react\n modifiedHtml = (await this.config.getVite()!.transformIndexHtml(req.originalUrl, this.html))\n // Make vite script 'async'\n .replace(/(<script.+)(>[\\s\\S]+injectIntoGlobalHook.+)/, '$1async$2');\n }\n\n return modifiedHtml.split('<!--ssr-outlet-->') as [string, string];\n }\n\n /**\n * Run server created hook\n */\n public async onAppCreated(): Promise<PrepareServer> {\n await this.loadEntrypoint();\n await this.onServerCreated?.(this.config.getApp()!);\n\n return this;\n }\n}\n\nexport default PrepareServer;\n"],"names":["PrepareServer","config","entrypoint","onServerCreated","html","constructor","this","static","async","isProd","root","serverFile","getParams","entrypointPath","path","resolve","resolvedEntrypoint","import","default","getVite","ssrLoadModule","fixStacktrace","render","init","onRequest","onRouterReady","onShellReady","onResponse","onShellError","onError","getState","req","indexFile","fs","readFileSync","modifiedHtml","transformIndexHtml","originalUrl","replace","split","loadEntrypoint","getApp"],"mappings":"sCAsBA,MAAMA,EAIeC,OAKTC,WAKAC,gBAKAC,KAMVC,YAAsBJ,GACpBK,KAAKL,OAASA,CACf,CAKMM,YAAYN,GACjB,OAAO,IAAID,EAAcC,EAC1B,CAKMO,uBAEL,GAAIF,KAAKJ,YAAcI,KAAKL,OAAOQ,OACjC,OAAOH,KAAKJ,WAGd,MAAMQ,KAAEA,EAAID,OAAEA,EAAME,WAAEA,GAAeL,KAAKL,OAAOW,YAC3CC,EAAiBC,EAAKC,QAAQ,GAAGL,KAAQC,KAE/C,IAAIK,EASFA,EAPGP,SAOyBQ,OAAOJ,IAAiBK,eAL5CZ,KAAKL,OAAOkB,UAAWC,cAAcP,EAAgB,CACzDQ,eAAe,KAEjBH,QAKJ,MAAMI,OAAEA,EAAMC,KAAEA,GAASP,GACnBb,gBACJA,EAAeqB,UACfA,EAASC,cACTA,EAAaC,aACbA,EAAYC,WACZA,EAAUC,aACVA,EAAYC,QACZA,EAAOC,SACPA,SAEOP,IAAO,CACZtB,OAAQK,KAAKL,WACR,CAAA,EAcT,OAZAK,KAAKJ,WAAa,CAChBoB,SACAE,YACAC,gBACAC,eACAE,eACAD,aACAE,UACAC,YAEFxB,KAAKH,gBAAkBA,EAEhBG,KAAKJ,UACb,CAKMM,eAAeuB,GACpB,MAAMtB,OAAEA,EAAMC,KAAEA,EAAIsB,UAAEA,GAAc1B,KAAKL,OAAOW,YAE3CN,KAAKF,MAASK,IACjBH,KAAKF,KAAO6B,EAAGC,aAAapB,EAAKC,QAAQ,GAAGL,KAAQsB,KAAc,UAGpE,IAAIG,EAAe7B,KAAKF,KAWxB,OATKK,IAIH0B,SAAsB7B,KAAKL,OAAOkB,UAAWiB,mBAAmBL,EAAIM,YAAa/B,KAAKF,OAEnFkC,QAAQ,8CAA+C,cAGrDH,EAAaI,MAAM,0BAC3B,CAKM/B,qBAIL,aAHMF,KAAKkC,uBACLlC,KAAKH,kBAAkBG,KAAKL,OAAOwC,WAElCnC,IACR"}
1
+ {"version":3,"file":"prepare-server.js","sources":["../../src/services/prepare-server.ts"],"sourcesContent":["import fs from 'fs';\nimport process from 'node:process';\nimport path from 'path';\nimport chalk from 'chalk';\nimport type { Request } from 'express';\nimport type { IEntrypointOptions, IPrepareRenderOut } from '@node/entry';\nimport type { TRender } from '@node/render';\nimport type ServerConfig from '@services/server-config';\n\ninterface IPrepareServerEntrypointLoadOut<TAppProps = Record<string, any>> {\n render: TRender;\n onRequest?: IEntrypointOptions<TAppProps>['onRequest'];\n onRouterReady?: IEntrypointOptions<TAppProps>['onRouterReady'];\n onShellReady?: IEntrypointOptions<TAppProps>['onShellReady'];\n onShellError?: IEntrypointOptions<TAppProps>['onShellError'];\n onResponse?: IEntrypointOptions<TAppProps>['onResponse'];\n onError?: IEntrypointOptions<TAppProps>['onError'];\n getState?: IEntrypointOptions<TAppProps>['getState'];\n}\n\n/**\n * Load server entrypoint and template\n * DEV MODE: refresh entrypoint and template\n */\nclass PrepareServer {\n /**\n * Server configuration\n */\n protected readonly config: ServerConfig;\n\n /**\n * Entrypoint resolved params\n */\n protected entrypoint?: IPrepareServerEntrypointLoadOut;\n\n /**\n * Hook which calls after express server created\n */\n protected onServerCreated?: IEntrypointOptions['onServerCreated'];\n\n /**\n * Html shell\n */\n protected html: string;\n\n /**\n * @constructor\n */\n protected constructor(config: ServerConfig) {\n this.config = config;\n }\n\n /**\n * Init service\n */\n public static init(config: ServerConfig): PrepareServer {\n return new PrepareServer(config);\n }\n\n /**\n * Resolve and return entrypoint params\n */\n public async loadEntrypoint(): Promise<IPrepareServerEntrypointLoadOut> {\n // load server entrypoint each time only in development mode (for fast refresh)\n if (this.entrypoint && this.config.isProd) {\n return this.entrypoint;\n }\n\n const { root, isProd, serverFile } = this.config.getParams();\n const entrypointPath = path.resolve(`${root}/${serverFile}`);\n\n let resolvedEntrypoint: IPrepareRenderOut;\n\n try {\n if (!isProd) {\n resolvedEntrypoint = (\n await this.config.getVite()!.ssrLoadModule(entrypointPath, {\n fixStacktrace: true,\n })\n ).default;\n } else {\n resolvedEntrypoint = (await import(entrypointPath)).default;\n }\n } catch (e) {\n if (e.message.includes('Cannot find module') && e.message.includes('/build/')) {\n this.config\n .getLogger()\n .error(\n `Before starting the server, you need to create a build: ${chalk.yellow(\n 'ssr-boost build',\n )}`,\n );\n\n return process.exit(1);\n }\n\n throw e;\n }\n\n const { render, init } = resolvedEntrypoint;\n const {\n onServerCreated,\n onRequest,\n onRouterReady,\n onShellReady,\n onResponse,\n onShellError,\n onError,\n getState,\n } =\n (await init?.({\n config: this.config,\n })) ?? {};\n\n this.entrypoint = {\n render,\n onRequest,\n onRouterReady,\n onShellReady,\n onShellError,\n onResponse,\n onError,\n getState,\n };\n this.onServerCreated = onServerCreated;\n\n return this.entrypoint;\n }\n\n /**\n * Load and return html shell\n */\n public async loadHtml(req: Request): Promise<[string, string]> {\n const { isProd, root, indexFile } = this.config.getParams();\n\n if (!this.html || !isProd) {\n this.html = fs.readFileSync(path.resolve(`${root}/${indexFile}`), 'utf-8');\n }\n\n let modifiedHtml = this.html;\n\n if (!isProd) {\n // Apply Vite HTML transforms. This injects the Vite HMR client,\n // and also applies HTML transforms from Vite plugins, e.g. global\n // preambles from @vitejs/plugin-react\n modifiedHtml = (await this.config.getVite()!.transformIndexHtml(req.originalUrl, this.html))\n // Make vite script 'async'\n .replace(/(<script.+)(>[\\s\\S]+injectIntoGlobalHook.+)/, '$1async$2');\n }\n\n return modifiedHtml.split('<!--ssr-outlet-->') as [string, string];\n }\n\n /**\n * Run server created hook\n */\n public async onAppCreated(): Promise<PrepareServer> {\n await this.loadEntrypoint();\n await this.onServerCreated?.(this.config.getApp()!);\n\n return this;\n }\n}\n\nexport default PrepareServer;\n"],"names":["PrepareServer","config","entrypoint","onServerCreated","html","constructor","this","static","async","isProd","root","serverFile","getParams","entrypointPath","path","resolve","resolvedEntrypoint","import","default","getVite","ssrLoadModule","fixStacktrace","e","message","includes","getLogger","error","chalk","yellow","process","exit","render","init","onRequest","onRouterReady","onShellReady","onResponse","onShellError","onError","getState","req","indexFile","fs","readFileSync","modifiedHtml","transformIndexHtml","originalUrl","replace","split","loadEntrypoint","getApp"],"mappings":"uFAwBA,MAAMA,EAIeC,OAKTC,WAKAC,gBAKAC,KAKVC,YAAsBJ,GACpBK,KAAKL,OAASA,CACf,CAKMM,YAAYN,GACjB,OAAO,IAAID,EAAcC,EAC1B,CAKMO,uBAEL,GAAIF,KAAKJ,YAAcI,KAAKL,OAAOQ,OACjC,OAAOH,KAAKJ,WAGd,MAAMQ,KAAEA,EAAID,OAAEA,EAAME,WAAEA,GAAeL,KAAKL,OAAOW,YAC3CC,EAAiBC,EAAKC,QAAQ,GAAGL,KAAQC,KAE/C,IAAIK,EAEJ,IAQIA,EAPGP,SAOyBQ,OAAOJ,IAAiBK,eAL5CZ,KAAKL,OAAOkB,UAAWC,cAAcP,EAAgB,CACzDQ,eAAe,KAEjBH,OAIL,CAAC,MAAOI,GACP,GAAIA,EAAEC,QAAQC,SAAS,uBAAyBF,EAAEC,QAAQC,SAAS,WASjE,OARAlB,KAAKL,OACFwB,YACAC,MACC,2DAA2DC,EAAMC,OAC/D,sBAICC,EAAQC,KAAK,GAGtB,MAAMR,CACP,CAED,MAAMS,OAAEA,EAAMC,KAAEA,GAAShB,GACnBb,gBACJA,EAAe8B,UACfA,EAASC,cACTA,EAAaC,aACbA,EAAYC,WACZA,EAAUC,aACVA,EAAYC,QACZA,EAAOC,SACPA,SAEOP,IAAO,CACZ/B,OAAQK,KAAKL,WACR,CAAA,EAcT,OAZAK,KAAKJ,WAAa,CAChB6B,SACAE,YACAC,gBACAC,eACAE,eACAD,aACAE,UACAC,YAEFjC,KAAKH,gBAAkBA,EAEhBG,KAAKJ,UACb,CAKMM,eAAegC,GACpB,MAAM/B,OAAEA,EAAMC,KAAEA,EAAI+B,UAAEA,GAAcnC,KAAKL,OAAOW,YAE3CN,KAAKF,MAASK,IACjBH,KAAKF,KAAOsC,EAAGC,aAAa7B,EAAKC,QAAQ,GAAGL,KAAQ+B,KAAc,UAGpE,IAAIG,EAAetC,KAAKF,KAWxB,OATKK,IAIHmC,SAAsBtC,KAAKL,OAAOkB,UAAW0B,mBAAmBL,EAAIM,YAAaxC,KAAKF,OAEnF2C,QAAQ,8CAA+C,cAGrDH,EAAaI,MAAM,0BAC3B,CAKMxC,qBAIL,aAHMF,KAAK2C,uBACL3C,KAAKH,kBAAkBG,KAAKL,OAAOiD,WAElC5C,IACR"}