Skip to content

Commit 2db419a

Browse files
committed
BREAKING: use pushPlugins
BREAKING: removed old babel deprecations
1 parent b44ff04 commit 2db419a

1 file changed

Lines changed: 71 additions & 155 deletions

File tree

src/main.ts

Lines changed: 71 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ export type Plugin =
2828
| "as"
2929
| "terser"
3030
| "replace"
31+
| "sourcemaps"
32+
| "commonjs"
33+
| "resolve"
3134
| ["ts", typeof typescript]
3235
| ["babel", typeof babel]
3336
| ["coffee", typeof coffeescript]
@@ -37,66 +40,32 @@ export type Plugin =
3740
| ["as", typeof asc]
3841
| ["terser", typeof terser]
3942
| ["replace", typeof replace]
43+
| ["sourcemaps", typeof sourcemaps]
44+
| ["commonjs", typeof commonjs]
45+
| ["resolve", typeof resolve]
4046

4147
export function createPlugins(
4248
inputPluginsNames: Array<Plugin> = ["ts", "js", "json", "coffee"],
43-
extraPlugins?: Array<any> | boolean,
44-
extraPluginsDeprecated?: Array<any>
49+
extraPlugins?: Array<any>
4550
) {
4651
let plugins = []
4752

4853
// language specific
4954

5055
// typescript
51-
const tsIndex = includesAny(inputPluginsNames, ["ts", ".ts", "typescript", "TypeScript"])
52-
if (tsIndex !== null) {
53-
const typescript = require("@rollup/plugin-typescript")
54-
if (typeof inputPluginsNames[tsIndex] === "string") {
55-
// plugin name only
56-
plugins.push(
57-
typescript({
58-
noEmitOnError: false,
59-
module: "ESNext", // do not modify the imports
60-
})
61-
)
62-
} else {
63-
// plugin with options
64-
plugins.push(typescript(inputPluginsNames[tsIndex][1]))
65-
}
66-
}
56+
pushPlugin(["ts", ".ts", "typescript", "TypeScript"], ["@rollup/plugin-typescript"], {
57+
noEmitOnError: false,
58+
module: "ESNext", // do not modify the imports
59+
})
6760

6861
// coffeescript
69-
const coffeeIndex = includesAny(inputPluginsNames, [
70-
"coffee",
71-
".coffee",
72-
"coffeescript",
73-
"coffee-script",
74-
"CoffeeScript",
75-
"cs",
76-
])
77-
if (coffeeIndex !== null) {
78-
const coffeescript = require("rollup-plugin-coffee-script")
79-
if (typeof inputPluginsNames[coffeeIndex] === "string") {
80-
// plugin name only
81-
plugins.push(coffeescript())
82-
} else {
83-
// plugin with options
84-
plugins.push(coffeescript(inputPluginsNames[coffeeIndex][1]))
85-
}
86-
}
62+
pushPlugin(
63+
["coffee", ".coffee", "coffeescript", "coffee-script", "CoffeeScript", "cs"],
64+
["rollup-plugin-coffee-script"]
65+
)
8766

8867
// json
89-
const jsonIndex = includesAny(inputPluginsNames, ["json", ".json", "JSON"])
90-
if (jsonIndex !== null) {
91-
const json = require("@rollup/plugin-json")
92-
if (typeof inputPluginsNames[jsonIndex] === "string") {
93-
// plugin name only
94-
plugins.push(json({ compact: true }))
95-
} else {
96-
// plugin with options
97-
plugins.push(json(inputPluginsNames[jsonIndex][1]))
98-
}
99-
}
68+
pushPlugin(["json", ".json", "JSON"], ["@rollup/plugin-json"], { compact: true })
10069

10170
// css only
10271
const cssIndex = includesAny(inputPluginsNames, ["css", ".css"])
@@ -122,146 +91,93 @@ export function createPlugins(
12291
}
12392
}
12493

125-
// babel
126-
let babelInput = extraPlugins
127-
if (typeof babelInput === "boolean") {
128-
console.warn(
129-
'Setting babel with the second argument is depcrated. Pass "babel" like other plugins to the first argument'
130-
)
131-
}
132-
133-
const babelIndex = includesAny(inputPluginsNames, ["babel"])
134-
if (babelIndex !== null || babelInput === true) {
135-
const { babel } = require("@rollup/plugin-babel")
136-
if (babelInput === true || typeof inputPluginsNames[babelIndex!] === "string") {
137-
// plugin name only
138-
plugins.push(
139-
babel({
140-
extensions: [".js", ".jsx", ".mjs", ".coffee"],
141-
babelHelpers: "bundled",
142-
})
143-
)
144-
} else {
145-
// plugin with options
146-
plugins.push(babel(inputPluginsNames[babelIndex!][1]))
147-
}
148-
}
94+
pushPlugin(["babel"], ["@rollup/plugin-babel", "babel"], {
95+
extensions: [".js", ".jsx", ".mjs", ".coffee"],
96+
babelHelpers: "bundled",
97+
})
14998

15099
// wasm
151-
const wasmIndex = includesAny(inputPluginsNames, ["wasm", "WebAssembly"])
152-
if (wasmIndex !== null) {
153-
const wasm = require("@rollup/plugin-wasm")
154-
if (typeof inputPluginsNames[wasmIndex] === "string") {
155-
// plugin name only
156-
plugins.push(wasm())
157-
} else {
158-
// plugin with options
159-
plugins.push(wasm(inputPluginsNames[wasmIndex][1]))
160-
}
161-
}
100+
pushPlugin(["wasm", "WebAssembly"], ["@rollup/plugin-wasm", "wasm"])
162101

