2021-07-12 10:54:24 +00:00
|
|
|
import { dirname, join, normalize, relative, resolve } from 'upath'
|
2020-11-01 23:17:44 +00:00
|
|
|
import { InputOptions, OutputOptions } from 'rollup'
|
2020-12-07 21:59:24 +00:00
|
|
|
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'
|
2020-11-12 18:20:55 +00:00
|
|
|
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'
|
2021-04-21 15:10:53 +00:00
|
|
|
import * as unenv from 'unenv'
|
2020-11-05 12:26:00 +00:00
|
|
|
|
2021-05-24 11:14:10 +00:00
|
|
|
import type { Preset } from 'unenv'
|
2021-01-22 19:55:59 +00:00
|
|
|
import { NitroContext } from '../context'
|
2020-11-14 13:05:09 +00:00
|
|
|
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'
|
2020-12-01 23:28:42 +00:00
|
|
|
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
|
|
|
|
2021-04-21 15:10:53 +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
|
2021-04-21 15:10:53 +00:00
|
|
|
debug: 'unenv/runtime/npm/debug',
|
|
|
|
consola: 'unenv/runtime/npm/consola',
|
2020-11-21 11:42:02 +00:00
|
|
|
// Vue 2
|
2021-04-21 15:10:53 +00:00
|
|
|
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
|
2021-04-21 15:10:53 +00:00
|
|
|
'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
|
|
|
}
|
|
|
|
|
2021-04-21 15:10:53 +00:00
|
|
|
const env = unenv.env(nodePreset, builtinPreset, nitroContext.env)
|
2020-12-07 12:36:43 +00:00
|
|
|
|
2020-12-07 21:59:24 +00:00
|
|
|
delete env.alias['node-fetch'] // FIX ME
|
|
|
|
|
2021-01-22 19:55:59 +00:00
|
|
|
if (nitroContext.sourceMap) {
|
2021-07-15 09:38:06 +00:00
|
|
|
env.polyfill.push('source-map-support/register.js')
|
2020-12-07 12:36:43 +00:00
|
|
|
}
|
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')
|
2020-11-14 13:33:31 +00:00
|
|
|
|
2020-11-14 13:05:09 +00:00
|
|
|
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,
|
2021-07-15 09:38:06 +00:00
|
|
|
entryFileNames: 'index.mjs',
|
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
|
|
|
}
|
2021-07-15 09:38:06 +00:00
|
|
|
return join('chunks', prefix, '[name].mjs')
|
2020-11-15 01:31:50 +00:00
|
|
|
},
|
2021-02-22 12:12:59 +00:00
|
|
|
inlineDynamicImports: nitroContext.inlineDynamicImports,
|
2021-07-15 09:38:06 +00:00
|
|
|
format: 'esm',
|
2020-11-14 13:05:09 +00:00
|
|
|
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,
|
2021-04-12 22:08:18 +00:00
|
|
|
// https://github.com/rollup/rollup/pull/4021#issuecomment-809985618
|
2021-05-24 11:21:37 +00:00
|
|
|
// 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)
|
|
|
|
}
|
2021-07-26 11:32:35 +00:00
|
|
|
},
|
|
|
|
treeshake: {
|
|
|
|
moduleSideEffects (id) {
|
|
|
|
return nitroContext.moduleSideEffects.some(match => id.startsWith(match))
|
|
|
|
}
|
2020-11-20 21:04:48 +00:00
|
|
|
}
|
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
|
2020-11-14 13:05:09 +00:00
|
|
|
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.',
|
2021-06-14 18:37:35 +00:00
|
|
|
'process.server': 'true',
|
|
|
|
'process.client': 'false',
|
2021-07-21 20:05:22 +00:00
|
|
|
'process.env.NUXT_NO_SSR': JSON.stringify(!nitroContext._nuxt.ssr),
|
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({
|
2021-06-14 09:03:44 +00:00
|
|
|
target: 'es2019',
|
2020-12-07 12:36:43 +00:00
|
|
|
sourceMap: true
|
|
|
|
}))
|
2020-11-28 21:11:14 +00:00
|
|
|
|
2020-11-03 19:55:36 +00:00
|
|
|
// Dynamic Require Support
|
2020-11-14 13:05:09 +00:00
|
|
|
rollupConfig.plugins.push(dynamicRequire({
|
2021-01-22 19:55:59 +00:00
|
|
|
dir: resolve(nitroContext._nuxt.buildDir, 'dist/server'),
|
2021-02-22 12:12:59 +00:00
|
|
|
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) {
|
2020-12-01 23:28:42 +00:00
|
|
|
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-12 18:20:55 +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
|
2021-02-18 16:06:58 +00:00
|
|
|
rollupConfig.plugins.push(middleware(() => {
|
|
|
|
const _middleware = [
|
|
|
|
...nitroContext.scannedMiddleware,
|
|
|
|
...nitroContext.middleware
|
|
|
|
]
|
|
|
|
if (nitroContext.serveStatic) {
|
2021-04-20 11:03:18 +00:00
|
|
|
_middleware.unshift({ route: '/', handle: '#nitro/server/static' })
|
2021-02-18 16:06:58 +00:00
|
|
|
}
|
|
|
|
return _middleware
|
|
|
|
}))
|
2020-11-12 18:20:55 +00:00
|
|
|
|
2020-11-20 00:16:31 +00:00
|
|
|
// Polyfill
|
|
|
|
rollupConfig.plugins.push(virtual({
|
2021-06-18 09:54:35 +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')
|
2020-11-14 13:05:09 +00:00
|
|
|
rollupConfig.plugins.push(alias({
|
2020-11-02 00:31:43 +00:00
|
|
|
entries: {
|
2021-04-20 11:03:18 +00:00
|
|
|
'#nitro': nitroContext._internal.runtimeDir,
|
2021-07-12 10:54:24 +00:00
|
|
|
'#nitro-renderer': normalize(require.resolve(resolve(nitroContext._internal.runtimeDir, 'app', renderer))),
|
|
|
|
'#config': normalize(require.resolve(resolve(nitroContext._internal.runtimeDir, 'app/config'))),
|
2021-04-20 11:03:18 +00:00
|
|
|
'#nitro-vue-renderer': vue2ServerRenderer,
|
|
|
|
'#build': nitroContext._nuxt.buildDir,
|
2021-06-04 20:04:08 +00:00
|
|
|
'~': 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
|
|
|
}
|
|
|
|
}))
|
|
|
|
|
2020-12-07 21:59:24 +00:00
|
|
|
const moduleDirectories = [
|
2021-01-22 19:55:59 +00:00
|
|
|
resolve(nitroContext._nuxt.rootDir, 'node_modules'),
|
2020-12-07 21:59:24 +00:00
|
|
|
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,
|
2020-12-07 21:59:24 +00:00
|
|
|
moduleDirectories,
|
2021-06-04 20:04:08 +00:00
|
|
|
external: [
|
|
|
|
...(nitroContext._nuxt.dev ? [nitroContext._nuxt.buildDir] : [])
|
|
|
|
],
|
|
|
|
inline: [
|
|
|
|
'#',
|
|
|
|
'~',
|
|
|
|
'@/',
|
|
|
|
'~~',
|
|
|
|
'@@/',
|
|
|
|
'virtual:',
|
2021-07-15 09:38:06 +00:00
|
|
|
'ohmyfetch', // TODO: Webpack externals forces default import!
|
2021-01-22 19:55:59 +00:00
|
|
|
nitroContext._internal.runtimeDir,
|
2021-06-04 20:04:08 +00:00
|
|
|
nitroContext._nuxt.srcDir,
|
|
|
|
nitroContext._nuxt.rootDir,
|
2021-04-12 22:06:27 +00:00
|
|
|
nitroContext._nuxt.serverDir,
|
2021-06-04 20:04:08 +00:00
|
|
|
...nitroContext.middleware.map(m => m.handle),
|
|
|
|
...(nitroContext._nuxt.dev ? [] : ['vue', '@vue/', '@nuxt/'])
|
2020-12-07 21:59:24 +00:00
|
|
|
],
|
|
|
|
traceOptions: {
|
2021-04-19 20:41:02 +00:00
|
|
|
base: '/',
|
2021-07-15 09:38:06 +00:00
|
|
|
processCwd: nitroContext._nuxt.rootDir,
|
|
|
|
exportsOnly: true
|
2020-12-07 21:59:24 +00:00
|
|
|
}
|
|
|
|
})))
|
2020-11-14 13:05:09 +00:00
|
|
|
}
|
|
|
|
|
2020-11-02 00:31:43 +00:00
|
|
|
// https://github.com/rollup/plugins/tree/master/packages/node-resolve
|
2020-11-14 13:05:09 +00:00
|
|
|
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,
|
2020-12-07 21:59:24 +00:00
|
|
|
moduleDirectories,
|
2021-07-15 09:38:06 +00:00
|
|
|
// 'module' is intentionally not supported because of externals
|
|
|
|
mainFields: ['main'],
|
|
|
|
exportConditions: [
|
|
|
|
'default',
|
|
|
|
'module',
|
|
|
|
'import'
|
|
|
|
]
|
2020-11-02 00:31:43 +00:00
|
|
|
}))
|
|
|
|
|
2020-11-20 21:08:57 +00:00
|
|
|
// Automatically mock unresolved externals
|
2021-05-24 11:25:34 +00:00
|
|
|
// rollupConfig.plugins.push(autoMock())
|
2020-11-20 21:08:57 +00:00
|
|
|
|
2020-11-02 00:31:43 +00:00
|
|
|
// https://github.com/rollup/plugins/tree/master/packages/commonjs
|
2020-11-14 13:05:09 +00:00
|
|
|
rollupConfig.plugins.push(commonjs({
|
2021-07-28 07:58:07 +00:00
|
|
|
esmExternals: id => !id.startsWith('unenv/'),
|
2021-04-29 13:00:31 +00:00
|
|
|
requireReturnsDefault: 'auto'
|
2020-11-02 00:31:43 +00:00
|
|
|
}))
|
|
|
|
|
|
|
|
// https://github.com/rollup/plugins/tree/master/packages/json
|
2020-11-14 13:05:09 +00:00
|
|
|
rollupConfig.plugins.push(json())
|
2020-11-02 00:31:43 +00:00
|
|
|
|
2020-11-13 16:18:27 +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))
|
2020-11-13 16:18:27 +00:00
|
|
|
|
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
|
2020-11-14 13:05:09 +00:00
|
|
|
rollupConfig.plugins.push(analyze())
|
2020-11-01 23:17:44 +00:00
|
|
|
}
|
|
|
|
|
2020-11-14 22:23:17 +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) {
|
2020-11-14 22:23:17 +00:00
|
|
|
rollupConfig.plugins.push(terser({
|
|
|
|
mangle: {
|
|
|
|
keep_fnames: true,
|
|
|
|
keep_classnames: true
|
|
|
|
},
|
|
|
|
format: {
|
|
|
|
comments: false
|
|
|
|
}
|
|
|
|
}))
|
2020-11-01 23:17:44 +00:00
|
|
|
}
|
|
|
|
|
2020-11-14 13:05:09 +00:00
|
|
|
return rollupConfig
|
2020-11-01 23:17:44 +00:00
|
|
|
}
|