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

226 lines
7.0 KiB
TypeScript
Raw Normal View History

2020-11-01 23:17:44 +00:00
import Module from 'module'
import { dirname, join, resolve } from 'path'
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'
import hasha from 'hasha'
import { SLSOptions } from '../config'
import { resolvePath, MODULE_DIR } from '../utils'
import { dynamicRequire } from './dynamic-require'
import { externals } from './externals'
2020-11-01 23:17:44 +00:00
const mapArrToVal = (val, arr) => arr.reduce((p, c) => ({ ...p, [c]: val }), {})
2020-11-13 16:14:17 +00:00
2020-11-01 23:17:44 +00:00
export type RollupConfig = InputOptions & { output: OutputOptions }
export const getRollupConfig = (options: SLSOptions) => {
2020-11-13 16:14:17 +00:00
const extensions: string[] = ['.ts', '.mjs', '.js', '.json', '.node']
2020-11-01 23:17:44 +00:00
const external: InputOptions['external'] = []
2020-11-13 16:14:17 +00:00
const injects:{ [key: string]: string| string[] } = {}
2020-11-01 23:17:44 +00:00
const aliases: { [key: string]: string } = {}
Object.assign(aliases, mapArrToVal('~mocks/generic', [
// @nuxt/devalue
'consola',
// vue2
'encoding',
'stream',
'he',
'resolve',
'source-map',
'lodash.template',
'serialize-javascript',
// vue3
'@babel/parser',
'@vue/compiler-core',
'@vue/compiler-dom',
'@vue/compiler-ssr'
]))
2020-11-13 13:18:07 +00:00
// Uses eval
aliases.depd = '~mocks/custom/depd'
if (options.node === false) {
2020-11-13 16:14:17 +00:00
// Globals
// injects.Buffer = ['buffer', 'Buffer'] <-- TODO: Make it opt-in
2020-11-13 17:45:03 +00:00
injects.process = '~mocks/node/process'
2020-11-13 16:14:17 +00:00
// Aliases
Object.assign(aliases, {
// Node
...mapArrToVal('~mocks/generic', Module.builtinModules),
http: '~mocks/node/http',
fs: '~mocks/node/fs',
2020-11-13 17:45:03 +00:00
process: '~mocks/node/process',
'node-process': require.resolve('process/browser.js'),
// buffer: require.resolve('buffer/index.js'),
2020-11-13 16:14:17 +00:00
util: require.resolve('util/util.js'),
events: require.resolve('events/events.js'),
inherits: require.resolve('inherits/inherits_browser.js'),
// Custom
'node-fetch': '~mocks/custom/node-fetch',
2020-11-13 16:14:17 +00:00
etag: '~mocks/generic/noop',
// Express
...mapArrToVal('~mocks/generic', [
'serve-static',
'iconv-lite'
]),
2020-11-13 16:14:17 +00:00
// Mime
'mime-db': '~mocks/custom/mime-db',
'mime/lite': require.resolve('mime/lite'),
mime: '~mocks/custom/mime'
})
2020-11-01 23:17:44 +00:00
} else {
external.push(...Module.builtinModules)
}
const chunksDirName = join(dirname(options.outName), 'chunks')
const rollupConfig: RollupConfig = {
input: resolvePath(options, options.entry),
2020-11-01 23:17:44 +00:00
output: {
dir: options.targetDir,
entryFileNames: options.outName,
chunkFileNames: join(chunksDirName, '[name].js'),
inlineDynamicImports: options.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
},
external,
2020-11-02 00:31:43 +00:00
plugins: []
2020-11-01 23:17:44 +00:00
}
if (options.logStartup) {
rollupConfig.output.intro += 'global._startTime = global.process.hrtime();'
2020-11-02 14:42:27 +00:00
// eslint-disable-next-line no-template-curly-in-string
rollupConfig.output.outro += 'global._endTime = global.process.hrtime(global._startTime); global._coldstart = ((global._endTime[0] * 1e9) + global._endTime[1]) / 1e6; console.log(`λ Cold start took: ${global._coldstart}ms (${typeof __filename !== "undefined" ? __filename.replace(process.cwd(), "") : "<entry>"})`);'
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: {
'process.env.NODE_ENV': '"production"',
2020-11-05 18:53:17 +00:00
'typeof window': '"undefined"',
'process.env.ROUTER_BASE': JSON.stringify(options.routerBase),
'process.env.PUBLIC_PATH': JSON.stringify(options.publicPath),
'process.env.NUXT_STATIC_BASE': JSON.stringify(options.staticAssets.base),
'process.env.NUXT_STATIC_VERSION': JSON.stringify(options.staticAssets.version),
// @ts-ignore
'process.env.NUXT_FULL_STATIC': options.fullStatic
2020-11-02 00:31:43 +00:00
}
}))
2020-11-03 19:55:36 +00:00
// Dynamic Require Support
rollupConfig.plugins.push(dynamicRequire({
dir: resolve(options.buildDir, 'dist/server'),
inline: options.node === false || options.inlineChunks,
2020-11-03 19:55:36 +00:00
globbyOptions: {
ignore: [
'server.js'
]
}
}))
2020-11-02 13:11:26 +00:00
2020-11-14 00:49:37 +00:00
// https://github.com/rollup/plugins/tree/master/packages/replace
// TODO: better fix for node-fetch issue
rollupConfig.plugins.push(replace({
2020-11-14 00:49:37 +00:00
delimiters: ['', ''],
values: {
'require(\'encoding\')': '{}'
}
}))
// Provide serverMiddleware
const getImportId = p => '_' + hasha(p).substr(0, 6)
rollupConfig.plugins.push(virtual({
'~serverMiddleware': `
${options.serverMiddleware.filter(m => !m.lazy).map(m => `import ${getImportId(m.handle)} from '${m.handle}';`).join('\n')}
${options.serverMiddleware.filter(m => m.lazy).map(m => `const ${getImportId(m.handle)} = () => import('${m.handle}');`).join('\n')}
export default [
${options.serverMiddleware.map(m => `{ route: '${m.route}', handle: ${getImportId(m.handle)}, lazy: ${m.lazy || false} }`).join(',\n')}
];
`
}))
2020-11-02 00:31:43 +00:00
// https://github.com/rollup/plugins/tree/master/packages/alias
const renderer = options.renderer || 'vue2'
rollupConfig.plugins.push(alias({
2020-11-02 00:31:43 +00:00
entries: {
'~runtime': options.runtimeDir,
'~mocks': resolve(options.runtimeDir, 'mocks'),
'~renderer': require.resolve(resolve(options.runtimeDir, 'ssr', renderer)),
'~build': options.buildDir,
'~mock': require.resolve(resolve(options.runtimeDir, 'mocks/generic')),
2020-11-13 13:18:07 +00:00
...aliases
2020-11-02 00:31:43 +00:00
}
}))
// External Plugin
if (options.externals) {
rollupConfig.plugins.push(externals({
relativeTo: options.targetDir,
include: [
options.runtimeDir,
...options.serverMiddleware.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,
rootDir: options.rootDir,
2020-11-02 12:12:39 +00:00
// https://www.npmjs.com/package/resolve
customResolveOptions: {
basedir: options.rootDir,
paths: [
resolve(options.rootDir, 'node_modukes'),
resolve(MODULE_DIR, 'node_modules')
]
},
2020-11-02 00:31:43 +00:00
mainFields: ['main'] // Force resolve CJS (@vue/runtime-core ssrUtils)
}))
// 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
rollupConfig.plugins.push(inject(injects))
if (options.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
}
if (options.minify !== false) {
rollupConfig.plugins.push(terser())
2020-11-01 23:17:44 +00:00
}
return rollupConfig
2020-11-01 23:17:44 +00:00
}