refactor(vite): reuse common modules (#1723)

This commit is contained in:
Anthony Fu 2021-11-08 18:34:39 +08:00 committed by GitHub
parent 39db33d625
commit ca557e7c0b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 11 additions and 48 deletions

View File

@ -1,7 +1,7 @@
import { resolve } from 'pathe'
import fse from 'fs-extra'
import { uniq, isJS, isCSS, hash } from '../../../vite/src/utils'
import { ViteBuildContext } from './types'
import { uniq, isJS, isCSS, hash } from './utils'
const DEFAULT_APP_TEMPLATE = `
<!DOCTYPE html>

View File

@ -5,11 +5,11 @@ import consola from 'consola'
import fse from 'fs-extra'
import pDebounce from 'p-debounce'
import { bundleRequest } from '../../../vite/src/dev-bundler'
import { ViteBuildContext, ViteOptions } from './types'
import { isCSS } from '../../../vite/src/utils'
import { wpfs } from './utils/wpfs'
import { ViteBuildContext, ViteOptions } from './types'
import { jsxPlugin } from './plugins/jsx'
import { generateDevSSRManifest } from './manifest'
import { isCSS } from './utils'
export async function buildServer (ctx: ViteBuildContext) {
// Workaround to disable HMR

View File

@ -1,20 +0,0 @@
import type { ViteDevServer } from 'vite'
import consola from 'consola'
export async function warmupViteServer (server: ViteDevServer, entries: string[]) {
const warmedUrls = new Set<String>()
const warmup = async (url: string) => {
if (warmedUrls.has(url)) { return undefined }
warmedUrls.add(url)
try {
await server.transformRequest(url)
} catch (e) {
consola.debug('Warmup for %s failed with: %s', url, e)
}
const deps = Array.from(server.moduleGraph.urlToModuleMap.get(url)?.importedModules || [])
await Promise.all(deps.map(m => warmup(m.url)))
}
await Promise.all(entries.map(entry => warmup(entry)))
}

View File

@ -2,13 +2,13 @@ import { resolve } from 'pathe'
import * as vite from 'vite'
import consola from 'consola'
import { distDir } from '../dirs'
import { warmupViteServer } from '../../../vite/src/utils/warmup'
import { buildClient } from './client'
import { buildServer } from './server'
import { defaultExportPlugin } from './plugins/default-export'
import { jsxPlugin } from './plugins/jsx'
import { replace } from './plugins/replace'
import { resolveCSSOptions } from './css'
import { warmupViteServer } from './utils/warmup'
import type { Nuxt, ViteBuildContext, ViteOptions } from './types'
import { prepareManifests } from './manifest'

View File

@ -1,22 +0,0 @@
import { createHash } from 'crypto'
export function hashId (id: string) {
return '$id_' + hash(id)
}
export function hash (input: string, length = 8) {
return createHash('sha256')
.update(input)
.digest('hex')
.substr(0, length)
}
export function uniq<T> (arr: T[]): T[] {
return Array.from(new Set(arr))
}
const IS_CSS_RE = /\.(?:css|scss|sass|postcss|less|stylus|styl)(\?[^.]+)?$/
export function isCSS (file: string) {
return IS_CSS_RE.test(file)
}

View File

@ -8,7 +8,7 @@ export function uniq<T> (arr: T[]): T[] {
const IS_JS_RE = /\.[cm]?js(\?[^.]+)?$/
const IS_MODULE_RE = /\.mjs(\?[^.]+)?$/
const HAS_EXT_RE = /[^./]+\.[^./]+$/
const IS_CSS_RE = /\.css(\?[^.]+)?$/
const IS_CSS_RE = /\.(?:css|scss|sass|postcss|less|stylus|styl)(\?[^.]+)?$/
export function isJS (file: string) {
return IS_JS_RE.test(file) || !HAS_EXT_RE.test(file)

View File

@ -1,3 +1,4 @@
import consola from 'consola'
import type { ViteDevServer } from 'vite'
export async function warmupViteServer (server: ViteDevServer, entries: string[]) {
@ -6,7 +7,11 @@ export async function warmupViteServer (server: ViteDevServer, entries: string[]
const warmup = async (url: string) => {
if (warmedUrls.has(url)) { return undefined }
warmedUrls.add(url)
await server.transformRequest(url)
try {
await server.transformRequest(url)
} catch (e) {
consola.debug('Warmup for %s failed with: %s', url, e)
}
const deps = Array.from(server.moduleGraph.urlToModuleMap.get(url)?.importedModules || [])
await Promise.all(deps.map(m => warmup(m.url)))
}