forked from algolia/vue-instantsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.js
163 lines (153 loc) · 4.02 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
import path from 'path';
import vueV2 from 'rollup-plugin-vue2';
import vueV3 from 'rollup-plugin-vue3';
import buble from 'rollup-plugin-buble';
import filesize from 'rollup-plugin-filesize';
import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';
import { terser } from 'rollup-plugin-terser';
import replace from 'rollup-plugin-replace';
import json from 'rollup-plugin-json';
import babel from 'rollup-plugin-babel';
import { extensionResolver } from './scripts/babel-plugin-extension-resolver';
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;
},
});
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', 'vue', 'mitt'].some(
dep => id === dep || id.startsWith(`${dep}/`)
);
return [
{
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 })
),
],
},
{
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({
extensions: ['.js', '.jsx', '.es6', '.es', '.mjs', '.vue'],
babelrc: false,
plugins: [
[
extensionResolver,
{
// For verification, see test/module/packages-are-es-modules.mjs
modulesToResolve: [
// InstantSearch.js/es is an ES Module, so needs complete paths,
'instantsearch.js',
],
},
],
],
}),
],
},
{
input: 'src/instantsearch.umd.js',
external: ['vue'],
output: [
{
sourcemap: true,
file: `${vueVersion}/umd/index.js`,
format: 'umd',
name: 'VueInstantSearch',
exports: 'named',
globals: {
vue: 'Vue',
},
},
],
plugins: [
...plugins,
resolve({
browser: true,
preferBuiltins: false,
}),
filesize(),
],
},
];
}
export default [...outputs('vue2'), ...outputs('vue3')];