163102
// as
164-
const ascIndex = includesAny(inputPluginsNames, ["as", "asc", "assemblyscript", "AssemblyScript"])
165-
if (ascIndex !== null) {
166-
const { asc } = require("rollup-plugin-assemblyscript")
167-
if (typeof inputPluginsNames[ascIndex] === "string") {
168-
// plugin name only
169-
plugins.push(asc())
170-
} else {
171-
// plugin with options
172-
plugins.push(asc(inputPluginsNames[ascIndex][1]))
173-
}
174-
}
103+
pushPlugin(["as", "asc", "assemblyscript", "AssemblyScript"], ["rollup-plugin-assemblyscript", "asc"])
175104

176105
// visualizer
177-
const visualizerIndex = includesAny(inputPluginsNames, ["visualizer", "plot"])
178-
if (visualizerIndex !== null) {
179-
const visualizer = require("rollup-plugin-visualizer")
180-
if (typeof inputPluginsNames[visualizerIndex] === "string") {
181-
// plugin name only
182-
plugins.push(visualizer({sourcemap: true, open: true}))
183-
} else {
184-
// plugin with options
185-
plugins.push(visualizer(inputPluginsNames[visualizerIndex][1]))
186-
}
187-
}
106+
pushPlugin(["visualizer", "plot"], ["rollup-plugin-visualizer"], { sourcemap: true, open: true })
188107

189108
// extra plugins
190-
if (typeof extraPlugins !== "boolean" && extraPlugins !== undefined) {
109+
if (extraPlugins !== undefined && typeof extraPlugins === "object" /*array*/) {
191110
try {
192111
plugins.push(...extraPlugins)
193112
} catch (e) {
194113
console.error("You should pass extraPlugins as an array")
195114
}
196115
}
197116

198-
if (extraPluginsDeprecated) {
199-
try {
200-
plugins.push(...extraPluginsDeprecated)
201-
} catch (e) {
202-
console.error("You should pass extraPluginsDeprecated as an array")
203-
}
204-
}
117+
// Default plugins
205118

206-
let pluginsCommon = [
207-
// loading files with existing source maps
208-
sourcemaps(),
119+
// loading files with existing source maps
120+
pushPlugin(["sourcemaps"], ["rollup-plugin-sourcemaps"], {}, true)
209121

210-
autoExternal({
122+
pushPlugin(
123+
["autoExternal"],
124+
["rollup-plugin-auto-external"],
125+
{
211126
builtins: true,
212127
dependencies: false,
213128
peerDependencies: false,
214-
}),
215-
216-
// so Rollup can find externals
217-
resolve({
218-
mainFields: ['module', 'exports', 'es', 'es6', 'esm', 'main'],
129+
},
130+
true
131+
)
132+
133+
// so Rollup can find externals
134+
pushPlugin(
135+
["resolve"],
136+
["@rollup/plugin-node-resolve"],
137+
{
138+
mainFields: ["module", "exports", "es", "es6", "esm", "main"],
219139
extensions: [".ts", ".js", ".coffee", ".tsx", ".jsx", ".mjs", ".node", ".json"],
220140
preferBuiltins: true,
221141
dedupe: [],
222-
}),
223-
224-
// so Rollup can convert externals to an ES module
225-
commonjs({
226-
transformMixedEsModules: true
227-
}),
228-
]
229-
230-
plugins.push(...pluginsCommon)
142+
},
143+
true
144+
)
145+
146+
// so Rollup can convert externals to an ES module
147+
pushPlugin(
148+
["commonjs"],
149+
["@rollup/plugin-commonjs"],
150+
{
151+
transformMixedEsModules: true,
152+
},
153+
true
154+
)
231155

232156
// replace
233-
const replaceIndex = includesAny(inputPluginsNames, ["replace"])
234-
if (replaceIndex !== null && typeof inputPluginsNames[replaceIndex] === "string") {
235-
// plugin with options
236-
plugins.push(replace(inputPluginsNames[replaceIndex][1]))
237-
} else {
238-
if (process.env.NODE_ENV === "production") {
239-
plugins.push(
240-
// set NODE_ENV to production
241-
replace({
242-
'process.env.NODE_ENV':JSON.stringify('production'),
243-
}),
244-
)
245-
}
246-
}
157+
pushPlugin(
158+
["replace"],
159+
["@rollup/plugin-replace"],
160+
{
161+
"process.env.NODE_ENV": JSON.stringify(process.env.NODE_ENV),
162+
},
163+
true
164+
)
247165

248166
// terser
249-
const terserIndex = includesAny(inputPluginsNames, ["terser"])
250-
if (terserIndex !== null && typeof inputPluginsNames[terserIndex] === "string") {
251-
// plugin with options
252-
plugins.push(terser(inputPluginsNames[terserIndex][1]))
253-
} else {
254-
if (process.env.NODE_ENV === "production") {
255-
plugins.push(
256-
// minify
257-
terser({
167+
pushPlugin(
168+
["terser"],
169+
["rollup-plugin-terser", "terser"],
170+
process.env.NODE_ENV === "production"
171+
? {
258172
ecma: 2018,
259173
warnings: true,
260174
compress: {
261175
drop_console: false,
262176
},
263-
}),
264-
)
177+
}
178+
: {},
179+
true
180+
)
265181

266182
// utility function that pushes a plugin
267183
function pushPlugin(

0 commit comments

Comments
 (0)