feat: lean builds

pull/18/head
Evan You 5 years ago
parent 2f8ef43da5
commit b61e2398fc

@ -4,7 +4,7 @@ import fs from 'fs-extra'
import { APP_PATH, createResolver, SITE_DATA_REQUEST_PATH } from '../resolver' import { APP_PATH, createResolver, SITE_DATA_REQUEST_PATH } from '../resolver'
import { BuildOptions, ASSETS_DIR } from './build' import { BuildOptions, ASSETS_DIR } from './build'
import { SiteConfig } from '../config' import { SiteConfig } from '../config'
import { Plugin } from 'rollup' import { Plugin, OutputAsset, OutputChunk } from 'rollup'
import { createMarkdownToVueRenderFn } from '../markdownToVue' import { createMarkdownToVueRenderFn } from '../markdownToVue'
import { import {
build, build,
@ -13,6 +13,20 @@ import {
BuildResult BuildResult
} from 'vite' } from 'vite'
const staticInjectMarkerRE = /\b(const _hoisted_\d+ = \/\*#__PURE__\*\/createStaticVNode)\("(.*)", (\d+)\)/g
const staticStripRE = /__VP_STATIC_START__.*?__VP_STATIC_END__/g
const staticRestoreRE = /__VP_STATIC_(START|END)__/g
const isPageChunk = (
chunk: OutputAsset | OutputChunk
): chunk is OutputChunk & { facadeModuleId: string } =>
!!(
chunk.type === 'chunk' &&
chunk.isEntry &&
chunk.facadeModuleId &&
chunk.facadeModuleId.endsWith('.md')
)
// bundles the VitePress app for both client AND server. // bundles the VitePress app for both client AND server.
export async function bundle( export async function bundle(
config: SiteConfig, config: SiteConfig,
@ -44,16 +58,29 @@ export async function bundle(
return vueSrc return vueSrc
} }
}, },
renderChunk(code, chunk) {
if (isClientBuild && isPageChunk(chunk as OutputChunk)) {
// For each page chunk, inject marker for start/end of static strings.
// we do this here because in generateBundle the chunks would have been
// minified and we won't be able to safely locate the strings.
// Using a regexp relies on specific output from Vue compiler core,
// which is a reasonable trade-off considering the massive perf win over
// a full AST parse.
code = code.replace(
staticInjectMarkerRE,
'$1("__VP_STATIC_START__$2__VP_STATIC_END__", $3)'
)
return code
}
return null
},
generateBundle(_options, bundle) { generateBundle(_options, bundle) {
// for each .md entry chunk, adjust its name to its correct path. // for each .md entry chunk, adjust its name to its correct path.
for (const name in bundle) { for (const name in bundle) {
const chunk = bundle[name] const chunk = bundle[name]
if ( if (isPageChunk(chunk)) {
chunk.type === 'chunk' &&
chunk.isEntry &&
chunk.facadeModuleId &&
chunk.facadeModuleId.endsWith('.md')
) {
// foo/bar.md -> foo_bar.md.js // foo/bar.md -> foo_bar.md.js
chunk.fileName = chunk.fileName =
slash(path.relative(root, chunk.facadeModuleId)).replace( slash(path.relative(root, chunk.facadeModuleId)).replace(
@ -66,11 +93,10 @@ export async function bundle(
bundle[name + '-lean'] = { bundle[name + '-lean'] = {
...chunk, ...chunk,
fileName: chunk.fileName.replace(/\.js$/, '.lean.js'), fileName: chunk.fileName.replace(/\.js$/, '.lean.js'),
code: chunk.code.replace( code: chunk.code.replace(staticStripRE, ``)
/createStaticVNode\([^)]+, (\d+)\)/g,
`createStaticVNode("", $1)`
)
} }
// remove static markers from orginal code
chunk.code = chunk.code.replace(staticRestoreRE, '')
} }
} }
} }

Loading…
Cancel
Save