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

241 lines
8.0 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'
2020-11-21 11:42:02 +00:00
import type { Preset } from '@nuxt/un'
2020-11-20 00:16:31 +00:00
import * as un from '@nuxt/un'
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'
2020-12-07 12:53:32 +00:00
import { autoMock } from './plugins/automock'
import { staticAssets, dirnames } from './plugins/static'
2020-11-28 22:49:39 +00:00
import { middleware } from './plugins/middleware'
import { esbuild } from './plugins/esbuild'
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
2021-01-22 19:55:59 +00:00
const nodePreset = nitroContext.node === false ? un.nodeless : un.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
2020-11-22 21:54:08 +00:00
depd: 'un/npm/depd',
2020-11-21 11:42:02 +00:00
// Vue 2
encoding: 'un/mock/proxy',
he: 'un/mock/proxy',
resolve: 'un/mock/proxy',
'source-map': 'un/mock/proxy',
'lodash.template': 'un/mock/proxy',
'serialize-javascript': 'un/mock/proxy',
// Vue 3
'@babel/parser': 'un/mock/proxy',
'@vue/compiler-core': 'un/mock/proxy',
'@vue/compiler-dom': 'un/mock/proxy',
'@vue/compiler-ssr': 'un/mock/proxy'
2020-11-20 00:16:31 +00:00
}
2020-11-21 11:42:02 +00:00
}
2021-01-22 19:55:59 +00:00
const env = un.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'
} else if (!prefix && nitroContext.middleware.find(m => lastModule.startsWith(m.handle))) {
2020-11-15 01:31:50 +00:00
prefix = 'middleware'
}
2020-11-20 00:16:31 +00:00
return join('chunks', prefix, '[name].js')
2020-11-15 01:31:50 +00:00
},
2021-01-22 19:55:59 +00:00
inlineDynamicImports: nitroContext.inlineChunks,
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,
2020-11-20 21:04:48 +00:00
plugins: [],
onwarn (warning, rollupWarn) {
if (!['CIRCULAR_DEPENDENCY', 'EVAL'].includes(warning.code)) {
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
}
2020-11-02 00:31:43 +00:00
// https://github.com/rollup/plugins/tree/master/packages/replace
rollupConfig.plugins.push(replace({
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-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({
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.inlineChunks,
2020-11-03 19:55:36 +00:00
globbyOptions: {
ignore: [
'server.js'
]
}
}))
2020-11-02 13:11:26 +00:00
2020-11-28 22:49:39 +00:00
// Static
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
}
2020-11-28 22:49:39 +00:00
// Middleware
2021-01-22 19:55:59 +00:00
const _middleware = [...nitroContext.middleware]
if (nitroContext.serveStatic) {
2020-11-28 22:49:39 +00:00
_middleware.unshift({ route: '/', handle: '~runtime/server/static' })
}
rollupConfig.plugins.push(middleware(_middleware))
2020-11-20 00:16:31 +00:00
// Polyfill
rollupConfig.plugins.push(virtual({
2020-11-20 12:14:16 +00:00
'~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: {
2021-01-22 19:55:59 +00:00
'~runtime': nitroContext._internal.runtimeDir,
'~renderer': require.resolve(resolve(nitroContext._internal.runtimeDir, 'app', renderer)),
2021-01-18 11:42:00 +00:00
'~vueServerRenderer': vue2ServerRenderer,
2021-01-22 19:55:59 +00:00
'~build': nitroContext._nuxt.buildDir,
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,
ignore: [
2021-01-22 19:55:59 +00:00
nitroContext._internal.runtimeDir,
...(nitroContext._nuxt.dev ? [] : [nitroContext._nuxt.buildDir]),
...nitroContext.middleware.map(m => m.handle)
],
traceOptions: {
2021-01-22 19:55:59 +00:00
base: 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
2020-12-07 12:53:32 +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({
2020-11-02 13:11:26 +00:00
extensions: extensions.filter(ext => ext !== '.json')
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
}