2020-11-01 23:17:44 +00:00
|
|
|
import Module from 'module'
|
2020-11-05 15:36:31 +00:00
|
|
|
import { basename, extname, 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 analyze from 'rollup-plugin-analyzer'
|
2020-11-05 12:26:00 +00:00
|
|
|
|
2020-11-05 13:38:15 +00:00
|
|
|
import { SLSOptions } from '../config'
|
2020-11-05 21:59:14 +00:00
|
|
|
import { RUNTIME_DIR } from '../utils'
|
2020-11-03 19:55:36 +00:00
|
|
|
import dynamicRequire from './dynamic-require'
|
2020-11-01 23:17:44 +00:00
|
|
|
|
|
|
|
export type RollupConfig = InputOptions & { output: OutputOptions }
|
|
|
|
|
2020-11-05 13:38:15 +00:00
|
|
|
export const getRollupConfig = (config: SLSOptions) => {
|
2020-11-01 23:17:44 +00:00
|
|
|
const mocks = [
|
2020-11-02 00:54:45 +00:00
|
|
|
// @nuxt/devalue
|
|
|
|
'consola',
|
|
|
|
// vue2
|
2020-11-02 00:31:43 +00:00
|
|
|
'encoding',
|
2020-11-04 02:06:23 +00:00
|
|
|
'stream',
|
2020-11-02 00:54:45 +00:00
|
|
|
'he',
|
|
|
|
'resolve',
|
|
|
|
'source-map',
|
|
|
|
'lodash.template',
|
|
|
|
'serialize-javascript',
|
|
|
|
// vue3
|
|
|
|
'@babel/parser',
|
2020-11-01 23:17:44 +00:00
|
|
|
'@vue/compiler-core',
|
|
|
|
'@vue/compiler-dom',
|
|
|
|
'@vue/compiler-ssr'
|
|
|
|
]
|
|
|
|
|
2020-11-04 13:52:12 +00:00
|
|
|
const providedDeps = [
|
|
|
|
'@nuxt/devalue',
|
|
|
|
'vue-bundle-renderer'
|
|
|
|
]
|
|
|
|
|
2020-11-01 23:17:44 +00:00
|
|
|
const extensions = ['.ts', '.mjs', '.js', '.json', '.node']
|
|
|
|
|
|
|
|
const external = []
|
|
|
|
|
|
|
|
if (config.node === false) {
|
|
|
|
mocks.push(...Module.builtinModules)
|
|
|
|
} else {
|
|
|
|
external.push(...Module.builtinModules)
|
|
|
|
}
|
|
|
|
|
|
|
|
const options: RollupConfig = {
|
|
|
|
input: config.entry,
|
|
|
|
output: {
|
2020-11-05 15:36:31 +00:00
|
|
|
file: resolve(config.targetDir, config.outName),
|
2020-11-01 23:17:44 +00:00
|
|
|
format: 'cjs',
|
|
|
|
intro: '',
|
|
|
|
outro: '',
|
|
|
|
preferConst: true
|
|
|
|
},
|
|
|
|
external,
|
2020-11-02 00:31:43 +00:00
|
|
|
plugins: []
|
2020-11-01 23:17:44 +00:00
|
|
|
}
|
|
|
|
|
2020-11-02 14:42:27 +00:00
|
|
|
if (config.logStartup) {
|
|
|
|
options.output.intro += 'global._startTime = process.hrtime();'
|
|
|
|
// eslint-disable-next-line no-template-curly-in-string
|
2020-11-03 17:09:39 +00:00
|
|
|
options.output.outro += 'global._endTime = process.hrtime(global._startTime); global._coldstart = ((global._endTime[0] * 1e9) + global._endTime[1]) / 1e6; console.log(`λ Cold start took: ${global._coldstart}ms`);'
|
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
|
|
|
|
options.plugins.push(replace({
|
|
|
|
values: {
|
2020-11-04 02:14:39 +00:00
|
|
|
'process.env.NODE_ENV': '"production"',
|
2020-11-05 18:53:17 +00:00
|
|
|
'typeof window': '"undefined"',
|
|
|
|
'process.env.NUXT_STATIC_BASE': JSON.stringify(config.staticAssets.base),
|
|
|
|
'process.env.NUXT_STATIC_VERSION': JSON.stringify(config.staticAssets.version),
|
2020-11-05 21:23:24 +00:00
|
|
|
// @ts-ignore
|
2020-11-05 18:53:17 +00:00
|
|
|
'process.env.NUXT_FULL_STATIC': config.fullStatic
|
2020-11-02 00:31:43 +00:00
|
|
|
}
|
|
|
|
}))
|
|
|
|
|
2020-11-03 19:55:36 +00:00
|
|
|
// Dynamic Require Support
|
|
|
|
options.plugins.push(dynamicRequire({
|
2020-11-05 15:36:31 +00:00
|
|
|
dir: resolve(config.buildDir, 'dist/server'),
|
2020-11-05 11:28:39 +00:00
|
|
|
outDir: (config.node === false || config.inlineChunks) ? undefined : config.targetDir,
|
2020-11-05 15:36:31 +00:00
|
|
|
chunksDir: '_' + basename(config.outName, extname(config.outName)),
|
2020-11-03 19:55:36 +00:00
|
|
|
globbyOptions: {
|
|
|
|
ignore: [
|
|
|
|
'server.js'
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}))
|
2020-11-02 13:11:26 +00:00
|
|
|
|
2020-11-02 00:31:43 +00:00
|
|
|
// https://github.com/rollup/plugins/tree/master/packages/alias
|
2020-11-03 20:51:11 +00:00
|
|
|
const renderer = config.renderer || (config.nuxt === 2 ? 'vue2' : 'vue3')
|
2020-11-02 00:31:43 +00:00
|
|
|
options.plugins.push(alias({
|
|
|
|
entries: {
|
2020-11-04 13:15:38 +00:00
|
|
|
'~runtime': RUNTIME_DIR,
|
2020-11-05 15:36:31 +00:00
|
|
|
'~renderer': require.resolve(resolve(RUNTIME_DIR, renderer)),
|
2020-11-02 00:31:43 +00:00
|
|
|
'~build': config.buildDir,
|
2020-11-05 15:36:31 +00:00
|
|
|
'~mock': require.resolve(resolve(RUNTIME_DIR, 'mock')),
|
2020-11-04 13:52:12 +00:00
|
|
|
...mocks.reduce((p, c) => ({ ...p, [c]: '~mock' }), {}),
|
|
|
|
...providedDeps.reduce((p, c) => ({ ...p, [c]: require.resolve(c) }), {})
|
2020-11-02 00:31:43 +00:00
|
|
|
}
|
|
|
|
}))
|
|
|
|
|
|
|
|
// https://github.com/rollup/plugins/tree/master/packages/node-resolve
|
2020-11-05 15:36:31 +00:00
|
|
|
options.plugins.push(nodeResolve({
|
2020-11-02 00:31:43 +00:00
|
|
|
extensions,
|
|
|
|
preferBuiltins: true,
|
2020-11-02 12:12:39 +00:00
|
|
|
rootDir: config.rootDir,
|
|
|
|
// https://www.npmjs.com/package/resolve
|
|
|
|
customResolveOptions: {
|
|
|
|
basedir: config.rootDir
|
|
|
|
},
|
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
|
|
|
|
options.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
|
|
|
|
options.plugins.push(json())
|
|
|
|
|
2020-11-01 23:17:44 +00:00
|
|
|
if (config.analyze) {
|
|
|
|
// https://github.com/doesdev/rollup-plugin-analyzer
|
|
|
|
options.plugins.push(analyze())
|
|
|
|
}
|
|
|
|
|
2020-11-05 11:28:39 +00:00
|
|
|
if (config.minify !== false) {
|
2020-11-01 23:17:44 +00:00
|
|
|
options.plugins.push(terser())
|
|
|
|
}
|
|
|
|
|
|
|
|
return options
|
|
|
|
}
|