Skip to content

feat!: move to ESM-only (CJS generation format also dropped) #448

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Static-site generation for Vue 3 on Vite.

[![NPM version](https://img.shields.io/npm/v/vite-ssg?color=a1b858)](https://www.npmjs.com/package/vite-ssg)

> ℹ️ **ESM-only from `v27.0.0` (CJS generation format also dropped).**
>
> ℹ️ **Vite 2 is supported from `v0.2.x`, Vite 1's support is discontinued.**

## Install
Expand Down Expand Up @@ -46,6 +48,17 @@ export const createApp = ViteSSG(
)
```

### How to allow Rollup tree-shake your client code

In order to allow Rollup tree-shake your client code at build time, you need to wrap your code using `import.meta.env.SSR`, that's, checking if the build is for the server: Rollup will remove the server code from the client build.
```ts
if (import.meta.env.SSR) {
// your server code will be removed in the client build
}
else {
// your client code will be removed in the server build
}
```
### Single Page SSG

For SSG of an index page only (i.e. without `vue-router`); import `vite-ssg/single-page` instead, and only install `@unhead/vue` (`npm i -D vite-ssg @unhead/vue`).
Expand Down Expand Up @@ -307,14 +320,14 @@ const { app, router, initialState, isClient, onSSRAppRendered } = ctx
const pinia = createPinia()
app.use(pinia)

if (isClient) {
pinia.state.value = (initialState.pinia) || {}
}
else {
if (import.meta.env.SSR) {
onSSRAppRendered(() => {
initialState.pinia = pinia.state.value
})
}
else {
pinia.state.value = (initialState.pinia) || {}
}
```

## Configuration
Expand Down
3 changes: 1 addition & 2 deletions build.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ export default defineBuildConfig({
{ input: 'src/node', name: 'node' },
],
clean: true,
declaration: true,
declaration: 'node16',
externals: [
'vue',
'vue/server-renderer',
'vue/compiler-sfc',
],
rollup: {
emitCJS: true,
inlineDependencies: true,
},
})
File renamed without changes.
3 changes: 2 additions & 1 deletion examples/multiple-pages-pwa/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"vite-plugin-pages": "catalog:",
"vite-plugin-pwa": "catalog:",
"vite-ssg": "workspace:*",
"vue-router": "catalog:"
"vue-router": "catalog:",
"workbox-build": "catalog:"
}
}
28 changes: 10 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "vite-ssg",
"type": "module",
"version": "26.1.1",
"packageManager": "[email protected]",
"description": "Server-side generation for Vite",
Expand All @@ -20,29 +21,20 @@
],
"sideEffects": false,
"exports": {
".": {
"import": "./dist/index.mjs",
"require": "./dist/index.cjs"
},
"./node": {
"import": "./dist/node.mjs",
"require": "./dist/node.cjs"
},
"./single-page": {
"import": "./dist/client/single-page.mjs",
"require": "./dist/client/single-page.cjs"
}
".": "./dist/index.mjs",
"./node": "./dist/node.mjs",
"./single-page": "./dist/client/single-page.mjs"
},
"main": "dist/index.cjs",
"main": "dist/index.mjs",
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"types": "dist/index.d.mts",
"typesVersions": {
"*": {
"single-page": [
"dist/client/single-page.d.ts"
],
"node": [
"dist/node.d.ts"
"dist/node.d.mts"
],
"single-page": [
"dist/client/single-page.d.mts"
]
}
},
Expand Down
Loading