-
Notifications
You must be signed in to change notification settings - Fork 1
/
rollup.config.mjs
94 lines (87 loc) · 2.71 KB
/
rollup.config.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// @ts-check
import typescript from '@rollup/plugin-typescript'
import resolve from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'
import json from '@rollup/plugin-json'
import nodePolyfills from 'rollup-plugin-node-polyfills'
import alias from '@rollup/plugin-alias'
import inject from '@rollup/plugin-inject'
import replace from '@rollup/plugin-replace'
import path from 'path'
import { fileURLToPath } from 'url'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
/** @typedef {import("rollup").RollupOptions} RollupOptions */
/** @typedef {import("rollup").Plugin} RollupPlugin */
/** @type {() => RollupPlugin} */
function fixForGoogleAppsScript() {
return {
// this name will show up in warnings and errors:
name: 'google-apps-script',
// we want to expose all exported items as functions in global scope
renderChunk: (code, chunk, options) => {
/** @type {(exportName: string) => string} */
const mapExport = (exportName) => `function ${exportName}(...args) {
return ${options.name}.${exportName}(...args);
}`
return `${code}
${chunk.exports.map(mapExport).join('\n')}
`
},
}
}
const adaptersPath = path.resolve(
__dirname,
'src/google-apps-interop/adapters.js',
)
const axiosPath = path.resolve(__dirname, 'node_modules/axios')
/** @type {RollupOptions} */
const config = {
input: 'src/index.ts',
output: {
name: '__main',
format: 'iife',
file: 'build/script.js',
},
plugins: [
alias({
entries: [
// we don't need the 'axios/lib/adapters' because they're for Node and Browsers
// but we're running in a Google Apps Script engine
{
find: '../adapters/adapters.js',
replacement: adaptersPath,
},
// resolving inner dependencies in axios isn't currently possible due to the way they setup package.json
{
find: 'axios/lib/core/buildFullPath',
replacement: `${axiosPath}/lib/core/buildFullPath.js`,
},
{
find: 'axios/lib/helpers/buildURL',
replacement: `${axiosPath}/lib/helpers/buildURL.js`,
},
],
}),
inject({
Buffer: ['buffer', 'Buffer'],
}),
// @ts-ignore upstream issue:
nodePolyfills(),
commonjs({ transformMixedEsModules: true }),
resolve({ browser: true, preferBuiltins: false, modulesOnly: true }),
json(),
typescript(),
replace({
'module.exports': 'exports.__nested',
include: ['node_modules/**/axios/**/*'],
preventAssignment: true,
}),
replace({
module: 'exports.__nested',
include: ['node_modules/**/lodash/**/*'],
preventAssignment: true,
}),
fixForGoogleAppsScript(),
],
}
export default config