{"id":24957366,"url":"https://github.com/seasonjs/tegg-vite-plugin","last_synced_at":"2025-10-07T06:57:04.294Z","repository":{"id":45654798,"uuid":"431839913","full_name":"seasonjs/tegg-vite-plugin","owner":"seasonjs","description":"Use vite and egg for ssr or csr","archived":false,"fork":false,"pushed_at":"2024-02-22T00:44:49.000Z","size":231,"stargazers_count":5,"open_issues_count":5,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-08-21T08:35:29.765Z","etag":null,"topics":["csr","egg","egg-plugin","eggjs","plugin","ssr","vite"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/seasonjs.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-11-25T12:37:26.000Z","updated_at":"2023-11-02T07:07:49.000Z","dependencies_parsed_at":"2022-09-06T07:11:23.038Z","dependency_job_id":"e11d47f2-81d2-4c35-aff8-344ea1c159c7","html_url":"https://github.com/seasonjs/tegg-vite-plugin","commit_stats":{"total_commits":17,"total_committers":2,"mean_commits":8.5,"dds":"0.23529411764705888","last_synced_commit":"90bd10f92b2fa5932519a8a70d1b590d7fede0a3"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/seasonjs/tegg-vite-plugin","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seasonjs%2Ftegg-vite-plugin","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seasonjs%2Ftegg-vite-plugin/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seasonjs%2Ftegg-vite-plugin/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seasonjs%2Ftegg-vite-plugin/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/seasonjs","download_url":"https://codeload.github.com/seasonjs/tegg-vite-plugin/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seasonjs%2Ftegg-vite-plugin/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278734412,"owners_count":26036404,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-07T02:00:06.786Z","response_time":59,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["csr","egg","egg-plugin","eggjs","plugin","ssr","vite"],"created_at":"2025-02-03T07:00:19.756Z","updated_at":"2025-10-07T06:57:04.264Z","avatar_url":"https://github.com/seasonjs.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"English | [简体中文](./README.zh-CN.md)\n\n\u003cimg src=\"https://github.com//seasonjs/tools/blob/main/public/icon.svg?raw=true\" alt=\"logo.png\" width=\"150\"\u003e\n\n# @seasonjs/tegg-vite-plugin\n\n\u003ca href=\"https://www.npmjs.com/package/@seasonjs/tools\"\u003e\u003cimg src=\"https://img.shields.io/npm/v/@seasonjs/tegg-vite-plugin.svg?sanitize=true\" alt=\"Version\"\u003e\u003c/a\u003e\n\u003ca href=\"https://www.npmjs.com/package/@seasonjs/tools\"\u003e\u003cimg src=\"https://img.shields.io/npm/l/@seasonjs/tegg-vite-plugin.svg?sanitize=true\" alt=\"License\"\u003e\u003c/a\u003e\n\nuse vite and egg for ssr or csr\n\n## 1. Enable this plugin just two step\n\nThis example not enable ssr by default,it just enable csr\n\n1. Add `vite` filed to config by default will use default config it will set vite root dir at `/client`\n\n```typescript\n//config/config.default.ts\nimport {EggAppConfig, PowerPartial} from \"egg\";\n\nconst config: PowerPartial\u003cEggAppConfig\u003e = {\n    vite: {}\n}\nexport default config;\n\n```\n2. Add tegg-vite-plugin to `plugin.ts`\n\n```typescript\n//config/plugin.ts\nimport { EggPlugin } from 'egg';\nimport * as path from 'path';\n\nconst plugin: EggPlugin = {\n  teggVite: {\n    enable: true,\n    package: '@seasonjs/tegg-vite-plugin',\n  },\n};\n\nexport default plugin;\n```\n\n## 2. egg config options\n\n`vite` config filed extends `vite` config options by [InlineConfig](https://vitejs.dev/guide/api-javascript.html#inlineconfig) ,\n\ndetail see [vite doc:createserver](https://vitejs.dev/guide/api-javascript.html#createserver).\n\nbut there have add an new filed call `teggSSR`:\n```typescript\n\n interface ViteConfig extends InlineConfig {\n    teggSSR?: {\n        html?: string //template html path\n        entry?: string //ssr server js entry\n    }\n}\n\n```\n\n## 3. Enable SSR\n\n1. Add `vite` filed to config by default will use default config it will set vite root dir at `/client`.\n\nIf you use default setting,it will set html path to `your-project-root/client/index.html`\n\nand use ssr-entry by this plugin default render function:\n\n\n```typescript\n//config/config.default.ts\nimport {EggAppConfig, PowerPartial} from \"egg\";\nimport * as path from 'path';\n\nconst config: PowerPartial\u003cEggAppConfig\u003e = {\n    vite: {\n        server: {middlewareMode: 'ssr'},\n    }\n}\nexport default config;\n\n```\n\nor you can set by custom:\n```typescript\n//config/config.default.ts\nimport {EggAppConfig, PowerPartial} from \"egg\";\nimport * as path from 'path';\n\nconst config: PowerPartial\u003cEggAppConfig\u003e = {\n    vite: {\n        server: {middlewareMode: 'ssr'},\n        teggSSR: {\n            html: path.reslove('../client/index.html'),//path to your-project/client/index.html\n            entry: path.reslove('../client/ssr-entry.ts')//path to your-project/client/ssr-entry.ts\n        }\n    }\n}\nexport default config;\n\n```\n2. Put your render function to controller,if you want to use this plugin default handler:\n\n\n__[Notie]:  default handler not ready for work__\n\n```typescript\n//controller/SSRController.ts\nimport {\n    Context,\n    EggContext,\n    HTTPController,\n    HTTPMethod,\n    HTTPMethodEnum,\n    HTTPQuery,\n} from '@eggjs/tegg';\nimport { EggLogger } from 'egg';\n\n\n@HTTPController()\nexport class SSRController {\n\n    @HTTPMethod({\n        method: HTTPMethodEnum.GET,\n        path: '/*',\n    })\n    async hello(@Context() ctx: EggContext) {\n        this.logger.info('access url: %s', ctx.url);\n        ctx.viteSSRRender(ctx)\n    }\n}\n\n```\n\nElse I suggest you use your custom handle function It not complex:\n\n```typescript\n//controller/SSRController.ts\nimport {\n    Context,\n    EggContext,\n    HTTPController,\n    HTTPMethod,\n    HTTPMethodEnum,\n    HTTPQuery,\n} from '@eggjs/tegg';\nimport {Application, EggLogger} from 'egg';\n\n\n@HTTPController()\nexport class SSRController {\n    @Inject()\n    private app: Application;\n\n    @HTTPMethod({\n        method: HTTPMethodEnum.GET,\n        path: '/*',\n    })\n    async hello(@Context() ctx: EggContext) {\n        this.logger.info('access url: %s', ctx.url);\n        const url = ctx.req.originalUrl\n\n        try {\n            // 1. Read index.html\n            let template = fs.readFileSync(\n                path.resolve(__dirname, 'index.html'),\n                'utf-8'\n            )\n\n            // 2. Apply Vite HTML transforms. This injects the Vite HMR client, and\n            //    also applies HTML transforms from Vite plugins, e.g. global preambles\n            //    from @vitejs/plugin-react-refresh\n            template = await this.app.vite.transformIndexHtml(url, template)\n\n            // 3. Load the server entry. vite.ssrLoadModule automatically transforms\n            //    your ESM source code to be usable in Node.js! There is no bundling\n            //    required, and provides efficient invalidation similar to HMR.\n            const {render} = await this.app.vite.ssrLoadModule('/src/entry-server.js')\n\n            // 4. render the app HTML. This assumes entry-server.js's exported `render`\n            //    function calls appropriate framework SSR APIs,\n            //    e.g. ReactDOMServer.renderToString()\n            const appHtml = await render(url)\n\n            // 5. Inject the app-rendered HTML into the template.\n            const html = template.replace(`\u003c!--ssr-outlet--\u003e`, appHtml)\n\n            // 6. Send the rendered HTML back.\n            ctx.status = 200;\n            ctx.set('Content-Type', 'text/html');\n            ctx.body = html;\n        } catch (e) {\n            // If an error is caught, let Vite fix the stracktrace so it maps back to\n            // your actual source code.\n            app.vite.ssrFixStacktrace(e);\n            ctx.logger.error(e);\n            ctx.res.status(500);\n            ctx.body = e.message;\n        }\n    }\n}\n\n```\n## Support\n\nSupport all node \u003e 14.0.0 and egg \u003e 2.0.0\n\n## Contributors\n\n[@Cyberhan123](https://github.com/cyberhan123)\n\n## License\n\n[MIT](LICENSE)\nCopyright © 2021, seasonjs\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fseasonjs%2Ftegg-vite-plugin","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fseasonjs%2Ftegg-vite-plugin","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fseasonjs%2Ftegg-vite-plugin/lists"}