Skip to content

Commit

Permalink
Merge pull request #4 from atom-ide-community/options
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya authored Jul 23, 2020
2 parents fa7bee6 + c746960 commit e3aeae1
Show file tree
Hide file tree
Showing 4 changed files with 199 additions and 73 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/bump_deps.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2-beta
with:
node-version: '14'
node-version: "14"
- run: |
npm ci
npm run bump
npm install
- uses: peter-evans/create-pull-request@v2
with:
token: ${{ secrets.GITHUB_TOKEN }}
commit-message: Update dependencies
commit-message: Update dependencies
title: "[AUTO] Update dependencies"
labels: Dependencies
branch: "Bump"
95 changes: 62 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ You should also install the peer dependencies:
```
"rollup": "2.21.0",
```

and the following (only those that you use are needed):

```
"typescript": "^3.9.6",
"coffeescript": "^1.12.7",
Expand All @@ -25,34 +27,62 @@ and the following (only those that you use are needed):
Create a `rollup.config.js` file at the root of the project with the following content. See API section for more details

```js
const { createPlugins, createConfig } = require("rollup-plugin-atomic");

const plugins = createPlugins(["ts", "js"], true);
const { createPlugins } = require("rollup-plugin-atomic");

const config = createConfig(
"src/main.ts",
"dist",
"cjs",
["atom", "electron", "node-pty-prebuilt-multiarch"],
plugins
);
const plugins = createPlugins(["ts", "babel"]);

