1- import includesAny from "array-includes-any" ;
2-
31// common plugins
42import resolve from "@rollup/plugin-node-resolve" ;
53import commonjs from "@rollup/plugin-commonjs" ;
64import { terser } from "rollup-plugin-terser" ;
75// @ts -ignore
86import autoExternal from "rollup-plugin-auto-external" ;
97
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+
1050export 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 >
1454) {
15- let plugins = [ ]
55+ let plugins = [ ] ;
1656
1757 // language specific
58+
1859 // 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 ) {
2067 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+ }
2679 }
80+
2781 // 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 ) {
3791 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+ }
3999 }
100+
40101 // json
41- if ( includesAny ( languages , [ "json" , ".json" , "JSON" ] ) ) {
102+ const jsonIndex = includesAny ( inputPluginsNames , [ "json" , ".json" , "JSON" ] ) ;
103+ if ( jsonIndex !== null ) {
42104 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+ }
44112 }
45113
46114 // 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" ) ;
48118 console . log ( `
49119 css only was chosen to bundle css files into a single file.
50120 This plugin requires you to import css files in a dummy js file and pass it as an input to rollup.
51121 This should be done in a separate step from src code bundling
52122 ` ) ;
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+ }
55130 // minify css
56131 if ( process . env . NODE_ENV === "production" ) {
132+ // TODO get the output from the plugin when the user uses options
57133 const execute = require ( "rollup-plugin-execute" ) ;
58134 plugins . push ( execute ( [ "csso dist/bundle.css --output dist/bundle.css" ] ) ) ;
59135 }
60136 }
61137
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'
70143 ) ;
71144 }
72145
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+
73166 // extra plugins
74- if ( extraPlugins ) {
167+ if ( typeof extraPlugins !== "boolean" && extraPlugins !== undefined ) {
75168 plugins . push ( ...extraPlugins ) ;
76169 }
77170
171+ if ( extraPluginsDeprecated ) {
172+ plugins . push ( ...extraPluginsDeprecated ) ;
173+ }
174+
78175 let pluginsCommon = [
79176 autoExternal ( {
80177 builtins : true ,
@@ -92,7 +189,7 @@ export function createPlugins(
92189 commonjs ( ) ,
93190 ] ;
94191
95- plugins . push ( ...pluginsCommon )
192+ plugins . push ( ...pluginsCommon ) ;
96193
97194 // minify only in production mode
98195 if ( process . env . NODE_ENV === "production" ) {
0 commit comments