-
Notifications
You must be signed in to change notification settings - Fork 13
/
rollup.config.mjs
162 lines (151 loc) · 4.23 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import typescript from 'rollup-plugin-typescript2';
import terser from '@rollup/plugin-terser';
import serve from 'rollup-plugin-serve';
import commonjs from '@rollup/plugin-commonjs';
import {nodeResolve} from '@rollup/plugin-node-resolve';
import alias from '@rollup/plugin-alias';
import json from '@rollup/plugin-json';
import replace from '@rollup/plugin-replace';
import copy from 'rollup-plugin-copy';
import {parse, resolve} from 'path';
import packageJson from './package.json' assert {type: 'json'};
import * as url from 'url';
/**
* @typedef {import('rollup').RollupOptions} RollupOptions
*/
const __dirname = url.fileURLToPath(new URL('.', import.meta.url));
const browserFetch = () =>
alias({
entries: [
{
find: 'cross-fetch',
replacement: resolve(__dirname, './bundle/browser-fetch.ts'),
},
],
});
const tsPlugin = () =>
typescript({
useTsconfigDeclarationDir: true,
});
const versionReplace = () =>
replace({
preventAssignment: true,
'process.env.PKG_VERSION': JSON.stringify(packageJson.version),
});
/**
* @param {{sourceFileName: string, aliasFileName: string}} options
*/
const aliasFile = ({sourceFileName, aliasFileName}) => {
const {dir: dest, base: rename} = parse(aliasFileName);
return copy({hook: 'writeBundle', targets: [{src: sourceFileName, dest, rename}]});
};
/**
* @satisfies {RollupOptions}
*/
const coveouaConfig = {
input: './src/coveoua/browser.ts',
output: [
{
file: './dist/coveoua.js',
format: 'umd',
name: 'coveoua',
sourcemap: true,
plugins: [terser({format: {comments: false}})],
},
{
file: './dist/coveoua.browser.js',
format: 'iife',
name: 'coveoua',
sourcemap: true,
plugins: [terser({format: {comments: false}})],
},
{
file: './dist/coveoua.debug.js',
format: 'umd',
name: 'coveoua',
sourcemap: true,
},
],
plugins: [
browserFetch(),
nodeResolve({preferBuiltins: true, browser: true}),
versionReplace(),
tsPlugin(),
process.env.SERVE
? serve({
contentBase: ['dist', 'public'],
port: 9001,
open: true,
headers: {
'Access-Control-Allow-Origin': 'http://localhost:9001',
},
})
: null,
],
};
/**
* @satisfies {RollupOptions}
*/
const nodeModulesConfig = {
input: './src/coveoua/library.ts',
output: [
{
file: `./dist/library.cjs`,
format: 'cjs',
},
{
file: `./dist/library.mjs`,
format: 'es',
},
],
plugins: [
nodeResolve({mainFields: ['main'], preferBuiltins: true}),
versionReplace(),
commonjs(),
tsPlugin(),
json(),
aliasFile({sourceFileName: './dist/library.cjs', aliasFileName: './dist/library.js'}),
],
};
/**
* @satisfies {RollupOptions}
*/
const browserModulesConfig = {
input: './src/coveoua/headless.ts',
output: {
file: `./dist/browser.mjs`,
format: 'es',
},
plugins: [
browserFetch(),
nodeResolve({preferBuiltins: true}),
versionReplace(),
typescript({
useTsconfigDeclarationDir: true,
tsconfigOverride: {compilerOptions: {target: 'es6'}},
}),
aliasFile({sourceFileName: './dist/browser.mjs', aliasFileName: './dist/library.es.js'}),
],
};
/**
* @satisfies {RollupOptions}
*/
const reactNativeConfig = {
external: ['react-native', 'cross-fetch'],
input: './src/react-native/index.ts',
output: {
file: './dist/react-native.es.js',
format: 'es',
},
plugins: [
nodeResolve({preferBuiltins: true}),
versionReplace(),
commonjs(),
json(),
typescript({
useTsconfigDeclarationDir: true,
tsconfigOverride: {compilerOptions: {target: 'es6'}},
}),
],
};
export default [coveouaConfig, nodeModulesConfig, browserModulesConfig, reactNativeConfig];