Skip to content

Commit

Permalink
feat: Adding ssrFormat flag to overwrite default output format
Browse files Browse the repository at this point in the history
  • Loading branch information
rschristian committed May 3, 2021
1 parent 2957123 commit a102b11
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 6 deletions.
17 changes: 17 additions & 0 deletions packages/playground/esm-ssr/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "test-esm-ssr",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"build": "vite build --ssr src/App.jsx"
},
"dependencies": {
"preact": "^10.5.13",
"preact-render-to-string": "^5.1.19"
},
"devDependencies": {
"@preact/preset-vite": "2.0.1",
"vite": "^2.2.3"
}
}
23 changes: 23 additions & 0 deletions packages/playground/esm-ssr/src/App.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { render } from 'preact-render-to-string';

export function App() {
return (
<>
<p>Hello Vite + Preact!</p>
<p>
<a
class="link"
href="https://preactjs.com/"
target="_blank"
rel="noopener noreferrer"
>
Learn Preact
</a>
</p>
</>
)
}

export async function prerender() {
return await render(<App />);
}
6 changes: 6 additions & 0 deletions packages/playground/esm-ssr/vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { defineConfig } from 'vite'
import preact from '@preact/preset-vite'

export default defineConfig({
plugins: [preact.default()]
})
20 changes: 14 additions & 6 deletions packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ export interface BuildOptions {
* `rollupOptions.input`.
*/
ssr?: boolean | string
/**
* When present, overrides the module format for the project to instead output
* the SSR in another format.
*/
ssrFormat?: 'cjs' | 'es' | null
/**
* Generate SSR manifest for determining style links and asset preload
* directives in production.
Expand Down Expand Up @@ -220,6 +225,7 @@ export function resolveBuildOptions(raw?: BuildOptions): ResolvedBuildOptions {
manifest: false,
lib: false,
ssr: false,
ssrFormat: null,
ssrManifest: false,
brotliSize: true,
chunkSizeWarningLimit: 500,
Expand Down Expand Up @@ -332,6 +338,13 @@ async function doBuild(
)
}

const ssrFormat =
options.ssrFormat ||
(JSON.parse(fs.readFileSync(resolve('package.json'), 'utf-8')).type ===
'module'
? 'es'
: 'cjs')

const outDir = resolve(options.outDir)

// inject ssr arg to plugin load/transform hooks
Expand Down Expand Up @@ -399,12 +412,7 @@ async function doBuild(
const buildOutputOptions = (output: OutputOptions = {}): OutputOptions => {
return {
dir: outDir,
format:
ssr &&
JSON.parse(fs.readFileSync(resolve('package.json'), 'utf-8')).type !=
'module'
? 'cjs'
: 'es',
format: ssr ? ssrFormat : 'es',
exports: ssr ? 'named' : 'auto',
sourcemap: options.sourcemap,
name: libOptions ? libOptions.name : undefined,
Expand Down
4 changes: 4 additions & 0 deletions packages/vite/src/node/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ cli
'--ssr [entry]',
`[string] build specified entry for server-side rendering`
)
.option(
'-ssrFormat [format]',
'[string] SSR module format, either "cjs" or "es"'
)
.option(
'--sourcemap',
`[boolean] output source maps for build (default: false)`
Expand Down

0 comments on commit a102b11

Please sign in to comment.