Skip to content

Commit 3de9d1b

Browse files
committed
major: support giving options
BREAKING CHANGE
1 parent c5fb693 commit 3de9d1b

File tree

1 file changed

+93
-37
lines changed

1 file changed

+93
-37
lines changed

src/main.ts

Lines changed: 93 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import includesAny from "array-includes-any";
2-
31
// common plugins
42
import resolve from "@rollup/plugin-node-resolve";
53
import commonjs from "@rollup/plugin-commonjs";
@@ -48,74 +46,132 @@ function includesAny(
4846
}
4947
return null;
5048
}
49+
5150
export function createPlugins(
52-
languages: Array<string> = ["ts", "js", "json", "coffee"],
53-
babel: boolean = true,
54-
extraPlugins?: Array<any>
51+
inputPluginsNames: Array<Plugin> = ["ts", "js", "json", "coffee"],
52+
extraPlugins?: Array<any> | boolean,
53+
extraPluginsDeprecated?: Array<any>
5554
) {
56-
let plugins = []
55+
let plugins = [];
5756

5857
// language specific
58+
5959
// typescript
60-
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) {
6167
const typescript = require("@rollup/plugin-typescript");
62-
plugins.push(
63-
typescript({
64-
noEmitOnError: false,
65-
})
66-
);
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+
}
6779
}
80+
6881
// coffeescript
69-
if (
70-
includesAny(languages, [
71-
"coffee",
72-
".coffee",
73-
"coffeescript",
74-
"coffee-script",
75-
"CoffeeScript",
76-
])
77-
) {
82+
const coffeeIndex = includesAny(inputPluginsNames, [
83+
"coffee",
84+
".coffee",
85+
"coffeescript",
86+
"coffee-script",
87+
"CoffeeScript",
88+
"cs",
89+
]);
90+
if (coffeeIndex !== null) {
7891
const coffeescript = require("rollup-plugin-coffee-script");
79-
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+
}
8099
}
100+
81101
// json
82-
if (includesAny(languages, ["json", ".json", "JSON"])) {
102+
const jsonIndex = includesAny(inputPluginsNames, ["json", ".json", "JSON"]);
103+
if (jsonIndex !== null) {
83104
const json = require("@rollup/plugin-json");
84-
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+
}
85112
}
86113

87114
// css only
88-
if (includesAny(languages, ["css", ".css"])) {
115+
const cssIndex = includesAny(inputPluginsNames, ["css", ".css"]);
116+
if (cssIndex !== null) {
117+
const cssOnly = require("rollup-plugin-css-only");
89118
console.log(`
90119
css only was chosen to bundle css files into a single file.
91120
This plugin requires you to import css files in a dummy js file and pass it as an input to rollup.
92121
This should be done in a separate step from src code bundling
93122
`);
94-
const cssOnly = require("rollup-plugin-css-only");
95-
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+
}
96130
// minify css
97131
if (process.env.NODE_ENV === "production") {
132+
// TODO get the output from the plugin when the user uses options
98133
const execute = require("rollup-plugin-execute");
99134
plugins.push(execute(["csso dist/bundle.css --output dist/bundle.css"]));
100135
}
101136
}
102137

103-
// babel for js and coffee
104-
if (babel || languages.includes("babel")) {
105-
const { babel } = require("@rollup/plugin-babel");
106-
plugins.push(
107-
babel({
108-
extensions: [".js", ".jsx", ".mjs", ".coffee"],
109-
babelHelpers: "bundled",
110-
})
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'
111143
);
112144
}
113145

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+
114166
// extra plugins
115-
if (extraPlugins) {
167+
if (typeof extraPlugins !== "boolean" && extraPlugins !== undefined) {
116168
plugins.push(...extraPlugins);
117169
}
118170

171+
if (extraPluginsDeprecated) {
172+
plugins.push(...extraPluginsDeprecated);
173+
}
174+
119175
let pluginsCommon = [
120176
autoExternal({
121177
builtins: true,
@@ -133,7 +189,7 @@ export function createPlugins(
133189
commonjs(),
134190
];
135191

136-
plugins.push(...pluginsCommon)
192+
plugins.push(...pluginsCommon);
137193

138194
// minify only in production mode
139195
if (process.env.NODE_ENV === "production") {

0 commit comments

Comments
 (0)