1
- import fs from 'fs' ;
1
+ import babel from '@rollup/plugin-babel' ;
2
+ import { nodeResolve } from '@rollup/plugin-node-resolve' ;
3
+ import globals from 'rollup-plugin-node-globals' ;
4
+ import { terser } from 'rollup-plugin-terser' ;
5
+ import ts from 'rollup-plugin-typescript2' ;
2
6
3
7
// Org where the packages are pushed
4
8
const NPM_ORG = '@algolia/' ;
@@ -10,111 +14,51 @@ const NODE_FORMATS = ['esm-node', 'cjs'];
10
14
// Utils package with default options
11
15
const UTILS = {
12
16
'client-common' : {
13
- dependencies : [ ] ,
17
+ external : [ ] ,
18
+ plugins : [
19
+ babel ( {
20
+ babelrc : false ,
21
+ extensions : [ '.ts' ] ,
22
+ exclude : 'node_modules/**' ,
23
+ plugins : [ '@babel/plugin-proposal-class-properties' ] ,
24
+ } ) ,
25
+ ] ,
14
26
} ,
15
27
'requester-browser-xhr' : {
16
28
external : [ 'dom' ] ,
17
- dependencies : [ ` ${ NPM_ORG } client-common` ] ,
29
+ plugins : [ ] ,
18
30
} ,
19
31
'requester-fetch' : {
20
32
external : [ 'dom' ] ,
21
- dependencies : [ ` ${ NPM_ORG } client-common` ] ,
33
+ plugins : [ ] ,
22
34
} ,
23
35
'requester-node-http' : {
24
36
external : [ 'https' , 'http' , 'url' ] ,
25
- dependencies : [ ` ${ NPM_ORG } client-common` ] ,
37
+ plugins : [ ] ,
26
38
} ,
27
39
} ;
28
40
29
- /**
30
- * Returns the `UTILS` packages configuration with their default bundler options.
31
- */
32
- function getUtilConfigs ( ) {
33
- const commonOptions = {
34
- input : 'index.ts' ,
35
- formats : NODE_FORMATS ,
36
- external : [ ] ,
37
- } ;
38
-
39
- return Object . entries ( UTILS ) . map ( ( [ key , utilOptions ] ) => {
40
- return {
41
- ...commonOptions ,
42
- ...utilOptions ,
43
- output : key ,
44
- package : key ,
45
- name : `${ NPM_ORG } ${ key } ` ,
46
- } ;
47
- } ) ;
48
- }
49
-
50
- /**
51
- * Whether to build the given `utilClient` or not.
52
- */
53
- function shouldBuildUtil ( utilClient ) {
54
- if ( process . env . SKIP_UTILS === 'true' ) {
55
- return false ;
56
- }
57
-
58
- if ( ! process . env . CI ) {
59
- return true ;
60
- }
61
-
62
- // Checking existence of `dist` folder doesn't really guarantee the built files are up-to-date.
63
- // However, on the CI, it's very likely.
64
- return ! fs . existsSync ( path . resolve ( 'packages' , utilClient , 'dist' ) ) ;
65
- }
66
-
67
- /**
68
- * Reads available packages in the monorepo.
69
- */
70
- function getAvailableClients ( client ) {
71
- const availableClients = fs
72
- . readdirSync ( 'packages/' )
73
- . filter ( ( packageName ) => ! Object . keys ( UTILS ) . includes ( packageName ) ) ;
74
-
75
- return client === 'all'
76
- ? availableClients
77
- : availableClients . filter ( ( availableClient ) => availableClient === client ) ;
78
- }
79
-
80
- /**
81
- * Returns the packages to bundled based on environment variables and run conditions.
82
- */
83
- export function getPackageConfigs ( ) {
84
- const UTIL_CONFIGS = getUtilConfigs ( ) ;
85
- const CLIENT = process . env . CLIENT . replace ( NPM_ORG , '' ) ;
86
-
87
- if ( CLIENT === 'utils' ) {
88
- return UTIL_CONFIGS ;
89
- }
90
-
91
- if ( Object . keys ( UTILS ) . includes ( CLIENT ) ) {
92
- return UTIL_CONFIGS . filter ( ( config ) => config . package === CLIENT ) ;
93
- }
94
-
95
- const availableClients = getAvailableClients ( CLIENT ) ;
41
+ function getBaseConfigs ( pkg ) {
42
+ const packageName = pkg . name . replace ( NPM_ORG , '' ) ;
43
+ const isUtils = UTILS [ packageName ] !== undefined ;
44
+ const baseConfigs = [ ] ;
96
45
97
- if ( availableClients . length === 0 ) {
98
- throw new Error ( `No clients matches '${ CLIENT } '.` ) ;
99
- }
100
-
101
- const configs = availableClients . flatMap ( ( packageName ) => {
46
+ if ( ! isUtils ) {
102
47
const isAlgoliasearchClient = packageName === 'algoliasearch' ;
103
48
const commonConfig = {
104
49
package : packageName ,
105
- name : ` ${ NPM_ORG } ${ packageName } ` ,
50
+ name : pkg . name ,
106
51
output : packageName ,
107
- dependencies : [ ` ${ NPM_ORG } client-common` ] ,
52
+ dependencies : pkg . dependencies ? Object . keys ( pkg . dependencies ) : [ ] ,
108
53
external : [ ] ,
54
+ plugins : [ ] ,
109
55
} ;
110
- let liteBuildConfig = [ ] ;
111
56
112
57
// This non-generated client is an aggregation of client, hence does not follow
113
58
// the same build process.
114
59
if ( isAlgoliasearchClient ) {
115
60
const litePackageName = `${ packageName } /lite` ;
116
- // `algoliasearch/lite` related
117
- liteBuildConfig = [
61
+ baseConfigs . push (
118
62
{
119
63
...commonConfig ,
120
64
package : litePackageName ,
@@ -124,7 +68,7 @@ export function getPackageConfigs() {
124
68
formats : BROWSER_FORMATS ,
125
69
external : [ 'dom' ] ,
126
70
dependencies : [
127
- ... commonConfig . dependencies ,
71
+ ` ${ NPM_ORG } client-common` ,
128
72
`${ NPM_ORG } requester-browser-xhr` ,
129
73
] ,
130
74
globals : {
@@ -140,34 +84,20 @@ export function getPackageConfigs() {
140
84
input : 'lite/builds/node.ts' ,
141
85
formats : NODE_FORMATS ,
142
86
dependencies : [
143
- ... commonConfig . dependencies ,
87
+ ` ${ NPM_ORG } client-common` ,
144
88
`${ NPM_ORG } requester-node-http` ,
145
89
] ,
146
- } ,
147
- ] ;
148
-
149
- // `algoliasearch` related
150
- commonConfig . name = packageName ;
151
- commonConfig . dependencies = [
152
- `${ NPM_ORG } client-analytics` ,
153
- `${ NPM_ORG } client-common` ,
154
- `${ NPM_ORG } client-personalization` ,
155
- `${ NPM_ORG } client-search` ,
156
- ] ;
90
+ }
91
+ ) ;
157
92
}
158
93
159
- return [
160
- ...liteBuildConfig ,
94
+ baseConfigs . push (
161
95
// Browser build
162
96
{
163
97
...commonConfig ,
164
98
input : 'builds/browser.ts' ,
165
99
formats : BROWSER_FORMATS ,
166
100
external : [ 'dom' ] ,
167
- dependencies : [
168
- ...commonConfig . dependencies ,
169
- `${ NPM_ORG } requester-browser-xhr` ,
170
- ] ,
171
101
globals : {
172
102
[ packageName ] : packageName ,
173
103
} ,
@@ -177,36 +107,41 @@ export function getPackageConfigs() {
177
107
...commonConfig ,
178
108
input : 'builds/node.ts' ,
179
109
formats : NODE_FORMATS ,
180
- dependencies : [
181
- ...commonConfig . dependencies ,
182
- `${ NPM_ORG } requester-node-http` ,
183
- ] ,
184
- } ,
185
- ] ;
186
- } ) ;
110
+ }
111
+ ) ;
112
+
113
+ return baseConfigs ;
114
+ }
187
115
188
116
return [
189
- ...UTIL_CONFIGS . filter ( ( config ) => shouldBuildUtil ( config . package ) ) ,
190
- ...configs ,
117
+ {
118
+ ...UTILS [ packageName ] ,
119
+ formats : NODE_FORMATS ,
120
+ input : 'index.ts' ,
121
+ dependencies : pkg . dependencies ? Object . keys ( pkg . dependencies ) : [ ] ,
122
+ package : packageName ,
123
+ name : pkg . name ,
124
+ output : packageName ,
125
+ } ,
191
126
] ;
192
127
}
193
128
194
129
/**
195
130
* Returns the license at the top of the UMD bundled file.
196
131
*/
197
- export function createLicense ( name , version ) {
132
+ function createLicense ( name , version ) {
198
133
return `/*! ${ name } .umd.js | ${ version } | © Algolia, inc. | https://github.com/algolia/algoliasearch-client-javascript */` ;
199
134
}
200
135
201
136
/**
202
137
* Bundlers with their output format and file name for the given client.
203
138
*/
204
- export function createBundlers ( { output, clientPath , isLiteClient } ) {
139
+ function createBundlers ( { output, isLiteClient } ) {
205
140
const commonOptions = {
206
141
exports : 'named' ,
207
142
} ;
208
143
209
- const path = isLiteClient ? `${ clientPath } /dist/lite` : `${ clientPath } /dist` ;
144
+ const path = isLiteClient ? `. /dist/lite` : `. /dist` ;
210
145
211
146
return {
212
147
'esm-node' : {
@@ -232,3 +167,95 @@ export function createBundlers({ output, clientPath, isLiteClient }) {
232
167
} ,
233
168
} ;
234
169
}
170
+
171
+ export function buildConfigs ( pkg ) {
172
+ const baseConfigs = getBaseConfigs ( pkg ) ;
173
+ const rollupConfig = [ ] ;
174
+ let checkForTypes = true ;
175
+
176
+ baseConfigs . forEach ( ( baseConfig ) => {
177
+ const isLiteClient = baseConfig . name === 'algoliasearch/lite' ;
178
+ const bundlers = createBundlers ( {
179
+ output : baseConfig . output ,
180
+ isLiteClient,
181
+ } ) ;
182
+
183
+ baseConfig . formats . forEach ( ( format ) => {
184
+ const isUmdBuild = format === 'umd' ;
185
+ const isEsmBrowserBuild = format === 'esm-browser' ;
186
+ const umdConfig = {
187
+ compressorPlugins : [ ] ,
188
+ transpilerPlugins : [ ] ,
189
+ } ;
190
+
191
+ if ( isUmdBuild || isEsmBrowserBuild ) {
192
+ // eslint-disable-next-line no-param-reassign
193
+ baseConfig . dependencies = [ ] ;
194
+ }
195
+
196
+ if ( isUmdBuild ) {
197
+ bundlers [ format ] . name = baseConfig . name ;
198
+ bundlers [ format ] . banner = createLicense (
199
+ baseConfig . package ,
200
+ pkg . version
201
+ ) ;
202
+
203
+ umdConfig . compressorPlugins = [ terser ( ) ] ;
204
+ umdConfig . transpilerPlugins = [
205
+ babel ( {
206
+ babelrc : false ,
207
+ babelHelpers : 'runtime' ,
208
+ extensions : [ '.ts' ] ,
209
+ exclude : 'node_modules/**' ,
210
+ presets : [
211
+ [
212
+ '@babel/preset-env' ,
213
+ {
214
+ targets : {
215
+ browsers : [ '> .5%' , 'ie >= 11' ] ,
216
+ } ,
217
+ } ,
218
+ ] ,
219
+ ] ,
220
+ plugins : [ '@babel/plugin-transform-runtime' ] ,
221
+ } ) ,
222
+ ] ;
223
+ }
224
+
225
+ rollupConfig . push ( {
226
+ input : baseConfig . input ,
227
+ external : [ ...baseConfig . dependencies , ...baseConfig . external ] ,
228
+ plugins : [
229
+ globals ( {
230
+ global : true ,
231
+ } ) ,
232
+ nodeResolve ( ) ,
233
+ ts ( {
234
+ check : checkForTypes ,
235
+ tsconfig : 'tsconfig.json' ,
236
+ tsconfigOverride : {
237
+ compilerOptions : {
238
+ declaration : checkForTypes ,
239
+ declarationMap : checkForTypes ,
240
+ noEmit : ! checkForTypes ,
241
+ } ,
242
+ } ,
243
+ } ) ,
244
+ ...umdConfig . transpilerPlugins ,
245
+ ...umdConfig . compressorPlugins ,
246
+ ...baseConfig . plugins ,
247
+ ] ,
248
+ output : bundlers [ format ] ,
249
+ onwarn ( msg , warn ) {
250
+ if ( ! / C i r c u l a r / . test ( msg ) ) {
251
+ warn ( msg ) ;
252
+ }
253
+ } ,
254
+ } ) ;
255
+
256
+ checkForTypes = false ;
257
+ } ) ;
258
+ } ) ;
259
+
260
+ return rollupConfig ;
261
+ }
0 commit comments