Skip to content

Commit

Permalink
simplify dynamic import usage
Browse files Browse the repository at this point in the history
  • Loading branch information
brillout committed Feb 12, 2024
1 parent ed2ee37 commit 64cfcd8
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 37 deletions.
11 changes: 6 additions & 5 deletions vike/node/api/build.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
export { build }

import { resolveConfig } from './resolveConfig.js'
import { isVikeCli } from './isVikeCli.js'
import { _prerender } from './prerender.js'
import { build as buildVite } from 'vite'
import pc from '@brillout/picocolors'

async function build() {
const { default: pc } = await import('@brillout/picocolors')
const { build: buildVite } = await import('vite')
const { resolveConfig } = await import('./resolveConfig.js')
const { isVikeCli } = await import('./isVikeCli.js')
const { viteConfig, vikeConfigResolved, viteConfigResolved: resolvedConfig } = await resolveConfig({}, 'build')

const clientOutput = await buildVite(viteConfig).catch((error) => {
Expand Down Expand Up @@ -33,7 +35,6 @@ async function build() {
return { clientOutput, serverOutput }
}

const { _prerender } = await import('./prerender.js')
await _prerender({
viteConfig
})
Expand Down
17 changes: 13 additions & 4 deletions vike/node/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
export { build } from './build.js'
export { serve } from './serve.js'
export { prerender } from './prerender.js'
export { preview } from './preview.js'
export { build_ as build }
export { serve_ as serve }
export { preview_ as preview }
export { prerender_ as prerender }

import type { build } from './build.js'
import type { serve } from './serve.js'
import type { preview } from './preview.js'
import type { prerender } from './prerender.js'
import { assertIsNotBrowser } from '../../utils/assertIsNotBrowser.js'
assertIsNotBrowser()

const build_: typeof build = async (...args) => (await import('./build.js')).build(...args)
const serve_: typeof serve = async (...args) => (await import('./serve.js')).serve(...args)
const preview_: typeof preview = async (...args) => (await import('./preview.js')).preview(...args)
const prerender_: typeof prerender = async (...args) => (await import('./prerender.js')).prerender(...args)
7 changes: 3 additions & 4 deletions vike/node/api/prerender.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
export { prerender }
export { _prerender }

import { isVikeCli } from './isVikeCli.js'
import { runPrerenderFromAPI, runPrerender_forceExit, runPrerenderFromCLI } from '../prerender/runPrerender.js'
import { resolveConfig } from './resolveConfig.js'
import type { PrerenderOptions } from '../prerender/runPrerender.js'

// only called programatically on user-land or by running vike prerender
// adds the vike plugin if not present
async function prerender(options: PrerenderOptions = {}) {
const { resolveConfig } = await import('./resolveConfig.js')
const { viteConfig = {}, pageContextInit, onPagePrerender } = options
const resolved = await resolveConfig(viteConfig, 'build')
return _prerender({ viteConfig: resolved.viteConfig, pageContextInit, onPagePrerender })
}

async function _prerender(options: PrerenderOptions) {
const { pageContextInit, onPagePrerender, viteConfig } = options
const { isVikeCli } = await import('./isVikeCli.js')
if (isVikeCli) {
const { runPrerender_forceExit, runPrerenderFromCLI } = await import('../prerender/runPrerender.js')
await runPrerenderFromCLI({ pageContextInit, onPagePrerender, viteConfig })
runPrerender_forceExit()
} else {
const { runPrerenderFromAPI } = await import('../prerender/runPrerender.js')
await runPrerenderFromAPI({
pageContextInit,
onPagePrerender,
Expand Down
9 changes: 5 additions & 4 deletions vike/node/api/preview.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
export { preview }

import { resolveConfig } from './resolveConfig.js'
import { isVikeCli } from './isVikeCli.js'
import { preview as previewVite } from 'vite'
import pc from '@brillout/picocolors'

async function preview() {
const { preview: previewVite } = await import('vite')
const { resolveConfig } = await import('./resolveConfig.js')
const { isVikeCli } = await import('./isVikeCli.js')
// Adds vike to viteConfig if not present
const { viteConfig, viteConfigResolved: resolvedConfig } = await resolveConfig({}, 'preview')
if (!isVikeCli) return previewVite(viteConfig)

const { default: pc } = await import('@brillout/picocolors')
try {
const server = await previewVite(viteConfig)
server.printUrls()
Expand Down
8 changes: 4 additions & 4 deletions vike/node/api/resolveConfig.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
export { resolveConfig }

import { resolveConfig as resolveViteConfig } from 'vite'
import { getConfigVike } from '../shared/getConfigVike.js'
import pc from '@brillout/picocolors'
import type { InlineConfig } from 'vite'

async function resolveConfig(viteConfig: InlineConfig, command: 'build' | 'serve' | 'preview') {
const { default: pc } = await import('@brillout/picocolors')
const { resolveConfig: resolveViteConfig } = await import('vite')

//TODO: do we need this?
let nodeEnv = 'development'
if (['build', 'preview'].includes(command)) {
Expand All @@ -27,6 +27,7 @@ async function resolveConfig(viteConfig: InlineConfig, command: 'build' | 'serve

// Add vike to plugins if not present
if (!viteConfigResolved.plugins.some((p) => p.name.startsWith('vike:'))) {
// We using a dynamic import because the script calling the VIke API may not live in the same place as vite.config.js, thus have vike/plugin may resolved to two different node_modules/vike directories
const { plugin } = await import('../plugin/index.js')

viteConfig ??= {}
Expand All @@ -36,7 +37,6 @@ async function resolveConfig(viteConfig: InlineConfig, command: 'build' | 'serve
return resolveConfig(viteConfig, command)
}

const { getConfigVike } = await import('../shared/getConfigVike.js')
const vikeConfigResolved = await getConfigVike(viteConfigResolved)

//TODO: add vite plugins from extension to viteConfig.plugins
Expand Down
20 changes: 11 additions & 9 deletions vike/node/api/serve.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
export { serve }

import { resolveConfig } from './resolveConfig.js'
import { isVikeCli } from './isVikeCli.js'
import { startTime } from '../cli/bin.js'
import { projectInfo } from '../../utils/projectInfo.js'
import { createServer } from 'vite'
import pc from '@brillout/picocolors'

const { projectName, projectVersion } = projectInfo

async function serve() {
const { createServer: createServerVite } = await import('vite')
const { resolveConfig } = await import('./resolveConfig.js')
const { isVikeCli } = await import('./isVikeCli.js')
// Adds vike to viteConfig if not present
const { viteConfig, viteConfigResolved: resolvedConfig } = await resolveConfig({}, 'serve')
if (!isVikeCli) return createServerVite(viteConfig)
if (!isVikeCli) return createServer(viteConfig)

const { default: pc } = await import('@brillout/picocolors')
const { startTime } = await import('../cli/bin.js')
const { projectInfo } = await import('../../utils/projectInfo.js')
const { projectName, projectVersion } = projectInfo
try {
const server = await createServerVite(viteConfig)
const server = await createServer(viteConfig)
await server.listen()
const info = server.config.logger.info
const startupDurationString = pc.dim(
Expand Down
11 changes: 4 additions & 7 deletions vike/node/cli/bin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { cac } from 'cac'
import { projectInfo, assertUsage, assertIsNotBrowser } from './utils.js'
import { setIsVikeCli } from '../api/isVikeCli.js'
import { serve, build, preview, prerender } from '../api/index.js'
assertIsNotBrowser()

setIsVikeCli()
Expand All @@ -9,7 +10,6 @@ const cli = cac(projectInfo.projectName)
export const startTime = performance.now()

cli.command('prerender', 'Pre-render the HTML of your pages').action(async () => {
const { prerender } = await import('../api/prerender.js')
await prerender()
})

Expand All @@ -18,18 +18,15 @@ cli
.alias('serve')
.alias('dev')
.action(async () => {
const { serve } = await import('../api/serve.js')
return serve()
await serve()
})

cli.command('build', 'Build for production').action(async () => {
const { build } = await import('../api/build.js')
return build()
await build()
})

cli.command('preview', 'Start a preview server using production build').action(async () => {
const { preview } = await import('../api/preview.js')
return preview()
await preview()
})

// Listen to unknown commands
Expand Down

0 comments on commit 64cfcd8

Please sign in to comment.