Skip to content

Commit

Permalink
chore: fix prettier config
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya committed Jul 31, 2020
1 parent a012cbc commit 623bc10
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 85 deletions.
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
node_modules
package.json
package-lock.json
pnpm-lock.yaml
changelog.md
coverage
build
dist
25 changes: 10 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ 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 } = require("rollup-plugin-atomic");
const { createPlugins } = require("rollup-plugin-atomic")

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

module.exports = {
input: "src/main.ts",
Expand All @@ -41,7 +41,7 @@ module.exports = {
},
],
plugins: plugins,
};
}
```

## API
Expand Down Expand Up @@ -74,11 +74,12 @@ You can pass an input plugin with their supported option:
```ts
const plugins = createPlugins([
["ts", { tsconfig: "./lib/tsconfig.json", noEmitOnError: false, module: "ESNext" }],
"js"
"js",
])
```

For adding extra plugins, you can:

```ts
import multyentry from '@rollup/plugin-multi-entry'
createPlugins(["ts", [multyentry()])
Expand All @@ -101,23 +102,17 @@ createConfig(
An example that uses `createConfig`:
```js
const { createPlugins, createConfig } = require("rollup-plugin-atomic");
const { createPlugins, createConfig } = require("rollup-plugin-atomic")

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

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

module.exports = config;
module.exports = config
```
You can create multiple configs using `createConfig` and export them as an array:
```js
module.exports = [config1, config2];
module.exports = [config1, config2]
```
2 changes: 1 addition & 1 deletion .prettier.config.js → prettier.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ module.exports = {
},
},
],
};
}
127 changes: 58 additions & 69 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
// common plugins
import resolve from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
import { terser } from "rollup-plugin-terser";
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 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";
import { wasm } from "@rollup/plugin-wasm";
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"
import { wasm } from "@rollup/plugin-wasm"

export type Plugin =
| "js"
Expand All @@ -25,60 +25,52 @@ export type Plugin =
| ["coffee", typeof coffeescript]
| ["json", typeof json]
| ["css", typeof cssOnly]
| ["wasm", typeof wasm];
| ["wasm", typeof wasm]

// 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 {
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;
const elm = arr1[index]
let name: string
if (typeof elm === "string") {
// plugin name only
name = elm;
name = elm
} else {
// plugin with options
name = elm[0];
name = elm[0]
}
if (arr2.includes(name)) {
return index;
return index
}
}
return null;
return null
}

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

// language specific

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

Expand All @@ -90,110 +82,107 @@ export function createPlugins(
"coffee-script",
"CoffeeScript",
"cs",
]);
])
if (coffeeIndex !== null) {
const coffeescript = require("rollup-plugin-coffee-script");
const coffeescript = require("rollup-plugin-coffee-script")
if (typeof inputPluginsNames[coffeeIndex] === "string") {
// plugin name only
plugins.push(coffeescript());
plugins.push(coffeescript())
} else {
// plugin with options
plugins.push(coffeescript(inputPluginsNames[coffeeIndex][1]));
plugins.push(coffeescript(inputPluginsNames[coffeeIndex][1]))
}
}

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

// css only
const cssIndex = includesAny(inputPluginsNames, ["css", ".css"]);
const cssIndex = includesAny(inputPluginsNames, ["css", ".css"])
if (cssIndex !== null) {
const cssOnly = require("rollup-plugin-css-only");
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
`);
`)
if (typeof inputPluginsNames[cssIndex] === "string") {
// plugin name only
plugins.push(cssOnly({ output: "dist/bundle.css" }));
plugins.push(cssOnly({ output: "dist/bundle.css" }))
} else {
// plugin with options
plugins.push(cssOnly(inputPluginsNames[cssIndex][1]));
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"]));
const execute = require("rollup-plugin-execute")
plugins.push(execute(["csso dist/bundle.css --output dist/bundle.css"]))
}
}

// babel
let babelInput = extraPlugins;
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"]);
const babelIndex = includesAny(inputPluginsNames, ["babel"])
if (babelIndex !== null || babelInput === true) {
const { babel } = require("@rollup/plugin-babel");
if (
babelInput === true ||
typeof inputPluginsNames[babelIndex!] === "string"
) {
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]));
plugins.push(babel(inputPluginsNames[babelIndex!][1]))
}
}

// wasm
const wasmIndex = includesAny(inputPluginsNames, ["wasm", "WebAssembly"]);
const wasmIndex = includesAny(inputPluginsNames, ["wasm", "WebAssembly"])
if (wasmIndex !== null) {
const wasm = require("@rollup/plugin-wasm");
const wasm = require("@rollup/plugin-wasm")
if (typeof inputPluginsNames[wasmIndex] === "string") {
// plugin name only
plugins.push(wasm());
plugins.push(wasm())
} else {
// plugin with options
plugins.push(wasm(inputPluginsNames[wasmIndex][1]));
plugins.push(wasm(inputPluginsNames[wasmIndex][1]))
}
}

// extra plugins
if (typeof extraPlugins !== "boolean" && extraPlugins !== undefined) {
try {
plugins.push(...extraPlugins);
plugins.push(...extraPlugins)
} catch (e) {
console.error("You should pass extraPlugins as an array");
console.error("You should pass extraPlugins as an array")
}
}

if (extraPluginsDeprecated) {
try {
plugins.push(...extraPluginsDeprecated);
plugins.push(...extraPluginsDeprecated)
} catch (e) {
console.error("You should pass extraPluginsDeprecated as an array");
console.error("You should pass extraPluginsDeprecated as an array")
}
}

Expand All @@ -212,9 +201,9 @@ export function createPlugins(

// so Rollup can convert externals to an ES module
commonjs(),
];
]

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

// minify only in production mode
if (process.env.NODE_ENV === "production") {
Expand All @@ -226,10 +215,10 @@ export function createPlugins(
drop_console: false,
},
})
);
)
}

return plugins;
return plugins
}

export function createConfig(
Expand All @@ -251,5 +240,5 @@ export function createConfig(
// loaded externally
external: externals,
plugins: plugins,
};
}
}

0 comments on commit 623bc10

Please sign in to comment.