Nuxt/packages/nitro/src/rollup/config.ts

293 lines
9.7 KiB
TypeScript
Raw Normal View History

2020-11-20 00:16:31 +00:00
import { dirname, join, relative, resolve } from 'upath'
2020-11-01 23:17:44 +00:00
import { InputOptions, OutputOptions } from 'rollup'
import defu from 'defu'
2020-11-01 23:17:44 +00:00
import { terser } from 'rollup-plugin-terser'
import commonjs from '@rollup/plugin-commonjs'
2020-11-05 15:36:31 +00:00
import nodeResolve from '@rollup/plugin-node-resolve'
2020-11-01 23:17:44 +00:00
import alias from '@rollup/plugin-alias'
import json from '@rollup/plugin-json'
import replace from '@rollup/plugin-replace'
import virtual from '@rollup/plugin-virtual'
2020-11-13 16:14:17 +00:00
import inject from '@rollup/plugin-inject'
2020-11-01 23:17:44 +00:00
import analyze from 'rollup-plugin-analyzer'
import * as unenv from 'unenv'
import type { Preset } from 'unenv'
2021-01-22 19:55:59 +00:00
import { NitroContext } from '../context'
import { resolvePath, MODULE_DIR } from '../utils'
2020-11-15 00:52:18 +00:00
2020-11-28 22:49:39 +00:00
import { dynamicRequire } from './plugins/dynamic-require'
import { externals } from './plugins/externals'
import { timing } from './plugins/timing'
2021-05-24 11:25:34 +00:00
// import { autoMock } from './plugins/automock'
import { staticAssets, dirnames } from './plugins/static'
2021-04-12 21:28:48 +00:00
import { assets } from './plugins/assets'
2020-11-28 22:49:39 +00:00
import { middleware } from './plugins/middleware'
import { esbuild } from './plugins/esbuild'
2021-04-11 18:20:41 +00:00
import { raw } from './plugins/raw'
2021-04-11 21:22:02 +00:00
import { storage } from './plugins/storage'
2020-11-01 23:17:44 +00:00
export type RollupConfig = InputOptions & { output: OutputOptions }
2021-01-22 19:55:59 +00:00
export const getRollupConfig = (nitroContext: NitroContext) => {
2020-11-21 11:42:02 +00:00
const extensions: string[] = ['.ts', '.mjs', '.js', '.json', '.node']
2020-11-01 23:17:44 +00:00
const nodePreset = nitroContext.node === false ? unenv.nodeless : unenv.node
2020-11-13 16:14:17 +00:00
2020-11-21 11:42:02 +00:00
const builtinPreset: Preset = {
2020-11-20 00:16:31 +00:00
alias: {
2020-11-21 11:42:02 +00:00
// General
debug: 'unenv/runtime/npm/debug',
consola: 'unenv/runtime/npm/consola',
2020-11-21 11:42:02 +00:00
// Vue 2
encoding: 'unenv/runtime/mock/proxy',
he: 'unenv/runtime/mock/proxy',
resolve: 'unenv/runtime/mock/proxy',
'source-map': 'unenv/runtime/mock/proxy',
'lodash.template': 'unenv/runtime/mock/proxy',
'serialize-javascript': 'unenv/runtime/mock/proxy',
2020-11-21 11:42:02 +00:00
// Vue 3
'estree-walker': 'unenv/runtime/mock/proxy',
'@babel/parser': 'unenv/runtime/mock/proxy',
'@vue/compiler-core': 'unenv/runtime/mock/proxy',
'@vue/compiler-dom': 'unenv/runtime/mock/proxy',
'@vue/compiler-ssr': 'unenv/runtime/mock/proxy'
2020-11-20 00:16:31 +00:00
}
2020-11-21 11:42:02 +00:00
}
const env = unenv.env(nodePreset, builtinPreset, nitroContext.env)
2020-12-07 12:36:43 +00:00
delete env.alias['node-fetch'] // FIX ME
2021-01-22 19:55:59 +00:00
if (nitroContext.sourceMap) {
2020-12-07 12:36:43 +00:00
env.polyfill.push('source-map-support/register')
}
2020-11-20 00:16:31 +00:00
2021-01-22 19:55:59 +00:00
const buildServerDir = join(nitroContext._nuxt.buildDir, 'dist/server')
const runtimeAppDir = join(nitroContext._internal.runtimeDir, 'app')
const rollupConfig: RollupConfig = {
2021-01-22 19:55:59 +00:00
input: resolvePath(nitroContext, nitroContext.entry),
2020-11-01 23:17:44 +00:00
output: {
2021-01-22 19:55:59 +00:00
dir: nitroContext.output.serverDir,
2020-11-20 00:16:31 +00:00
entryFileNames: 'index.js',
2020-11-15 01:31:50 +00:00
chunkFileNames (chunkInfo) {
let prefix = ''
const modules = Object.keys(chunkInfo.modules)
const lastModule = modules[modules.length - 1]
2020-11-20 00:16:31 +00:00
if (lastModule.startsWith(buildServerDir)) {
prefix = join('app', relative(buildServerDir, dirname(lastModule)))
} else if (lastModule.startsWith(runtimeAppDir)) {
prefix = 'app'
2021-01-22 19:55:59 +00:00
} else if (lastModule.startsWith(nitroContext._nuxt.buildDir)) {
2020-11-20 00:16:31 +00:00
prefix = 'nuxt'
2021-01-22 19:55:59 +00:00
} else if (lastModule.startsWith(nitroContext._internal.runtimeDir)) {
prefix = 'nitro'
2021-04-12 21:28:48 +00:00
} else if (!prefix && nitroContext.middleware.find(m => lastModule.startsWith(m.handle as string))) {
2020-11-15 01:31:50 +00:00
prefix = 'middleware'
2021-04-12 21:28:48 +00:00
} else if (lastModule.includes('assets')) {
prefix = 'assets'
2020-11-15 01:31:50 +00:00
}
2020-11-20 00:16:31 +00:00
return join('chunks', prefix, '[name].js')
2020-11-15 01:31:50 +00:00
},
inlineDynamicImports: nitroContext.inlineDynamicImports,
2020-11-01 23:17:44 +00:00
format: 'cjs',
exports: 'auto',
2020-11-01 23:17:44 +00:00
intro: '',
outro: '',
2020-12-07 12:36:43 +00:00
preferConst: true,
2021-01-22 19:55:59 +00:00
sourcemap: nitroContext.sourceMap,
2020-12-07 12:36:43 +00:00
sourcemapExcludeSources: true,
sourcemapPathTransform (relativePath, sourcemapPath) {
return resolve(dirname(sourcemapPath), relativePath)
}
2020-11-01 23:17:44 +00:00
},
2020-11-21 11:42:02 +00:00
external: env.external,
// https://github.com/rollup/rollup/pull/4021#issuecomment-809985618
// https://github.com/nuxt/framework/issues/160
makeAbsoluteExternalsRelative: false,
2020-11-20 21:04:48 +00:00
plugins: [],
onwarn (warning, rollupWarn) {
2021-04-20 13:22:42 +00:00
if (
!['CIRCULAR_DEPENDENCY', 'EVAL'].includes(warning.code) &&
!warning.message.includes('Unsupported source map comment')
) {
2020-11-20 21:04:48 +00:00
rollupWarn(warning)
}
}
2020-11-01 23:17:44 +00:00
}
2021-01-22 19:55:59 +00:00
if (nitroContext.timing) {
2020-11-15 00:52:18 +00:00
rollupConfig.plugins.push(timing())
2020-11-02 14:42:27 +00:00
}
2021-04-11 18:20:41 +00:00
// Raw asset loader
rollupConfig.plugins.push(raw())
2020-11-02 00:31:43 +00:00
// https://github.com/rollup/plugins/tree/master/packages/replace
rollupConfig.plugins.push(replace({
2021-02-22 10:31:45 +00:00
// @ts-ignore https://github.com/rollup/plugins/pull/810
preventAssignment: true,
2020-11-02 00:31:43 +00:00
values: {
2021-01-22 19:55:59 +00:00
'process.env.NODE_ENV': nitroContext._nuxt.dev ? '"development"' : '"production"',
2020-11-05 18:53:17 +00:00
'typeof window': '"undefined"',
2021-04-21 15:35:20 +00:00
'global.': 'globalThis.',
'process.server': 'true',
'process.client': 'false',
2021-01-22 19:55:59 +00:00
'process.env.ROUTER_BASE': JSON.stringify(nitroContext._nuxt.routerBase),
'process.env.PUBLIC_PATH': JSON.stringify(nitroContext._nuxt.publicPath),
'process.env.NUXT_STATIC_BASE': JSON.stringify(nitroContext._nuxt.staticAssets.base),
'process.env.NUXT_STATIC_VERSION': JSON.stringify(nitroContext._nuxt.staticAssets.version),
'process.env.NUXT_FULL_STATIC': nitroContext._nuxt.fullStatic as unknown as string,
'process.env.NITRO_PRESET': JSON.stringify(nitroContext.preset),
'process.env.RUNTIME_CONFIG': JSON.stringify(nitroContext._nuxt.runtimeConfig),
'process.env.DEBUG': JSON.stringify(nitroContext._nuxt.dev)
2020-11-02 00:31:43 +00:00
}
}))
2020-12-07 12:36:43 +00:00
// ESBuild
rollupConfig.plugins.push(esbuild({
target: 'es2019',
2020-12-07 12:36:43 +00:00
sourceMap: true
}))
2020-11-03 19:55:36 +00:00
// Dynamic Require Support
rollupConfig.plugins.push(dynamicRequire({
2021-01-22 19:55:59 +00:00
dir: resolve(nitroContext._nuxt.buildDir, 'dist/server'),
inline: nitroContext.node === false || nitroContext.inlineDynamicImports,
2020-11-03 19:55:36 +00:00
globbyOptions: {
ignore: [
'server.js'
]
}
}))
2020-11-02 13:11:26 +00:00
2021-04-12 21:28:48 +00:00
// Assets
rollupConfig.plugins.push(assets(nitroContext.assets))
2020-11-28 22:49:39 +00:00
// Static
2021-04-12 21:28:48 +00:00
// TODO: use assets plugin
2021-01-22 19:55:59 +00:00
if (nitroContext.serveStatic) {
rollupConfig.plugins.push(dirnames())
2021-01-22 19:55:59 +00:00
rollupConfig.plugins.push(staticAssets(nitroContext))
2020-11-28 22:49:39 +00:00
}
2021-04-11 21:22:02 +00:00
// Storage
rollupConfig.plugins.push(storage(nitroContext.storage))
2020-11-28 22:49:39 +00:00
// Middleware
rollupConfig.plugins.push(middleware(() => {
const _middleware = [
...nitroContext.scannedMiddleware,
...nitroContext.middleware
]
if (nitroContext.serveStatic) {
_middleware.unshift({ route: '/', handle: '#nitro/server/static' })
}
return _middleware
}))
2020-11-20 00:16:31 +00:00
// Polyfill
rollupConfig.plugins.push(virtual({
'#polyfill': env.polyfill.map(p => `import '${p}';`).join('\n')
2020-11-20 00:16:31 +00:00
}))
2020-11-02 00:31:43 +00:00
// https://github.com/rollup/plugins/tree/master/packages/alias
2021-01-22 19:55:59 +00:00
const renderer = nitroContext.renderer || (nitroContext._nuxt.majorVersion === 3 ? 'vue3' : 'vue2')
const vue2ServerRenderer = 'vue-server-renderer/' + (nitroContext._nuxt.dev ? 'build.dev.js' : 'build.prod.js')
rollupConfig.plugins.push(alias({
2020-11-02 00:31:43 +00:00
entries: {
'#nitro': nitroContext._internal.runtimeDir,
'#nitro-renderer': require.resolve(resolve(nitroContext._internal.runtimeDir, 'app', renderer)),
2021-06-04 20:12:32 +00:00
'#config': require.resolve(resolve(nitroContext._internal.runtimeDir, 'app/config')),
'#nitro-vue-renderer': vue2ServerRenderer,
'#build': nitroContext._nuxt.buildDir,
'~': nitroContext._nuxt.srcDir,
'@/': nitroContext._nuxt.srcDir,
'~~': nitroContext._nuxt.rootDir,
'@@/': nitroContext._nuxt.rootDir,
2020-11-20 00:16:31 +00:00
...env.alias
2020-11-02 00:31:43 +00:00
}
}))
const moduleDirectories = [
2021-01-22 19:55:59 +00:00
resolve(nitroContext._nuxt.rootDir, 'node_modules'),
resolve(MODULE_DIR, 'node_modules'),
resolve(MODULE_DIR, '../node_modules'),
'node_modules'
]
// Externals Plugin
2021-01-22 19:55:59 +00:00
if (nitroContext.externals) {
rollupConfig.plugins.push(externals(defu(nitroContext.externals as any, {
outDir: nitroContext.output.serverDir,
moduleDirectories,
external: [
...(nitroContext._nuxt.dev ? [nitroContext._nuxt.buildDir] : [])
],
inline: [
'#',
'~',
'@/',
'~~',
'@@/',
'virtual:',
2021-01-22 19:55:59 +00:00
nitroContext._internal.runtimeDir,
nitroContext._nuxt.srcDir,
nitroContext._nuxt.rootDir,
nitroContext._nuxt.serverDir,
...nitroContext.middleware.map(m => m.handle),
...(nitroContext._nuxt.dev ? [] : ['vue', '@vue/', '@nuxt/'])
],
traceOptions: {
base: '/',
processCwd: nitroContext._nuxt.rootDir
}
})))
}
2020-11-02 00:31:43 +00:00
// https://github.com/rollup/plugins/tree/master/packages/node-resolve
rollupConfig.plugins.push(nodeResolve({
2020-11-02 00:31:43 +00:00
extensions,
preferBuiltins: true,
2021-01-22 19:55:59 +00:00
rootDir: nitroContext._nuxt.rootDir,
moduleDirectories,
2020-11-02 00:31:43 +00:00
mainFields: ['main'] // Force resolve CJS (@vue/runtime-core ssrUtils)
}))
// Automatically mock unresolved externals
2021-05-24 11:25:34 +00:00
// rollupConfig.plugins.push(autoMock())
2020-11-02 00:31:43 +00:00
// https://github.com/rollup/plugins/tree/master/packages/commonjs
rollupConfig.plugins.push(commonjs({
requireReturnsDefault: 'auto'
2020-11-02 00:31:43 +00:00
}))
// https://github.com/rollup/plugins/tree/master/packages/json
rollupConfig.plugins.push(json())
2020-11-02 00:31:43 +00:00
// https://github.com/rollup/plugins/tree/master/packages/inject
2020-11-20 00:16:31 +00:00
rollupConfig.plugins.push(inject(env.inject))
2021-01-22 19:55:59 +00:00
if (nitroContext.analyze) {
2020-11-01 23:17:44 +00:00
// https://github.com/doesdev/rollup-plugin-analyzer
rollupConfig.plugins.push(analyze())
2020-11-01 23:17:44 +00:00
}
// https://github.com/TrySound/rollup-plugin-terser
2021-01-22 19:55:59 +00:00
// https://github.com/terser/terser#minify-nitroContext
if (nitroContext.minify) {
rollupConfig.plugins.push(terser({
mangle: {
keep_fnames: true,
keep_classnames: true
},
format: {
comments: false
}
}))
2020-11-01 23:17:44 +00:00
}
return rollupConfig
2020-11-01 23:17:44 +00:00
}