Skip to content

Commit

Permalink
feat: build --ssr
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed May 9, 2020
1 parent b5cf006 commit 49e79e7
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.DS_Store
node_modules
dist
dist-ssr
TODOs.md
*.log
test/temp
5 changes: 2 additions & 3 deletions src/node/build/buildPluginHtml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,8 @@ export const createBuildHtmlPlugin = async (
}

const renderIndex = (
root: string,
cssFileName: string,
bundleOutput: RollupOutput['output']
bundleOutput: RollupOutput['output'],
cssFileName: string
) => {
// inject css link
processedHtml = injectCSS(processedHtml, cssFileName)
Expand Down
23 changes: 18 additions & 5 deletions src/node/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ export interface BuildOptions {
factory?: string
fragment?: string
}
/**
* Build for server-side rendering
*/
ssr?: boolean

// The following are API only and not documented in the CLI. -----------------

Expand Down Expand Up @@ -127,6 +131,11 @@ const writeColors = {
* Returns a Promise containing the build result.
*/
export async function build(options: BuildOptions = {}): Promise<BuildResult> {
if (options.ssr) {
delete options.ssr
return ssrBuild(options)
}

process.env.NODE_ENV = 'production'
const start = Date.now()

Expand All @@ -149,7 +158,7 @@ export async function build(options: BuildOptions = {}): Promise<BuildResult> {
sourcemap = false
} = options

const indexPath = emitIndex ? path.resolve(root, 'index.html') : null
const indexPath = path.resolve(root, 'index.html')
const publicBasePath = base.replace(/([^/])$/, '$1/') // ensure ending slash
const resolvedAssetsPath = path.join(outDir, assetsDir)
const cssFileName = 'style.css'
Expand Down Expand Up @@ -244,7 +253,7 @@ export async function build(options: BuildOptions = {}): Promise<BuildResult> {
...rollupOutputOptions
})

const indexHtml = renderIndex(root, cssFileName, output)
const indexHtml = emitIndex ? renderIndex(output, cssFileName) : ''

if (write) {
const cwd = process.cwd()
Expand Down Expand Up @@ -307,9 +316,11 @@ export async function build(options: BuildOptions = {}): Promise<BuildResult> {
}

// copy over /public if it exists
const publicDir = path.resolve(root, 'public')
if (await fs.pathExists(publicDir)) {
await fs.copy(publicDir, path.resolve(outDir, 'public'))
if (emitAssets) {
const publicDir = path.resolve(root, 'public')
if (await fs.pathExists(publicDir)) {
await fs.copy(publicDir, path.resolve(outDir, 'public'))
}
}
}

Expand Down Expand Up @@ -344,6 +355,8 @@ export async function ssrBuild(
} = options

return build({
outDir: path.resolve(options.root || process.cwd(), 'dist-ssr'),
assetsDir: '.',
...options,
rollupPluginVueOptions: {
...rollupPluginVueOptions,
Expand Down
1 change: 1 addition & 0 deletions src/node/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Options:
--sourcemap [boolean] output source maps for build (default: false)
--minify [boolean | 'terser' | 'esbuild'] disable minification, or specify
minifier to use. (default: 'terser')
--ssr [boolean] build for server-side rendering
--jsx-factory [string] (default: React.createElement)
--jsx-fragment [string] (default: React.Fragment)
`)
Expand Down

0 comments on commit 49e79e7

Please sign in to comment.