Skip to content

Commit

Permalink
fix(rollup): respect rollupConfig options to override default config
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed May 4, 2022
1 parent 6cdbc4a commit 43b1d02
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
24 changes: 10 additions & 14 deletions src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { watch } from 'chokidar'
import { debounce } from 'perfect-debounce'
import type { TSConfig } from 'pkg-types'
import { printFSTree } from './utils/tree'
import { getRollupConfig } from './rollup/config'
import { getRollupConfig, RollupConfig } from './rollup/config'
import { prettyPath, writeFile, isDirectory } from './utils'
import { GLOB_SCAN_PATTERN, scanHandlers } from './scan'
import type { Nitro } from './types'
Expand Down Expand Up @@ -35,9 +35,9 @@ export async function copyPublicAssets (nitro: Nitro) {
}

export async function build (nitro: Nitro) {
nitro.options.rollupConfig = getRollupConfig(nitro)
const rollupConfig = getRollupConfig(nitro)
await nitro.hooks.callHook('rollup:before', nitro)
return nitro.options.dev ? _watch(nitro) : _build(nitro)
return nitro.options.dev ? _watch(nitro, rollupConfig) : _build(nitro, rollupConfig)
}

export async function writeTypes (nitro: Nitro) {
Expand Down Expand Up @@ -129,19 +129,19 @@ async function _snapshot (nitro: Nitro) {
}))
}

async function _build (nitro: Nitro) {
async function _build (nitro: Nitro, rollupConfig: RollupConfig) {
await scanHandlers(nitro)
await writeTypes(nitro)
await _snapshot(nitro)

nitro.logger.start('Building server...')
const build = await rollup.rollup(nitro.options.rollupConfig).catch((error) => {
const build = await rollup.rollup(rollupConfig).catch((error) => {
nitro.logger.error('Rollup error: ' + error.message)
throw error
})

nitro.logger.start('Writing server bundle...')
await build.write(nitro.options.rollupConfig.output)
await build.write(rollupConfig.output)

// Write build info
const nitroConfigPath = resolve(nitro.options.output.dir, 'nitro.json')
Expand Down Expand Up @@ -172,15 +172,11 @@ async function _build (nitro: Nitro) {
if (buildInfo.commands.deploy) {
nitro.logger.success(`You can deploy this build using \`${rewriteRelativePaths(buildInfo.commands.deploy)}\``)
}

return {
entry: resolve(nitro.options.rollupConfig.output.dir, nitro.options.rollupConfig.output.entryFileNames as string)
}
}

function startRollupWatcher (nitro: Nitro) {
function startRollupWatcher (nitro: Nitro, rollupConfig: RollupConfig) {
type OT = rollup.RollupWatchOptions
const watcher = rollup.watch(defu<OT, OT>(nitro.options.rollupConfig, {
const watcher = rollup.watch(defu<OT, OT>(rollupConfig, {
watch: {
chokidar: nitro.options.watchOptions
}
Expand Down Expand Up @@ -213,13 +209,13 @@ function startRollupWatcher (nitro: Nitro) {
return watcher
}

async function _watch (nitro: Nitro) {
async function _watch (nitro: Nitro, rollupConfig: RollupConfig) {
let rollupWatcher: rollup.RollupWatcher

const reload = debounce(async () => {
if (rollupWatcher) { await rollupWatcher.close() }
await scanHandlers(nitro)
rollupWatcher = startRollupWatcher(nitro)
rollupWatcher = startRollupWatcher(nitro, rollupConfig)
await writeTypes(nitro)
})

Expand Down
16 changes: 10 additions & 6 deletions src/presets/netlify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ export const netlify = defineNitroPreset({
dir: '{{ rootDir }}/.netlify/functions-internal',
publicDir: '{{ rootDir }}/dist'
},
rollupConfig: {
output: {
entryFileNames: 'server.ts'
}
},
hooks: {
async 'compiled' (nitro: Nitro) {
const redirectsPath = join(nitro.options.output.publicDir, '_redirects')
Expand All @@ -24,9 +29,6 @@ export const netlify = defineNitroPreset({
contents = currentRedirects + '\n' + contents
}
await fsp.writeFile(redirectsPath, contents)
},
'rollup:before' (nitro: Nitro) {
nitro.options.rollupConfig.output.entryFileNames = 'server.ts'
}
}
})
Expand All @@ -45,6 +47,11 @@ export const netlifyEdge = defineNitroPreset({
serverDir: '{{ rootDir }}/.netlify/edge-functions',
publicDir: '{{ rootDir }}/dist'
},
rollupConfig: {
output: {
entryFileNames: 'server.js'
}
},
hooks: {
async 'compiled' (nitro: Nitro) {
const manifest = {
Expand All @@ -59,9 +66,6 @@ export const netlifyEdge = defineNitroPreset({
const manifestPath = join(nitro.options.rootDir, '.netlify/edge-functions/manifest.json')
await fsp.mkdir(dirname(manifestPath), { recursive: true })
await fsp.writeFile(manifestPath, JSON.stringify(manifest, null, 2))
},
'rollup:before' (nitro: Nitro) {
nitro.options.rollupConfig.output.entryFileNames = 'server.js'
}
}
})
4 changes: 2 additions & 2 deletions src/rollup/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export const getRollupConfig = (nitro: Nitro) => {
const buildServerDir = join(nitro.options.buildDir, 'dist/server')
const runtimeAppDir = join(runtimeDir, 'app')

const rollupConfig: RollupConfig = {
const rollupConfig: RollupConfig = defu<RollupConfig, RollupConfig>(nitro.options.rollupConfig, {
input: nitro.options.entry,
output: {
dir: nitro.options.output.serverDir,
Expand Down Expand Up @@ -116,7 +116,7 @@ export const getRollupConfig = (nitro: Nitro) => {
return nitro.options.moduleSideEffects.some(m => normalizedId.startsWith(m) || idWithoutNodeModules.startsWith(m))
}
}
}
})

if (nitro.options.timing) {
rollupConfig.plugins.push(timing())
Expand Down

0 comments on commit 43b1d02

Please sign in to comment.