1
- import includesAny from "array-includes-any" ;
2
-
3
1
// common plugins
4
2
import resolve from "@rollup/plugin-node-resolve" ;
5
3
import commonjs from "@rollup/plugin-commonjs" ;
6
4
import { terser } from "rollup-plugin-terser" ;
7
5
// @ts -ignore
8
6
import autoExternal from "rollup-plugin-auto-external" ;
9
7
8
+ import typescript from "@rollup/plugin-typescript" ;
9
+ import coffeescript from "rollup-plugin-coffee-script" ;
10
+ import json from "@rollup/plugin-json" ;
11
+ import cssOnly from "rollup-plugin-css-only" ;
12
+ import babel from "@rollup/plugin-babel" ;
13
+
14
+ export type Plugin =
15
+ | "js"
16
+ | "ts"
17
+ | "coffee"
18
+ | "json"
19
+ | "css"
20
+ | "babel"
21
+ | [ "ts" , typeof typescript ]
22
+ | [ "babel" , typeof babel ]
23
+ | [ "coffee" , typeof coffeescript ]
24
+ | [ "json" , typeof json ]
25
+ | [ "css" , typeof cssOnly ] ;
26
+
27
+ // function to check if the first array has any of the second array
28
+ // first array can have `[string, object]` as their input
29
+ function includesAny (
30
+ arr1 : Array < string | [ string , Object ] > ,
31
+ arr2 : Array < string >
32
+ ) : null | number {
33
+ for ( let index = 0 ; index < arr1 . length ; index ++ ) {
34
+ const elm = arr1 [ index ] ;
35
+ let name : string ;
36
+ if ( typeof elm === "string" ) {
37
+ // plugin name only
38
+ name = elm ;
39
+ } else {
40
+ // plugin with options
41
+ name = elm [ 0 ] ;
42
+ }
43
+ if ( arr2 . includes ( name ) ) {
44
+ return index ;
45
+ }
46
+ }
47
+ return null ;
48
+ }
49
+
10
50
export function createPlugins (
11
- languages : Array < string > = [ "ts" , "js" , "json" , "coffee" ] ,
12
- babel : boolean = true ,
13
- extraPlugins ?: Array < any >
51
+ inputPluginsNames : Array < Plugin > = [ "ts" , "js" , "json" , "coffee" ] ,
52
+ extraPlugins ?: Array < any > | boolean ,
53
+ extraPluginsDeprecated ?: Array < any >
14
54
) {
15
- let plugins = [ ]
55
+ let plugins = [ ] ;
16
56
17
57
// language specific
58
+
18
59
// typescript
19
- if ( includesAny ( languages , [ "ts" , ".ts" , "typescript" , "TypeScript" ] ) ) {
60
+ const tsIndex = includesAny ( inputPluginsNames , [
61
+ "ts" ,
62
+ ".ts" ,
63
+ "typescript" ,
64
+ "TypeScript" ,
65
+ ] ) ;
66
+ if ( tsIndex !== null ) {
20
67
const typescript = require ( "@rollup/plugin-typescript" ) ;
21
- plugins . push (
22
- typescript ( {
23
- noEmitOnError : false ,
24
- } )
25
- ) ;
68
+ if ( typeof inputPluginsNames [ tsIndex ] === "string" ) {
69
+ // plugin name only
70
+ plugins . push (
71
+ typescript ( {
72
+ noEmitOnError : false ,
73
+ } )
74
+ ) ;
75
+ } else {
76
+ // plugin with options
77
+ plugins . push ( typescript ( inputPluginsNames [ tsIndex ] [ 1 ] ) ) ;
78
+ }
26
79
}
80
+
27
81
// coffeescript
28
- if (
29
- includesAny ( languages , [
30
- " coffee",
31
- ".coffee ",
32
- "coffeescript ",
33
- "coffee-script ",
34
- "CoffeeScript ",
35
- ] )
36
- ) {
82
+ const coffeeIndex = includesAny ( inputPluginsNames , [
83
+ "coffee" ,
84
+ ". coffee",
85
+ "coffeescript ",
86
+ "coffee-script ",
87
+ "CoffeeScript ",
88
+ "cs ",
89
+ ] ) ;
90
+ if ( coffeeIndex !== null ) {
37
91
const coffeescript = require ( "rollup-plugin-coffee-script" ) ;
38
- plugins . push ( coffeescript ( ) ) ;
92
+ if ( typeof inputPluginsNames [ coffeeIndex ] === "string" ) {
93
+ // plugin name only
94
+ plugins . push ( coffeescript ( ) ) ;
95
+ } else {
96
+ // plugin with options
97
+ plugins . push ( coffeescript ( inputPluginsNames [ coffeeIndex ] [ 1 ] ) ) ;
98
+ }
39
99
}
100
+
40
101
// json
41
- if ( includesAny ( languages , [ "json" , ".json" , "JSON" ] ) ) {
102
+ const jsonIndex = includesAny ( inputPluginsNames , [ "json" , ".json" , "JSON" ] ) ;
103
+ if ( jsonIndex !== null ) {
42
104
const json = require ( "@rollup/plugin-json" ) ;
43
- plugins . push ( json ( { compact : true } ) ) ;
105
+ if ( typeof inputPluginsNames [ jsonIndex ] === "string" ) {
106
+ // plugin name only
107
+ plugins . push ( json ( { compact : true } ) ) ;
108
+ } else {
109
+ // plugin with options
110
+ plugins . push ( json ( inputPluginsNames [ jsonIndex ] [ 1 ] ) ) ;
111
+ }
44
112
}
45
113
46
114
// css only
47
- if ( includesAny ( languages , [ "css" , ".css" ] ) ) {
115
+ const cssIndex = includesAny ( inputPluginsNames , [ "css" , ".css" ] ) ;
116
+ if ( cssIndex !== null ) {
117
+ const cssOnly = require ( "rollup-plugin-css-only" ) ;
48
118
console . log ( `
49
119
css only was chosen to bundle css files into a single file.
50
120
This plugin requires you to import css files in a dummy js file and pass it as an input to rollup.
51
121
This should be done in a separate step from src code bundling
52
122
` ) ;
53
- const cssOnly = require ( "rollup-plugin-css-only" ) ;
54
- plugins . push ( cssOnly ( { output : "dist/bundle.css" } ) ) ;
123
+ if ( typeof inputPluginsNames [ cssIndex ] === "string" ) {
124
+ // plugin name only
125
+ plugins . push ( cssOnly ( { output : "dist/bundle.css" } ) ) ;
126
+ } else {
127
+ // plugin with options
128
+ plugins . push ( cssOnly ( inputPluginsNames [ cssIndex ] [ 1 ] ) ) ;
129
+ }
55
130
// minify css
56
131
if ( process . env . NODE_ENV === "production" ) {
132
+ // TODO get the output from the plugin when the user uses options
57
133
const execute = require ( "rollup-plugin-execute" ) ;
58
134
plugins . push ( execute ( [ "csso dist/bundle.css --output dist/bundle.css" ] ) ) ;
59
135
}
60
136
}
61
137
62
- // babel for js and coffee
63
- if ( babel || languages . includes ( "babel" ) ) {
64
- const { babel } = require ( "@rollup/plugin-babel" ) ;
65
- plugins . push (
66
- babel ( {
67
- extensions : [ ".js" , ".jsx" , ".mjs" , ".coffee" ] ,
68
- babelHelpers : "bundled" ,
69
- } )
138
+ // babel
139
+ let babelInput = extraPlugins ;
140
+ if ( typeof babelInput === "boolean" ) {
141
+ console . warn (
142
+ 'Setting babel with the second argument is depcrated. Pass "babel" like other plugins to the first argument'
70
143
) ;
71
144
}
72
145
146
+ const babelIndex = includesAny ( inputPluginsNames , [ "babel" ] ) ;
147
+ if ( babelIndex !== null || babelInput === true ) {
148
+ const { babel } = require ( "@rollup/plugin-babel" ) ;
149
+ if (
150
+ babelInput === true ||
151
+ typeof inputPluginsNames [ babelIndex ! ] === "string"
152
+ ) {
153
+ // plugin name only
154
+ plugins . push (
155
+ babel ( {
156
+ extensions : [ ".js" , ".jsx" , ".mjs" , ".coffee" ] ,
157
+ babelHelpers : "bundled" ,
158
+ } )
159
+ ) ;
160
+ } else {
161
+ // plugin with options
162
+ plugins . push ( babel ( inputPluginsNames [ babelIndex ! ] [ 1 ] ) ) ;
163
+ }
164
+ }
165
+
73
166
// extra plugins
74
- if ( extraPlugins ) {
167
+ if ( typeof extraPlugins !== "boolean" && extraPlugins !== undefined ) {
75
168
plugins . push ( ...extraPlugins ) ;
76
169
}
77
170
171
+ if ( extraPluginsDeprecated ) {
172
+ plugins . push ( ...extraPluginsDeprecated ) ;
173
+ }
174
+
78
175
let pluginsCommon = [
79
176
autoExternal ( {
80
177
builtins : true ,
@@ -92,7 +189,7 @@ export function createPlugins(
92
189
commonjs ( ) ,
93
190
] ;
94
191
95
- plugins . push ( ...pluginsCommon )
192
+ plugins . push ( ...pluginsCommon ) ;
96
193
97
194
// minify only in production mode
98
195
if ( process . env . NODE_ENV === "production" ) {
0 commit comments