-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrollup.config.js
More file actions
118 lines (110 loc) · 3.47 KB
/
Copy pathrollup.config.js
File metadata and controls
118 lines (110 loc) · 3.47 KB
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
import _ from 'lodash'
import commonjs from '@rollup/plugin-commonjs'
import copy from 'rollup-plugin-copy'
import mainPkg from './package.json'
import peerDepsExternal from 'rollup-plugin-peer-deps-external'
// Importing plugins for rollup.
import resolve from '@rollup/plugin-node-resolve'
import { terser } from 'rollup-plugin-terser'
const buildFolder = 'dist'
// Creating reference map between subpackage name and it folder name in hooks folder.
const subpackages = [
{ path: 'useFirebase', name: 'firebase' },
{ path: 'useAuthServices', name: 'auth' },
{ path: 'useDatabaseServices', name: 'database' },
{ path: 'useFirestoreServices', name: 'firestore' },
{ path: 'useStorageServices', name: 'storage' }
]
// Declaring peer dependencies for package. Get some from main package.json and add additional dependencies.
const peerDependencies = mainPkg.peerDependencies || {}
const external = [
...Object.keys(peerDependencies),
'react-dom',
'firebase/auth',
'firebase/database',
'firebase/firestore',
'firebase/storage'
]
// Configure additional plugins for build
const plugins = [
peerDepsExternal(), // peerDepsExternal plugin doing magic with dependencies and package size reduce twice.
terser(), // terser plugin doing code uglify and package size reduce twice.
commonjs(),
resolve()
]
const copyPluginConfig = subpackages.map(({ name, path }) => {
const sourceInputPath = `src/hooks/${path}`
return copy({
targets: [
// Copy package.json files into build folder to be able imports "subpackages"
{
src: `${sourceInputPath}/package.json`,
dest: name
}
]
})
})
/*
* Build script.
*
* First argument is config for main index.ts which includes imports from all subpackages
* and main declaration file which include declaration from subpackages.
* Second argument is generated build config for each subpackage.
*
* All values from map pass to lodash "merge" function to concat same fields into one config object to
* implement multiple source inputs in rollup(which in this way build package separately into different chunks,
* not into one index.esm.js file)
*
*/
export default _.merge(
[
/*
* Build main index to be able import all hooks or components you want from one place.
*/
{
input: {
[`${buildFolder}/index.esm`]: 'src/index.js'
},
/*
* To enable code splitting(chunks) in config need to be set output.dir, not output.file.
* And set chunkFileName to define how rollup will named chunks.
*/
output: {
dir: './',
format: 'es',
chunkFileNames: `${buildFolder}/esm/[name].js`
},
plugins: [...plugins, ...copyPluginConfig],
external
},
{
input: {
[`${buildFolder}/index.cjs`]: 'src/index.js'
},
output: {
dir: './',
format: 'cjs',
chunkFileNames: `${buildFolder}/cjs/[name].js`
},
plugins: [...plugins, ...copyPluginConfig],
external
}
],
...subpackages.map(({ name, path }) => {
// There is path to subpackage source folder.
const sourceInputPath = `src/hooks/${path}`
// Write here only fields which are different from main config and need to be concat with it.
return [
{
input: {
[`${name}/${buildFolder}/index.esm`]: `${sourceInputPath}/index.js`
}
},
{
input: {
[`${name}/${buildFolder}/index.cjs`]: `${sourceInputPath}/index.js`
}
}
]
})
)