Skip to content

Commit

Permalink
perf: lazyload large dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
hemengke1997 committed Nov 27, 2023
1 parent bf1306d commit 4920649
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
7 changes: 4 additions & 3 deletions src/node/build/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import createDebug from 'debug'
import { type BuildResult, type Plugin, build as esbuild } from 'esbuild'
import { resolveToEsbuildTarget } from 'esbuild-plugin-browserslist'
import { type BuildResult, type Plugin } from 'esbuild'
import path from 'node:path'
import colors from 'picocolors'
import { globalConfig } from '../global-config'
Expand Down Expand Up @@ -62,6 +61,7 @@ export async function esbuildTypescript(buildOptions: IBuildOptions) {
if (enableBabel) {
const { default: browserslist } = await import('browserslist')
babelTarget = browserslist.loadConfig({ path: viteConfig.root }) || []
const { resolveToEsbuildTarget } = await import('esbuild-plugin-browserslist')
esbuildTarget = resolveToEsbuildTarget(browserslist(babelTarget), { printUnknownTargets: false })
}

Expand All @@ -83,7 +83,8 @@ export async function esbuildTypescript(buildOptions: IBuildOptions) {
let res: BuildResult

try {
res = await esbuild({
const { build } = await import('esbuild')
res = await build({
bundle: true,
define,
entryPoints: [filePath],
Expand Down
4 changes: 2 additions & 2 deletions src/node/helper/file-watcher.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import createDebug from 'debug'
import path from 'node:path'
import colors from 'picocolors'
import Watcher from 'watcher'
import { build } from '../build'
import { globalConfig } from '../global-config'
import { type HmrFile } from './server'
Expand Down Expand Up @@ -39,8 +38,9 @@ async function handleFileChange(filePath: string, cb?: () => void) {
handleFileAdded(filePath, cb)
}

export function initWatcher(cb: (file: HmrFile) => void) {
export async function initWatcher(cb: (file: HmrFile) => void) {
try {
const { default: Watcher } = await import('watcher')
const watcher = new Watcher(globalConfig.get('absInputDir'), {
debounce: 0,
ignoreInitial: true,
Expand Down
4 changes: 2 additions & 2 deletions src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,12 @@ export default function publicTypescript(options: VPPTPluginOptions = {}) {

await setupManifestCache(viteConfig, opts)
},
configureServer(server) {
async configureServer(server) {
const { ws } = server

globalConfig.set('viteDevServer', server)

initWatcher((file) => reloadPage(ws, file))
await initWatcher((file) => reloadPage(ws, file))
},
async buildStart() {
const manifestPath = manifestCache.manifestPath
Expand Down

0 comments on commit 4920649

Please sign in to comment.