Skip to content

Commit

Permalink
wip: ssr build
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jan 18, 2021
1 parent 0e6a10a commit 7a45427
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 17 deletions.
44 changes: 31 additions & 13 deletions packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ export function resolveBuildOptions(
extensions: ['.js', '.cjs'],
...raw?.commonjsOptions
},
minify: 'terser',
minify: raw?.ssr ? false : 'terser',
terserOptions: {},
cleanCssOptions: {},
write: true,
Expand Down Expand Up @@ -275,23 +275,32 @@ async function doBuild(
config.logger.info(chalk.cyan(`building for ${config.mode}...`))

const options = config.build
const ssr = !!options.ssr
const libOptions = options.lib
const resolve = (p: string) => path.resolve(config.root, p)

const input = libOptions
? libOptions.entry
: options.rollupOptions?.input || resolve('index.html')

if (ssr && typeof input === 'string' && input.endsWith('.html')) {
throw new Error(
`rollupOptions.input should not be an html file when building for SSR. ` +
`Please specify a dedicated SSR entry.`
)
}

const outDir = resolve(options.outDir)
const publicDir = resolve('public')

// inject ssr arg to plugin load/transform hooks
const plugins = (options.ssr
const plugins = (ssr
? config.plugins.map((p) => injectSsrFlagToHooks(p))
: config.plugins) as Plugin[]

// inject ssrExternal if present
const userExternal = options.rollupOptions?.external
const external = options.ssr
const external = ssr
? resolveExternal(resolveSSRExternal(config.root), userExternal)
: userExternal

Expand All @@ -300,7 +309,11 @@ async function doBuild(
try {
const bundle = await rollup.rollup({
input,
preserveEntrySignatures: libOptions ? 'strict' : false,
preserveEntrySignatures: ssr
? 'allow-extension'
: libOptions
? 'strict'
: false,
...options.rollupOptions,
plugins,
external,
Expand All @@ -318,22 +331,27 @@ async function doBuild(
const generate = (output: OutputOptions = {}) => {
return bundle[options.write ? 'write' : 'generate']({
dir: outDir,
format: 'es',
exports: 'auto',
format: options.ssr ? 'cjs' : 'es',
exports: options.ssr ? 'named' : 'auto',
sourcemap: options.sourcemap,
name: libOptions ? libOptions.name : undefined,
entryFileNames: libOptions
? `${pkgName}.${output.format || `es`}.js`
: path.posix.join(options.assetsDir, `[name].[hash].js`),
chunkFileNames: libOptions
entryFileNames: ssr
? `[name].js`
: libOptions
? `${pkgName}.${output.format || `es`}.js`
: path.posix.join(options.assetsDir, `[name].[hash].js`),
assetFileNames: libOptions
? `[name].[ext]`
: path.posix.join(options.assetsDir, `[name].[hash].[ext]`),
chunkFileNames:
libOptions || ssr
? `[name].js`
: path.posix.join(options.assetsDir, `[name].[hash].js`),
assetFileNames:
libOptions || ssr
? `[name].[ext]`
: path.posix.join(options.assetsDir, `[name].[hash].[ext]`),
// #764 add `Symbol.toStringTag` when build es module into cjs chunk
// #1048 add `Symbol.toStringTag` for module default export
namespaceToStringTag: true,
inlineDynamicImports: ssr,
...output
})
}
Expand Down
17 changes: 15 additions & 2 deletions packages/vite/src/node/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,10 @@ cli
'--assetsInlineLimit <number>',
`[number] static asset base64 inline threshold in bytes (default: 4096)`
)
.option('--ssr', `[boolean] build for server-side rendering`)
.option(
'--ssr <entry>',
`[string] build specified entry for server-side rendering`
)
.option(
'--sourcemap',
`[boolean] output source maps for build (default: false)`
Expand All @@ -124,14 +127,24 @@ cli
.option('-m, --mode <mode>', `[string] set env mode`)
.action(async (root: string, options: BuildOptions & GlobalCLIOptions) => {
const { build } = await import('./build')
const buildOptions = cleanOptions(options) as BuildOptions

if (buildOptions.ssr) {
buildOptions.rollupOptions = {
...buildOptions.rollupOptions,
input: (buildOptions.ssr as any) as string
}
buildOptions.ssr = true
}

try {
await build({
root,
mode: options.mode,
configFile: options.config,
logLevel: options.logLevel,
clearScreen: options.clearScreen,
build: cleanOptions(options) as BuildOptions
build: buildOptions
})
} catch (e) {
createLogger(options.logLevel).error(
Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/node/plugins/importAnaysisBuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
}
},

async transform(source, importer) {
if (importer.includes('node_modules')) {
async transform(source, importer, ssr) {
if (ssr || importer.includes('node_modules')) {
return
}

Expand Down

0 comments on commit 7a45427

Please sign in to comment.