Skip to content

Commit

Permalink
wip: tweak ssr build
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jan 18, 2021
1 parent ffaf50d commit c6115e9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 28 deletions.
1 change: 0 additions & 1 deletion packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,6 @@ async function doBuild(
// #764 add `Symbol.toStringTag` when build es module into cjs chunk
// #1048 add `Symbol.toStringTag` for module default export
namespaceToStringTag: true,
inlineDynamicImports: ssr && typeof input === 'string',
...output
})
}
Expand Down
6 changes: 2 additions & 4 deletions packages/vite/src/node/plugins/define.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@ export function buildDefinePlugin(config: ResolvedConfig): Plugin {
}

const individualEnvKeys: Record<string, string> = {}
const env = {
const env: Record<string, any> = {
...config.env,
SSR: !!config.build.ssr
}
for (const key in env) {
individualEnvKeys[`import.meta.env.${key}`] = JSON.stringify(
config.env[key]
)
individualEnvKeys[`import.meta.env.${key}`] = JSON.stringify(env[key])
}

const replacements: Record<string, string | undefined> = {
Expand Down
53 changes: 30 additions & 23 deletions packages/vite/src/node/plugins/importAnaysisBuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ import { chunkToEmittedCssFileMap } from './css'
export const isModernFlag = `__VITE_IS_MODERN__`

const preloadHelperId = 'vite/preload-helper'
const preloadMethod = __vitePreload.name
const preloadModuleCode = `const seen = {};export ${__vitePreload.toString()}`
const preloadMethod = `__vitePreload`
const preloadCode = `const seen = {};export const ${preloadMethod} = ${preload.toString()}`
const preloadMarker = `__VITE_PRELOAD__`
const preloadMarkerRE = new RegExp(`,?"${preloadMarker}"`, 'g')

/**
* Helper for preloading CSS and direct imports of async chunks in parallell to
* the async chunk itself.
*/
function __vitePreload(baseModule: () => Promise<{}>, deps?: string[]) {
function preload(baseModule: () => Promise<{}>, deps?: string[]) {
// @ts-ignore
if (!__VITE_IS_MODERN__ || !deps) {
return baseModule()
Expand Down Expand Up @@ -70,6 +70,8 @@ function __vitePreload(baseModule: () => Promise<{}>, deps?: string[]) {
* Build only. During serve this is performed as part of ./importAnalysis.
*/
export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
const ssr = !!config.build.ssr

return {
name: 'vite:import-analysis',

Expand All @@ -81,12 +83,12 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {

load(id) {
if (id === preloadHelperId) {
return preloadModuleCode
return preloadCode
}
},

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

Expand All @@ -105,7 +107,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {

let s: MagicString | undefined
const str = () => s || (s = new MagicString(source))
let hasInjectedHelper = false
let needPreloadHelper = false

for (let index = 0; index < imports.length; index++) {
const { s: start, e: end, ss: expStart, d: dynamicIndex } = imports[
Expand All @@ -116,30 +118,24 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
source.slice(start, end) === 'import.meta' &&
source.slice(end, end + 5) === '.glob'

if (isGlob || dynamicIndex > -1) {
// inject parallelPreload helper.
if (!hasInjectedHelper) {
hasInjectedHelper = true
str().prepend(
`import { ${preloadMethod} } from "${preloadHelperId}";`
)
}
}

// import.meta.glob
if (isGlob) {
const { imports, exp, endIndex } = await transformImportGlob(
const { imports, exp, endIndex, isEager } = await transformImportGlob(
source,
start,
importer,
index
)
str().prepend(imports)
str().overwrite(expStart, endIndex, exp)
if (!isEager) {
needPreloadHelper = true
}
continue
}

if (dynamicIndex > -1) {
if (dynamicIndex > -1 && !ssr) {
needPreloadHelper = true
const dynamicEnd = source.indexOf(`)`, end) + 1
const original = source.slice(dynamicIndex, dynamicEnd)
let replacement =
Expand All @@ -160,6 +156,10 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
}
}

if (needPreloadHelper && !ssr) {
str().prepend(`import { ${preloadMethod} } from "${preloadHelperId}";`)
}

if (s) {
return {
code: s.toString(),
Expand All @@ -177,7 +177,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
},

generateBundle({ format }, bundle) {
if (format !== 'es') {
if (format !== 'es' || ssr) {
return
}

Expand Down Expand Up @@ -261,7 +261,14 @@ export async function transformImportGlob(
importer: string,
importIndex: number,
normalizeUrl?: (url: string, pos: number) => Promise<[string, string]>
): Promise<{ imports: string; exp: string; endIndex: number }> {
): Promise<{
imports: string
exp: string
endIndex: number
isEager: boolean
}> {
const isEager = source.slice(pos, pos + 21) === 'import.meta.globEager'

const err = (msg: string) => {
const e = new Error(`Invalid glob import syntax: ${msg}`)
;(e as any).pos = pos
Expand Down Expand Up @@ -300,7 +307,6 @@ export async function transformImportGlob(
;[importee] = await normalizeUrl(file, pos)
}
const identifier = `__glob_${importIndex}_${i}`
const isEager = source.slice(pos, pos + 21) === 'import.meta.globEager'
if (isEager) {
imports += `import * as ${identifier} from ${JSON.stringify(importee)};`
entries += ` ${JSON.stringify(file)}: ${identifier},`
Expand All @@ -319,7 +325,8 @@ export async function transformImportGlob(
return {
imports,
exp: `{${entries}}`,
endIndex
endIndex,
isEager
}
}

Expand Down

0 comments on commit c6115e9

Please sign in to comment.