-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.js
137 lines (118 loc) · 3.76 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
import pkg from './package.json'
import path from 'path'
import babel from 'rollup-plugin-babel'
import builtins from 'rollup-plugin-node-builtins'
import commonjs from 'rollup-plugin-commonjs'
import json from 'rollup-plugin-json'
import license from "rollup-plugin-license"
import resolve from 'rollup-plugin-node-resolve'
import { terser } from "rollup-plugin-terser"
// how to use built in crypto with node js 8 and rollup
// - use rollup plugin node builtins
// • and set { crypto: false } when calling builtins()
// - define crypto as an external
// - import { createHmac } from "crypto"; // for a lean import
// • call `createHmac` in stead of crypto.createHmac(...)
let input = './lib/combell.js'
let external = ['axios', 'crypto'] // not included in built distribution files
let babelExclude = ['node_modules/**','**/*.json']
let globals = {} // example: { '_': 'lodash' }
let babelConfig = {
babelrc: false,
presets: [['env', { modules: false }]],
exclude: babelExclude,
plugins: ["external-helpers"]
}
const minified = true
const unminified = false
const jsFileExtension = '.js'
const minifiedJSFileExtension = '.min' + jsFileExtension
let licensify = function() {
return license({banner: {
file: path.join(__dirname, 'LICENSE'),
encoding: 'utf-8', // Default is utf-8
}})
}
// minifies source code files
// stripcomments strips comments but leaves @license tags in place
let terserPlugin = (stripComments) => {
let comments = stripComments ? null : (node, comment) => {
let {value,type} = comment
if (type == "comment2") {
// multiline comment
return /@preserve|@license|@cc_on/i.test(value);
}
}
return terser({ie8: false, sourceMap: true, output: {comments: comments}})
}
let plugins = (shouldMinify,isCommonJSBuild) => {
let b = babelConfig;
let c = null;
if( isCommonJSBuild ){
b.plugins = ["external-helpers","transform-runtime"];
b.runtimeHelpers = true;
c = commonjs({ include: "node_modules/**" });
}else{
b.runtimeHelpers = false;
}
let stripComments = true
return [
builtins({crypto: false}),
resolve(), // so Rollup can find dependencies
json(), // so Rollup can handle axios' package.json
c,
babel( b ),
shouldMinify ? terserPlugin(stripComments) : null,
licensify()
].filter( p => p )
}
let outputExtension = (minifiedExtension) => {
return minifiedExtension ? minifiedJSFileExtension : jsFileExtension;
}
let commonJSOutput = (compress) => {
// Since we are pointing to .min.js files in package.json
// we should find / replace .min.js with .js extensions.
const packageExt = outputExtension(true)
const outputExt = outputExtension( compress ? minified : unminified )
let main = pkg.main.replace( packageExt, outputExt)
let module = pkg.module.replace( packageExt, outputExt)
return [
{ file: main,
format: 'cjs', exports: 'named', globals: globals },
{ file: module,
format: 'es', globals: globals }
]
}
let umdOutput = (compress) => {
return {
name: 'combell',
file: pkg.browser.replace(outputExtension(true), outputExtension(compress ? minified : unminified)),
format: 'umd',
exports: 'named',
moduleName: pkg.name,
globals: globals
}
}
let browserBuild = (shouldMinify) => {
return {
input: input,
external: external,
output: umdOutput(shouldMinify),
plugins: plugins(shouldMinify,false)
}
}
let nodejsBuild = (shouldMinify) => {
return {
input: input,
external: external,
output: commonJSOutput(shouldMinify),
plugins: plugins(shouldMinify,true)
}
}
// exports an (un)minified version for each type of build
export default [
browserBuild( unminified ),
browserBuild( minified ),
nodejsBuild( unminified ),
nodejsBuild( minified )
]