-
Notifications
You must be signed in to change notification settings - Fork 529
/
rollup.config.js
182 lines (165 loc) · 4.23 KB
/
rollup.config.js
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import path from 'path';
import babel from 'rollup-plugin-babel';
import buble from 'rollup-plugin-buble';
import commonjs from 'rollup-plugin-commonjs';
import filesize from 'rollup-plugin-filesize';
import json from 'rollup-plugin-json';
import resolve from 'rollup-plugin-node-resolve';
import replace from 'rollup-plugin-replace';
import { terser } from 'rollup-plugin-terser';
import vueV2 from 'rollup-plugin-vue2';
import vueV3 from 'rollup-plugin-vue3';
const processEnv = (conf) => ({
// parenthesis to avoid syntax errors in places where {} is interpreted as a block
'process.env': `(${JSON.stringify(conf)})`,
});
const createFile = (fileName, content) => ({
name: 'inject-package-json',
buildEnd() {
this.emitFile({
type: 'asset',
fileName,
source: content,
});
},
});
const aliasVueCompat = (vueVersion) => ({
name: 'alias-vue-compat',
resolveId(source, fileName) {
if (source.includes('vue-compat')) {
const matchingVueCompatFile = `./index-${vueVersion}.js`;
const compatFolder = path.resolve(
path.dirname(fileName),
// source is either './vue-compat' or './vue-compat/index.js'
source.replace(/\/index\.js$/, '/')
);
return path.resolve(compatFolder, matchingVueCompatFile);
}
return null;
},
});
const isWatching = process.env.ROLLUP_WATCH;
function outputs(vueVersion) {
const vuePlugin = vueVersion === 'vue3' ? vueV3 : vueV2;
const plugins = [
vuePlugin({ compileTemplate: true, css: false }),
commonjs(),
json(),
buble({
transforms: {
dangerousForOf: true,
},
}),
replace(processEnv({ NODE_ENV: 'production' })),
aliasVueCompat(vueVersion),
terser({
sourcemap: true,
}),
];
const external = (id) =>
[
'algoliasearch-helper',
'instantsearch.js',
'instantsearch-ui-components',
'vue',
'mitt',
].some((dep) => id === dep || id.startsWith(`${dep}/`));
const cjs = {
input: 'src/instantsearch.js',
external,
output: [
{
sourcemap: true,
file: `${vueVersion}/cjs/index.js`,
format: 'cjs',
exports: 'named',
},
],
plugins: [
...plugins,
replace({
'instantsearch.js/es': 'instantsearch.js/cjs',
}),
createFile(
'package.json',
JSON.stringify({ type: 'commonjs', sideEffects: true })
),
],
};
const esm = {
input: 'src/instantsearch.js',
external,
output: [
{
sourcemap: true,
dir: `${vueVersion}/es`,
format: 'es',
},
],
preserveModules: true,
plugins: [
...plugins,
createFile(
'index.js',
`import InstantSearch from './src/instantsearch.js';
export default InstantSearch;
export * from './src/instantsearch.js';`
),
createFile(
'package.json',
JSON.stringify({ type: 'module', sideEffects: true })
),
babel({
exclude: /node_modules|algoliasearch-helper/,
extensions: ['.js', '.jsx', '.es6', '.es', '.mjs', '.vue'],
babelrc: false,
plugins: [
[
require('../../scripts/babel/extension-resolver'),
{
// For verification, see test/module/packages-are-es-modules.mjs
modulesToResolve: [
// InstantSearch.js/es is an ES Module, so needs complete paths,
'instantsearch.js',
],
},
],
],
}),
],
};
const umd = {
input: 'src/instantsearch.umd.js',
external: ['vue'],
output: [
{
sourcemap: true,
file: `${vueVersion}/umd/index.js`,
format: 'umd',
name: 'VueInstantSearch',
exports: 'named',
globals: {
vue: 'Vue',
},
},
],
onwarn(warning, warn) {
if (warning.code === 'CIRCULAR_DEPENDENCY')
throw new Error(warning.message);
warn(warning);
},
plugins: [
...plugins,
resolve({
browser: true,
preferBuiltins: false,
}),
filesize(),
],
};
if (isWatching) {
return [esm];
}
return [cjs, esm, umd];
}
export default [...outputs('vue2'), ...(isWatching ? [] : outputs('vue3'))];