Skip to content

Commit e3aeae1

Browse files
authored
Merge pull request #4 from atom-ide-community/options
2 parents fa7bee6 + c746960 commit e3aeae1

File tree

4 files changed

+199
-73
lines changed

4 files changed

+199
-73
lines changed

.github/workflows/bump_deps.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ jobs:
1111
- uses: actions/checkout@v2
1212
- uses: actions/setup-node@v2-beta
1313
with:
14-
node-version: '14'
14+
node-version: "14"
1515
- run: |
1616
npm ci
1717
npm run bump
1818
npm install
1919
- uses: peter-evans/create-pull-request@v2
2020
with:
2121
token: ${{ secrets.GITHUB_TOKEN }}
22-
commit-message: Update dependencies
22+
commit-message: Update dependencies
2323
title: "[AUTO] Update dependencies"
2424
labels: Dependencies
2525
branch: "Bump"

README.md

Lines changed: 62 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ You should also install the peer dependencies:
1313
```
1414
"rollup": "2.21.0",
1515
```
16+
1617
and the following (only those that you use are needed):
18+
1719
```
1820
"typescript": "^3.9.6",
1921
"coffeescript": "^1.12.7",
@@ -25,34 +27,62 @@ and the following (only those that you use are needed):
2527
Create a `rollup.config.js` file at the root of the project with the following content. See API section for more details
2628

2729
```js
28-
const { createPlugins, createConfig } = require("rollup-plugin-atomic");
29-
30-
const plugins = createPlugins(["ts", "js"], true);
30+
const { createPlugins } = require("rollup-plugin-atomic");
3131

32-
const config = createConfig(
33-
"src/main.ts",
34-
"dist",
35-
"cjs",
36-
["atom", "electron", "node-pty-prebuilt-multiarch"],
37-
plugins
38-
);
32+
const plugins = createPlugins(["ts", "babel"]);
3933

40-
module.exports = config;
34+
module.exports = {
35+
input: "src/main.ts",
36+
output: [
37+
{
38+
dir: "dist",
39+
format: "cjs",
40+
sourcemap: true,
41+
},
42+
],
43+
plugins: plugins,
44+
};
4145
```
4246

4347
## API
4448

49+
### createPlugins
50+
4551
use `createPlugins` to create the plugins you need.
4652

4753
```ts
4854
createPlugins(
49-
languages: Array<string> = ["ts", "js", "json", "coffee"], // languages you use
50-
babel: boolean = true, // if you want to use babel
55+
inputPlugins: Array<Plugin> = ["ts", "babel", "json", "coffee"], // languages/plugins you use
5156
extraPlugins?: Array<any> // pass any extra plugins functions like `multientry()`
5257
)
5358
```
5459

55-
use `createConfig` to create the configs you need
60+
which `inputPlugins` is among these:
61+
62+
```
63+
ts
64+
babel
65+
coffee
66+
json
67+
css
68+
(js is considered by default)
69+
```
70+
71+
You can pass an input plugin with their supported option:
72+
73+
```js
74+
createPlugins(["ts", {noEmitOnError: false, tsconfig: "./lib/tsconfig.json"})
75+
```
76+
77+
For adding extra plugins, you can:
78+
```ts
79+
import multyentry from '@rollup/plugin-multi-entry'
80+
createPlugins(["ts", multyentry())
81+
```
82+
83+
### createConfig
84+
85+
You can use `createConfig` to create the configs you need. This is a simple wrapper around the rollup config.
5686
5787
```ts
5888
createConfig(
@@ -63,28 +93,27 @@ createConfig(
6393
plugins = createPlugins() // pass the plugins you created using `createPlugins()`
6494
)
6595
```
66-
You can create multiple configs using `createConfig` and export them as an array:
67-
```js
68-
module.exports = [config1, config2]
69-
```
7096
71-
## Only using createPlugins:
97+
An example that uses `createConfig`:
7298
73-
you can only use `createPlugins` and then export your config with the typical rollup style:
7499
```js
75-
const { createPlugins } = require("rollup-plugin-atomic");
100+
const { createPlugins, createConfig } = require("rollup-plugin-atomic");
76101

77-
const plugins = createPlugins(["ts", "js"], true);
102+
const plugins = createPlugins(["ts", "babel"]);
78103

79-
module.exports = {
80-
input: "src/main.ts",
81-
output: [
82-
{
83-
dir: "dist",
84-
format: "cjs",
85-
sourcemap: true,
86-
},
87-
],
88-
plugins: plugins,
89-
}
104+
const config = createConfig(
105+
"src/main.ts",
106+
"dist",
107+
"cjs",
108+
["atom", "electron", "node-pty-prebuilt-multiarch"],
109+
plugins
110+
);
111+
112+
module.exports = config;
113+
```
114+
115+
You can create multiple configs using `createConfig` and export them as an array:
116+
117+
```js
118+
module.exports = [config1, config2];
90119
```

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"license": "MIT",
1414
"scripts": {
1515
"format": "prettier --write .",
16-
"tsc": "tsc -p src/tsconfig.json",
16+
"tsc": "tsc -p src/tsconfig.json || echo done",
1717
"build": "npm run tsc",
1818
"prepare": "npm run build",
1919
"bump": "ncu -u -x coffeescript"

src/main.ts

Lines changed: 134 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,177 @@
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";
64
import { terser } from "rollup-plugin-terser";
75
// @ts-ignore
86
import 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+
1050
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>
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

Comments
 (0)