-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.ts
More file actions
251 lines (230 loc) · 6.81 KB
/
main.ts
File metadata and controls
251 lines (230 loc) · 6.81 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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// common plugins
import resolve from "@rollup/plugin-node-resolve"
import commonjs from "@rollup/plugin-commonjs"
import { terser } from "rollup-plugin-terser"
import closureCompiler from '@ampproject/rollup-plugin-closure-compiler';
// @ts-ignore
import autoExternal from "rollup-plugin-auto-external"
import typescript from "@rollup/plugin-typescript"
import coffeescript from "rollup-plugin-coffee-script"
import json from "@rollup/plugin-json"
import cssOnly from "rollup-plugin-css-only"
import babel from "@rollup/plugin-babel"
import { wasm } from "@rollup/plugin-wasm"
export type Plugin =
| "js"
| "ts"
| "coffee"
| "json"
| "css"
| "babel"
| "wasm"
| ["ts", typeof typescript]
| ["babel", typeof babel]
| ["coffee", typeof coffeescript]
| ["json", typeof json]
| ["css", typeof cssOnly]
| ["wasm", typeof wasm]
// function to check if the first array has any of the second array
// first array can have `[string, object]` as their input
function includesAny(arr1: Array<string | [string, Object]>, arr2: Array<string>): null | number {
for (let index = 0; index < arr1.length; index++) {
const elm = arr1[index]
let name: string
if (typeof elm === "string") {
// plugin name only
name = elm
} else {
// plugin with options
name = elm[0]
}
if (arr2.includes(name)) {
return index
}
}
return null
}
export function createPlugins(
inputPluginsNames: Array<Plugin> = ["ts", "js", "json", "coffee"],
extraPlugins?: Array<any> | boolean,
extraPluginsDeprecated?: Array<any>
) {
let plugins = []
// language specific
// typescript
const tsIndex = includesAny(inputPluginsNames, ["ts", ".ts", "typescript", "TypeScript"])
if (tsIndex !== null) {
const typescript = require("@rollup/plugin-typescript")
if (typeof inputPluginsNames[tsIndex] === "string") {
// plugin name only
plugins.push(
typescript({
noEmitOnError: false,
module: "ESNext", // do not modify the imports
})
)
} else {
// plugin with options
plugins.push(typescript(inputPluginsNames[tsIndex][1]))
}
}
// coffeescript
const coffeeIndex = includesAny(inputPluginsNames, [
"coffee",
".coffee",
"coffeescript",
"coffee-script",
"CoffeeScript",
"cs",
])
if (coffeeIndex !== null) {
const coffeescript = require("rollup-plugin-coffee-script")
if (typeof inputPluginsNames[coffeeIndex] === "string") {
// plugin name only
plugins.push(coffeescript())
} else {
// plugin with options
plugins.push(coffeescript(inputPluginsNames[coffeeIndex][1]))
}
}
// json
const jsonIndex = includesAny(inputPluginsNames, ["json", ".json", "JSON"])
if (jsonIndex !== null) {
const json = require("@rollup/plugin-json")
if (typeof inputPluginsNames[jsonIndex] === "string") {
// plugin name only
plugins.push(json({ compact: true }))
} else {
// plugin with options
plugins.push(json(inputPluginsNames[jsonIndex][1]))
}
}
// css only
const cssIndex = includesAny(inputPluginsNames, ["css", ".css"])
if (cssIndex !== null) {
const cssOnly = require("rollup-plugin-css-only")
console.log(`
css only was chosen to bundle css files into a single file.
This plugin requires you to import css files in a dummy js file and pass it as an input to rollup.
This should be done in a separate step from src code bundling
`)
if (typeof inputPluginsNames[cssIndex] === "string") {
// plugin name only
plugins.push(cssOnly({ output: "dist/bundle.css" }))
} else {
// plugin with options
plugins.push(cssOnly(inputPluginsNames[cssIndex][1]))
}
// minify css
if (process.env.NODE_ENV === "production") {
// TODO get the output from the plugin when the user uses options
const execute = require("rollup-plugin-execute")
plugins.push(execute(["csso dist/bundle.css --output dist/bundle.css"]))
}
}
// babel
let babelInput = extraPlugins
if (typeof babelInput === "boolean") {
console.warn(
'Setting babel with the second argument is depcrated. Pass "babel" like other plugins to the first argument'
)
}
const babelIndex = includesAny(inputPluginsNames, ["babel"])
if (babelIndex !== null || babelInput === true) {
const { babel } = require("@rollup/plugin-babel")
if (babelInput === true || typeof inputPluginsNames[babelIndex!] === "string") {
// plugin name only
plugins.push(
babel({
extensions: [".js", ".jsx", ".mjs", ".coffee"],
babelHelpers: "bundled",
})
)
} else {
// plugin with options
plugins.push(babel(inputPluginsNames[babelIndex!][1]))
}
}
// wasm
const wasmIndex = includesAny(inputPluginsNames, ["wasm", "WebAssembly"])
if (wasmIndex !== null) {
const wasm = require("@rollup/plugin-wasm")
if (typeof inputPluginsNames[wasmIndex] === "string") {
// plugin name only
plugins.push(wasm())
} else {
// plugin with options
plugins.push(wasm(inputPluginsNames[wasmIndex][1]))
}
}
// extra plugins
if (typeof extraPlugins !== "boolean" && extraPlugins !== undefined) {
try {
plugins.push(...extraPlugins)
} catch (e) {
console.error("You should pass extraPlugins as an array")
}
}
if (extraPluginsDeprecated) {
try {
plugins.push(...extraPluginsDeprecated)
} catch (e) {
console.error("You should pass extraPluginsDeprecated as an array")
}
}
let pluginsCommon = [
autoExternal({
builtins: true,
dependencies: false,
peerDependencies: false,
}),
// so Rollup can find externals
resolve({
extensions: [".ts", ".js", ".coffee", ".tsx", ".jsx", ".mjs"],
preferBuiltins: true,
}),
// so Rollup can convert externals to an ES module
commonjs(),
]
plugins.push(...pluginsCommon)
// minify only in production mode
if (process.env.NODE_ENV === "production") {
plugins.push(
closureCompiler({
compilation_level: "SIMPLE",
language_in: "ECMASCRIPT_2018",
language_out: "ECMASCRIPT_2018",
strict_mode_input: true,
use_types_for_optimization: true
}),
)
// terser({
// ecma: 2018,
// warnings: true,
// compress: {
// drop_console: false,
// }),
}
return plugins
}
export function createConfig(
input: string | Array<string> = "src/main.ts",
output_dir: string = "dist",
output_format = "cjs",
externals: Array<string> = ["atom", "electron"],
plugins = createPlugins()
) {
return {
input: input,
output: [
{
dir: output_dir,
format: output_format,
sourcemap: true,
},
],
// loaded externally
external: externals,
plugins: plugins,
}
}