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

216 lines
7.1 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 { 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'
2020-11-20 00:16:31 +00:00
import { SigmaContext } 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'
// 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 }
2020-11-20 00:16:31 +00:00
export const getRollupConfig = (sigmaContext: SigmaContext) => {
2020-11-21 11:42:02 +00:00
const extensions: string[] = ['.ts', '.mjs', '.js', '.json', '.node']
2020-11-01 23:17:44 +00:00
2020-11-21 11:42:02 +00:00
const nodePreset = sigmaContext.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
}
const env = un.env(nodePreset, builtinPreset, sigmaContext.env)
// console.log(env)
2020-11-20 00:16:31 +00:00
const buildServerDir = join(sigmaContext._nuxt.buildDir, 'dist/server')
const runtimeAppDir = join(sigmaContext._internal.runtimeDir, 'app')
const rollupConfig: RollupConfig = {
2020-11-20 00:16:31 +00:00
input: resolvePath(sigmaContext, sigmaContext.entry),
2020-11-01 23:17:44 +00:00
output: {
2020-11-20 00:16:31 +00:00
dir: sigmaContext.output.serverDir,
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'
} else if (lastModule.startsWith(sigmaContext._nuxt.buildDir)) {
prefix = 'nuxt'
} else if (lastModule.startsWith(sigmaContext._internal.runtimeDir)) {
prefix = 'sigma'
} else if (!prefix && sigmaContext.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
},
2020-11-20 00:16:31 +00:00
inlineDynamicImports: sigmaContext.inlineChunks,
2020-11-01 23:17:44 +00:00
format: 'cjs',
exports: 'auto',
2020-11-01 23:17:44 +00:00
intro: '',
outro: '',
preferConst: true
},
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
}
2020-11-20 00:16:31 +00:00
if (sigmaContext.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: {
2020-11-20 00:16:31 +00:00
'process.env.NODE_ENV': sigmaContext._nuxt.dev ? '"development"' : '"production"',
2020-11-05 18:53:17 +00:00
'typeof window': '"undefined"',
2020-11-20 00:16:31 +00:00
'process.env.ROUTER_BASE': JSON.stringify(sigmaContext._nuxt.routerBase),
'process.env.PUBLIC_PATH': JSON.stringify(sigmaContext._nuxt.publicPath),
'process.env.NUXT_STATIC_BASE': JSON.stringify(sigmaContext._nuxt.staticAssets.base),
'process.env.NUXT_STATIC_VERSION': JSON.stringify(sigmaContext._nuxt.staticAssets.version),
2020-11-21 13:58:24 +00:00
'process.env.NUXT_FULL_STATIC': sigmaContext._nuxt.fullStatic as unknown as string,
'process.env.SIGMA_PRESET': JSON.stringify(sigmaContext.preset),
'process.env.RUNTIME_CONFIG': JSON.stringify(sigmaContext._nuxt.runtimeConfig)
2020-11-02 00:31:43 +00:00
}
}))
// ESBuild (typescript)
rollupConfig.plugins.push(esbuild({}))
2020-11-03 19:55:36 +00:00
// Dynamic Require Support
rollupConfig.plugins.push(dynamicRequire({
2020-11-20 00:16:31 +00:00
dir: resolve(sigmaContext._nuxt.buildDir, 'dist/server'),
inline: sigmaContext.node === false || sigmaContext.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
if (sigmaContext.serveStatic) {
rollupConfig.plugins.push(dirnames())
2020-11-28 22:49:39 +00:00
rollupConfig.plugins.push(staticAssets(sigmaContext))
}
2020-11-28 22:49:39 +00:00
// Middleware
const _middleware = [...sigmaContext.middleware]
if (sigmaContext.serveStatic) {
_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
2020-11-20 00:16:31 +00:00
const renderer = sigmaContext.renderer || 'vue2'
rollupConfig.plugins.push(alias({
2020-11-02 00:31:43 +00:00
entries: {
2020-11-20 00:16:31 +00:00
'~runtime': sigmaContext._internal.runtimeDir,
'~renderer': require.resolve(resolve(sigmaContext._internal.runtimeDir, 'app', renderer)),
'~build': sigmaContext._nuxt.buildDir,
...env.alias
2020-11-02 00:31:43 +00:00
}
}))
// External Plugin
2020-11-20 00:16:31 +00:00
if (sigmaContext.externals) {
rollupConfig.plugins.push(externals({
2020-11-20 00:16:31 +00:00
relativeTo: sigmaContext.output.serverDir,
include: [
2020-11-20 00:16:31 +00:00
sigmaContext._internal.runtimeDir,
...sigmaContext.middleware.map(m => m.handle)
]
}))
}
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,
2020-11-20 00:16:31 +00:00
rootDir: sigmaContext._nuxt.rootDir,
2020-12-02 13:12:29 +00:00
moduleDirectories: [
resolve(sigmaContext._nuxt.rootDir, 'node_modules'),
resolve(MODULE_DIR, 'node_modules')
],
2020-11-02 00:31:43 +00:00
mainFields: ['main'] // Force resolve CJS (@vue/runtime-core ssrUtils)
}))
// Automatically mock unresolved externals
// 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))
2020-11-20 00:16:31 +00:00
if (sigmaContext.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
2020-11-20 00:16:31 +00:00
// https://github.com/terser/terser#minify-sigmaContext
if (sigmaContext.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
}