Skip to content

Commit eaa57bc

Browse files
userquinCopilotkevinmarrecantfu
authored
feat!: move to ESM-only (CJS generation format also dropped) (#448)
Co-authored-by: Copilot <[email protected]> Co-authored-by: Kevin Marrec <[email protected]> Co-authored-by: Anthony Fu <[email protected]>
1 parent 8e3627b commit eaa57bc

File tree

11 files changed

+219
-229
lines changed

11 files changed

+219
-229
lines changed

README.md

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Static-site generation for Vue 3 on Vite.
44

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

7+
> ℹ️ **ESM-only from `v27.0.0` (CJS generation format also dropped).**
8+
>
79
> ℹ️ **Vite 2 is supported from `v0.2.x`, Vite 1's support is discontinued.**
810
911
## Install
@@ -46,6 +48,17 @@ export const createApp = ViteSSG(
4648
)
4749
```
4850

51+
### How to allow Rollup tree-shake your client code
52+
53+
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.
54+
```ts
55+
if (import.meta.env.SSR) {
56+
// your server code will be removed in the client build
57+
}
58+
else {
59+
// your client code will be removed in the server build
60+
}
61+
```
4962
### Single Page SSG
5063

5164
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`).
@@ -307,14 +320,14 @@ const { app, router, initialState, isClient, onSSRAppRendered } = ctx
307320
const pinia = createPinia()
308321
app.use(pinia)
309322

310-
if (isClient) {
311-
pinia.state.value = (initialState.pinia) || {}
312-
}
313-
else {
323+
if (import.meta.env.SSR) {
314324
onSSRAppRendered(() => {
315325
initialState.pinia = pinia.state.value
316326
})
317327
}
328+
else {
329+
pinia.state.value = (initialState.pinia) || {}
330+
}
318331
```
319332

320333
## Configuration

build.config.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@ export default defineBuildConfig({
88
{ input: 'src/node', name: 'node' },
99
],
1010
clean: true,
11-
declaration: true,
11+
declaration: 'node16',
1212
externals: [
1313
'vue',
1414
'vue/server-renderer',
1515
'vue/compiler-sfc',
1616
],
1717
rollup: {
18-
emitCJS: true,
1918
inlineDependencies: true,
2019
},
2120
})
File renamed without changes.

examples/multiple-pages-pwa/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"vite-plugin-pages": "catalog:",
1919
"vite-plugin-pwa": "catalog:",
2020
"vite-ssg": "workspace:*",
21-
"vue-router": "catalog:"
21+
"vue-router": "catalog:",
22+
"workbox-build": "catalog:"
2223
}
2324
}

package.json

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"name": "vite-ssg",
3+
"type": "module",
34
"version": "26.1.1",
45
"packageManager": "[email protected]",
56
"description": "Server-side generation for Vite",
@@ -20,29 +21,20 @@
2021
],
2122
"sideEffects": false,
2223
"exports": {
23-
".": {
24-
"import": "./dist/index.mjs",
25-
"require": "./dist/index.cjs"
26-
},
27-
"./node": {
28-
"import": "./dist/node.mjs",
29-
"require": "./dist/node.cjs"
30-
},
31-
"./single-page": {
32-
"import": "./dist/client/single-page.mjs",
33-
"require": "./dist/client/single-page.cjs"
34-
}
24+
".": "./dist/index.mjs",
25+
"./node": "./dist/node.mjs",
26+
"./single-page": "./dist/client/single-page.mjs"
3527
},
36-
"main": "dist/index.cjs",
28+
"main": "dist/index.mjs",
3729
"module": "dist/index.mjs",
38-
"types": "dist/index.d.ts",
30+
"types": "dist/index.d.mts",
3931
"typesVersions": {
4032
"*": {
41-
"single-page": [
42-
"dist/client/single-page.d.ts"
43-
],
4433
"node": [
45-
"dist/node.d.ts"
34+
"dist/node.d.mts"
35+
],
36+
"single-page": [
37+
"dist/client/single-page.d.mts"
4638
]
4739
}
4840
},

0 commit comments

Comments
 (0)