Skip to content
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

feat: Supports ES SSR build based on module type #2157

Closed
wants to merge 4 commits into from
Closed
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
13 changes: 13 additions & 0 deletions packages/playground/esm-ssr/__tests__/build.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import fs from 'fs'
import path from 'path'
import { isBuild } from '../../testUtils'

if (isBuild) {
test('outputs ESM', () => {
const content = fs.readFileSync(
path.join(__dirname, '..', 'dist', 'App.js'),
'utf8'
)
expect(/^import/.test(content)).toBeTruthy()
})
}
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()]
})
23 changes: 18 additions & 5 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 @@ -396,10 +409,10 @@ async function doBuild(
try {
const pkgName = libOptions && getPkgName(config.root)

const buildOuputOptions = (output: OutputOptions = {}): OutputOptions => {
const buildOutputOptions = (output: OutputOptions = {}): OutputOptions => {
return {
dir: outDir,
format: ssr ? 'cjs' : 'es',
format: ssr ? ssrFormat : 'es',
exports: ssr ? 'named' : 'auto',
sourcemap: options.sourcemap,
name: libOptions ? libOptions.name : undefined,
Expand Down Expand Up @@ -443,10 +456,10 @@ async function doBuild(
const output: OutputOptions[] = []
if (Array.isArray(outputs)) {
for (const resolvedOutput of outputs) {
output.push(buildOuputOptions(resolvedOutput))
output.push(buildOutputOptions(resolvedOutput))
}
} else {
output.push(buildOuputOptions(outputs))
output.push(buildOutputOptions(outputs))
}

const watcherOptions = config.build.watch
Expand Down Expand Up @@ -494,7 +507,7 @@ async function doBuild(

const generate = (output: OutputOptions = {}) => {
return bundle[options.write ? 'write' : 'generate'](
buildOuputOptions(output)
buildOutputOptions(output)
)
}

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