module.exports = config;
module.exports = {
input: "src/main.ts",
output: [
{
dir: "dist",
format: "cjs",
sourcemap: true,
},
],
plugins: plugins,
};
```

## API

### createPlugins

use `createPlugins` to create the plugins you need.

```ts
createPlugins(
languages: Array<string> = ["ts", "js", "json", "coffee"], // languages you use
babel: boolean = true, // if you want to use babel
inputPlugins: Array<Plugin> = ["ts", "babel", "json", "coffee"], // languages/plugins you use
extraPlugins?: Array<any> // pass any extra plugins functions like `multientry()`
)
```

use `createConfig` to create the configs you need
which `inputPlugins` is among these:

```
ts
babel
coffee
json
css
(js is considered by default)
```

You can pass an input plugin with their supported option:

```js
createPlugins(["ts", {noEmitOnError: false, tsconfig: "./lib/tsconfig.json"})
```

For adding extra plugins, you can:
```ts
import multyentry from '@rollup/plugin-multi-entry'
createPlugins(["ts", multyentry())
```
### createConfig
You can use `createConfig` to create the configs you need. This is a simple wrapper around the rollup config.
```ts
createConfig(
Expand All @@ -63,28 +93,27 @@ createConfig(
plugins = createPlugins() // pass the plugins you created using `createPlugins()`
)
```
You can create multiple configs using `createConfig` and export them as an array:
```js
module.exports = [config1, config2]
```
## Only using createPlugins:
An example that uses `createConfig`:
you can only use `createPlugins` and then export your config with the typical rollup style:
```js
const { createPlugins } = require("rollup-plugin-atomic");
const { createPlugins, createConfig } = require("rollup-plugin-atomic");

const plugins = createPlugins(["ts", "js"], true);
const plugins = createPlugins(["ts", "babel"]);

module.exports = {
input: "src/main.ts",
output: [
{
dir: "dist",
format: "cjs",
sourcemap: true,
},
],
plugins: plugins,
}
const config = createConfig(
"src/main.ts",
"dist",
"cjs",
["atom", "electron", "node-pty-prebuilt-multiarch"],
plugins
);

module.exports = config;
```
You can create multiple configs using `createConfig` and export them as an array:
```js
module.exports = [config1, config2];
```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"license": "MIT",
"scripts": {
"format": "prettier --write .",
"tsc": "tsc -p src/tsconfig.json",
"tsc": "tsc -p src/tsconfig.json || echo done",
"build": "npm run tsc",
"prepare": "npm run build",
"bump": "ncu -u -x coffeescript"
Expand Down
171 changes: 134 additions & 37 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,80 +1,177 @@
import includesAny from "array-includes-any";

// common plugins
import resolve from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
import { terser } from "rollup-plugin-terser";
// @ts-ignore
import autoExternal from "rollup-plugin-auto-external";

import typescript from "@rollup/plugin-typescript";
import coffeescript from "rollup-plugin-coffee-script";
import json from "@rollup/plugin-json";
import cssOnly from "rollup-plugin-css-only";
import babel from "@rollup/plugin-babel";

export type Plugin =
| "js"
| "ts"
| "coffee"
| "json"
| "css"
| "babel"
| ["ts", typeof typescript]
| ["babel", typeof babel]
| ["coffee", typeof coffeescript]
| ["json", typeof json]
| ["css", typeof cssOnly];

// function to check if the first array has any of the second array
// first array can have `[string, object]` as their input
function includesAny(
arr1: Array<string | [string, Object]>,
arr2: Array<string>
): null | number {
for (let index = 0; index < arr1.length; index++) {
const elm = arr1[index];
let name: string;
if (typeof elm === "string") {
// plugin name only
name = elm;
} else {
// plugin with options
name = elm[0];
}
if (arr2.includes(name)) {
return index;
}
}
return null;
}

export function createPlugins(
languages: Array<string> = ["ts", "js", "json", "coffee"],
babel: boolean = true,
extraPlugins?: Array<any>
inputPluginsNames: Array<Plugin> = ["ts", "js", "json", "coffee"],
extraPlugins?: Array<any> | boolean,
extraPluginsDeprecated?: Array<any>
) {
let plugins = []
let plugins = [];

// language specific

// typescript
if (includesAny(languages, ["ts", ".ts", "typescript", "TypeScript"])) {
const tsIndex = includesAny(inputPluginsNames, [
"ts",
".ts",
"typescript",
"TypeScript",
]);
if (tsIndex !== null) {
const typescript = require("@rollup/plugin-typescript");
plugins.push(
typescript({
noEmitOnError: false,
})
);
if (typeof inputPluginsNames[tsIndex] === "string") {
// plugin name only
plugins.push(
typescript({
noEmitOnError: false,
})
);
} else {
// plugin with options
plugins.push(typescript(inputPluginsNames[tsIndex][1]));
}
}

// coffeescript
if (
includesAny(languages, [
"coffee",
".coffee",
"coffeescript",
"coffee-script",
"CoffeeScript",
])
) {
const coffeeIndex = includesAny(inputPluginsNames, [
"coffee",
".coffee",
"coffeescript",
"coffee-script",
"CoffeeScript",
"cs",
]);
if (coffeeIndex !== null) {
const coffeescript = require("rollup-plugin-coffee-script");
plugins.push(coffeescript());
if (typeof inputPluginsNames[coffeeIndex] === "string") {
// plugin name only
plugins.push(coffeescript());
} else {
// plugin with options
plugins.push(coffeescript(inputPluginsNames[coffeeIndex][1]));
}
}

// json
if (includesAny(languages, ["json", ".json", "JSON"])) {
const jsonIndex = includesAny(inputPluginsNames, ["json", ".json", "JSON"]);
if (jsonIndex !== null) {
const json = require("@rollup/plugin-json");
plugins.push(json({ compact: true }));
if (typeof inputPluginsNames[jsonIndex] === "string") {
// plugin name only
plugins.push(json({ compact: true }));
} else {
// plugin with options
plugins.push(json(inputPluginsNames[jsonIndex][1]));
}
}

// css only
if (includesAny(languages, ["css", ".css"])) {
const cssIndex = includesAny(inputPluginsNames, ["css", ".css"]);
if (cssIndex !== null) {
const cssOnly = require("rollup-plugin-css-only");
console.log(`
css only was chosen to bundle css files into a single file.
This plugin requires you to import css files in a dummy js file and pass it as an input to rollup.
This should be done in a separate step from src code bundling
`);
const cssOnly = require("rollup-plugin-css-only");
plugins.push(cssOnly({ output: "dist/bundle.css" }));
if (typeof inputPluginsNames[cssIndex] === "string") {
// plugin name only
plugins.push(cssOnly({ output: "dist/bundle.css" }));
} else {
// plugin with options
plugins.push(cssOnly(inputPluginsNames[cssIndex][1]));
}
// minify css
if (process.env.NODE_ENV === "production") {
// TODO get the output from the plugin when the user uses options
const execute = require("rollup-plugin-execute");
plugins.push(execute(["csso dist/bundle.css --output dist/bundle.css"]));
}
}

// babel for js and coffee
if (babel || languages.includes("babel")) {
const { babel } = require("@rollup/plugin-babel");
plugins.push(
babel({
extensions: [".js", ".jsx", ".mjs", ".coffee"],
babelHelpers: "bundled",
})
// babel
let babelInput = extraPlugins;
if (typeof babelInput === "boolean") {
console.warn(
'Setting babel with the second argument is depcrated. Pass "babel" like other plugins to the first argument'
);
}

const babelIndex = includesAny(inputPluginsNames, ["babel"]);
if (babelIndex !== null || babelInput === true) {
const { babel } = require("@rollup/plugin-babel");
if (
babelInput === true ||
typeof inputPluginsNames[babelIndex!] === "string"
) {
// plugin name only
plugins.push(
babel({
extensions: [".js", ".jsx", ".mjs", ".coffee"],
babelHelpers: "bundled",
})
);
} else {
// plugin with options
plugins.push(babel(inputPluginsNames[babelIndex!][1]));
}
}

// extra plugins
if (extraPlugins) {
if (typeof extraPlugins !== "boolean" && extraPlugins !== undefined) {
plugins.push(...extraPlugins);
}

if (extraPluginsDeprecated) {
plugins.push(...extraPluginsDeprecated);
}

let pluginsCommon = [
autoExternal({
builtins: true,
Expand All @@ -92,7 +189,7 @@ export function createPlugins(
commonjs(),
];

plugins.push(...pluginsCommon)
plugins.push(...pluginsCommon);

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

0 comments on commit e3aeae1

Please sign in to comment.