2020-11-01 23:17:44 +00:00
|
|
|
import Module from 'module'
|
2020-11-10 18:19:24 +00:00
|
|
|
import { basename, dirname, 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'
|
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'
|
2020-11-05 12:26:00 +00:00
|
|
|
|
2020-11-12 18:20:55 +00:00
|
|
|
import hasha from 'hasha'
|
2020-11-05 13:38:15 +00:00
|
|
|
import { SLSOptions } from '../config'
|
2020-11-06 09:51:35 +00:00
|
|
|
import { resolvePath } from '../utils'
|
2020-11-03 19:55:36 +00:00
|
|
|
import dynamicRequire from './dynamic-require'
|
2020-11-01 23:17:44 +00:00
|
|
|
|
2020-11-13 18:29:47 +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 }
|
|
|
|
|
2020-11-05 13:38:15 +00:00
|
|
|
export const getRollupConfig = (config: SLSOptions) => {
|
2020-11-04 13:52:12 +00:00
|
|
|
const providedDeps = [
|
|
|
|
'@nuxt/devalue',
|
2020-11-06 16:40:00 +00:00
|
|
|
'vue-bundle-renderer',
|
|
|
|
'@cloudflare/kv-asset-handler'
|
2020-11-04 13:52:12 +00:00
|
|
|
]
|
|
|
|
|
2020-11-13 16:14:17 +00:00
|
|
|
const extensions: string[] = ['.ts', '.mjs', '.js', '.json', '.node']
|
2020-11-01 23:17:44 +00:00
|
|
|
|
2020-11-13 16:14:17 +00:00
|
|
|
const external: string[] = []
|
|
|
|
|
|
|
|
const injects:{ [key: string]: string| string[] } = {}
|
2020-11-01 23:17:44 +00:00
|
|
|
|
2020-11-13 16:18:27 +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
|
|
|
|
2020-11-01 23:17:44 +00:00
|
|
|
if (config.node === false) {
|
2020-11-13 16:14:17 +00:00
|
|
|
// Globals
|
2020-11-13 18:29:47 +00:00
|
|
|
// 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'),
|
2020-11-13 18:29:47 +00:00
|
|
|
// 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
|
2020-11-14 00:11:26 +00:00
|
|
|
'node-fetch': '~mocks/custom/node-fetch',
|
2020-11-13 16:14:17 +00:00
|
|
|
depd: '~mocks/custom/depd',
|
|
|
|
etag: '~mocks/generic/noop',
|
|
|
|
|
2020-11-13 18:29:47 +00:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2020-11-10 18:19:24 +00:00
|
|
|
const outFile = resolve(config.targetDir, config.outName)
|
|
|
|
|
2020-11-01 23:17:44 +00:00
|
|
|
const options: RollupConfig = {
|
2020-11-06 09:51:35 +00:00
|
|
|
input: resolvePath(config, config.entry),
|
2020-11-01 23:17:44 +00:00
|
|
|
output: {
|
2020-11-10 18:19:24 +00:00
|
|
|
file: outFile,
|
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) {
|
2020-11-13 17:45:03 +00:00
|
|
|
options.output.intro += 'global._startTime = global.process.hrtime();'
|
2020-11-02 14:42:27 +00:00
|
|
|
// eslint-disable-next-line no-template-curly-in-string
|
2020-11-13 17:45:03 +00:00
|
|
|
options.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`);'
|
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"',
|
2020-11-10 23:04:33 +00:00
|
|
|
'process.env.ROUTER_BASE': JSON.stringify(config.routerBase),
|
2020-11-11 13:42:10 +00:00
|
|
|
'process.env.PUBLIC_PATH': JSON.stringify(config.publicPath),
|
2020-11-05 18:53:17 +00:00
|
|
|
'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-10 18:19:24 +00:00
|
|
|
outDir: (config.node === false || config.inlineChunks) ? undefined : dirname(outFile),
|
|
|
|
chunksDir: '_' + basename(outFile, extname(outFile)),
|
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
|
|
|
|
options.plugins.push(replace({
|
|
|
|
delimiters: ['', ''],
|
|
|
|
values: {
|
|
|
|
'require(\'encoding\')': '{}'
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
|
2020-11-12 18:20:55 +00:00
|
|
|
// Provide serverMiddleware
|
|
|
|
const getImportId = p => '_' + hasha(p).substr(0, 6)
|
|
|
|
options.plugins.push(virtual({
|
|
|
|
'~serverMiddleware': `
|
|
|
|
${config.serverMiddleware.map(m => `import ${getImportId(m.handle)} from '${m.handle}';`).join('\n')}
|
|
|
|
|
|
|
|
export default [
|
|
|
|
${config.serverMiddleware.map(m => `{ route: '${m.route}', handle: ${getImportId(m.handle)} }`).join(',\n')}
|
|
|
|
];
|
|
|
|
`
|
|
|
|
}))
|
|
|
|
|
2020-11-02 00:31:43 +00:00
|
|
|
// https://github.com/rollup/plugins/tree/master/packages/alias
|
2020-11-06 13:46:17 +00:00
|
|
|
const renderer = config.renderer || 'vue2'
|
2020-11-02 00:31:43 +00:00
|
|
|
options.plugins.push(alias({
|
|
|
|
entries: {
|
2020-11-06 09:51:35 +00:00
|
|
|
'~runtime': config.runtimeDir,
|
2020-11-13 16:14:17 +00:00
|
|
|
'~mocks': resolve(config.runtimeDir, 'mocks'),
|
2020-11-11 14:03:55 +00:00
|
|
|
'~renderer': require.resolve(resolve(config.runtimeDir, 'ssr', renderer)),
|
2020-11-02 00:31:43 +00:00
|
|
|
'~build': config.buildDir,
|
2020-11-13 13:18:07 +00:00
|
|
|
'~mock': require.resolve(resolve(config.runtimeDir, 'mocks/generic')),
|
|
|
|
...providedDeps.reduce((p, c) => ({ ...p, [c]: require.resolve(c) }), {}),
|
|
|
|
...aliases
|
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
|
2020-11-12 18:20:55 +00:00
|
|
|
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-13 16:18:27 +00:00
|
|
|
// https://github.com/rollup/plugins/tree/master/packages/inject
|
|
|
|
options.plugins.push(inject(injects))
|
|
|
|
|
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
|
|
|
|
}
|