-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrollup.config.js
148 lines (118 loc) · 3.38 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
import { resolve, relative, extname, basename } from 'path';
import commonjs from '@rollup/plugin-commonjs';
import nodeResolve from '@rollup/plugin-node-resolve';
import typescript from 'rollup-plugin-typescript2';
import pkg from './package.json';
// Get the Default Source Folder
const src = `${resolve(__dirname, 'src')}/`;
/** Config Builder Helper */
const buildConfig = ({
/** Set path for base tsconfig.json to use */
tsconfig = './tsconfig.json',
/** Override tsConfig options */
tsconfigOverride = {},
/** Choose if must preserve components */
preserveModules = false,
/** Append extra plugins */
plugins = [],
/** Check if must use cache */
cache = false,
/** Get Output files */
output = [],
/** Build the source map */
sourcemap = true,
...rest
} = {}) => ({
/** Set the default entry */
input: 'src/index.ts',
/** Keep externals from peerDependencies */
external: Object.keys(pkg.peerDependencies || {}).concat(Object.keys(pkg.dependencies || {})),
/** Append Plugins */
plugins: [
/** Compile Typescript */
typescript({
/** Set the tsconfig file */
tsconfig,
/** Override mandatory ts options */
tsconfigOverride: {
compilerOptions: {
module: 'ES2015',
target: 'ES5',
noEmit: true,
...tsconfigOverride
}
},
/** Disable/enable cache */
clean : !cache
}),
/** User Node Module Resolver */
nodeResolve(),
/** Add the commonJS plugin */
commonjs(),
/** Extra plugins */
...plugins
],
/** Set Options by Args */
...(!preserveModules ? {} : {
/**
* Instead using the original preserve modules
* function of Rollup, must use a custom function to
* preserve each components and files, and bundle each
* external node_modules file into a single helper file
*/
manualChunks: (id) => {
/** Get filename and extension */
const ext = extname(id);
const filename = basename(id, ext);
/** If id come from node_modules, put into external helpers */
if (/node_modules/.test(id)) {
return `external-modules/${filename}`;
}
/** Get relative file path, stripping original extension */
const relativePath = relative(src, id).replace(ext, '');
/** If relative it's outside source folder, place into external helpers */
if (/\.\./.test(relativePath)) {
return `external-modules/${filename}`;
}
/** Else if is internal, return the relative path */
return relativePath;
}
}),
cache,
output: output.map((dest) => {
/** Enable/Disable the sourceMap for all output */
dest.sourcemap = sourcemap;
return dest;
}),
...rest
});
/** Export Rollup Configuration */
export default [
/** Build Modules */
buildConfig({
/** Build d.ts files */
tsconfigOverride: { target: 'ES2017' },
/** Set the Output */
output : [
{
dir : 'dist/module',
format : 'esm',
chunkFileNames : '[name].js',
hoistTransitiveImports: false,
minifyInternalExports : false
}
],
/** Preserve original module */
preserveModules: true
}),
/** Build Lib */
buildConfig({
/** Set the Output */
output: [
{
file : 'dist/lib/index.js',
format : 'cjs'
}
]
}),
];