From ca557e7c0bad744e9c16624272594e5a5c49627f Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Mon, 8 Nov 2021 18:34:39 +0800 Subject: [PATCH] refactor(vite): reuse common modules (#1723) --- packages/bridge/src/vite/manifest.ts | 2 +- packages/bridge/src/vite/server.ts | 4 ++-- packages/bridge/src/vite/utils/warmup.ts | 20 ----------------- packages/bridge/src/vite/vite.ts | 2 +- packages/vite/src/utils.ts | 22 ------------------- .../vite/utils.ts => vite/src/utils/index.ts} | 2 +- packages/vite/src/utils/warmup.ts | 7 +++++- 7 files changed, 11 insertions(+), 48 deletions(-) delete mode 100644 packages/bridge/src/vite/utils/warmup.ts delete mode 100644 packages/vite/src/utils.ts rename packages/{bridge/src/vite/utils.ts => vite/src/utils/index.ts} (90%) diff --git a/packages/bridge/src/vite/manifest.ts b/packages/bridge/src/vite/manifest.ts index 93ffa5e9a4..421536872e 100644 --- a/packages/bridge/src/vite/manifest.ts +++ b/packages/bridge/src/vite/manifest.ts @@ -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 = ` diff --git a/packages/bridge/src/vite/server.ts b/packages/bridge/src/vite/server.ts index 6cfdbe6a6d..a91a265d69 100644 --- a/packages/bridge/src/vite/server.ts +++ b/packages/bridge/src/vite/server.ts @@ -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 diff --git a/packages/bridge/src/vite/utils/warmup.ts b/packages/bridge/src/vite/utils/warmup.ts deleted file mode 100644 index 05a271bd44..0000000000 --- a/packages/bridge/src/vite/utils/warmup.ts +++ /dev/null @@ -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() - - 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))) -} diff --git a/packages/bridge/src/vite/vite.ts b/packages/bridge/src/vite/vite.ts index af067041ba..2ed65beb7d 100644 --- a/packages/bridge/src/vite/vite.ts +++ b/packages/bridge/src/vite/vite.ts @@ -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' diff --git a/packages/vite/src/utils.ts b/packages/vite/src/utils.ts deleted file mode 100644 index 015e0efe31..0000000000 --- a/packages/vite/src/utils.ts +++ /dev/null @@ -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 (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) -} diff --git a/packages/bridge/src/vite/utils.ts b/packages/vite/src/utils/index.ts similarity index 90% rename from packages/bridge/src/vite/utils.ts rename to packages/vite/src/utils/index.ts index 099ad124a0..15a0d7bcce 100644 --- a/packages/bridge/src/vite/utils.ts +++ b/packages/vite/src/utils/index.ts @@ -8,7 +8,7 @@ export function uniq (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) diff --git a/packages/vite/src/utils/warmup.ts b/packages/vite/src/utils/warmup.ts index a382f00250..0b45d5ae4f 100644 --- a/packages/vite/src/utils/warmup.ts +++ b/packages/vite/src/utils/warmup.ts @@ -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))) }