From 623bc108f50df3c48d5833d41d8880038ad3e072 Mon Sep 17 00:00:00 2001 From: aminya Date: Fri, 31 Jul 2020 16:54:37 -0500 Subject: [PATCH] chore: fix prettier config --- .prettierignore | 2 + README.md | 25 ++--- .prettier.config.js => prettier.config.js | 2 +- src/main.ts | 127 ++++++++++------------ 4 files changed, 71 insertions(+), 85 deletions(-) rename .prettier.config.js => prettier.config.js (99%) diff --git a/.prettierignore b/.prettierignore index 7fe3a26..21c98ed 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1,6 +1,8 @@ node_modules package.json package-lock.json +pnpm-lock.yaml +changelog.md coverage build dist diff --git a/README.md b/README.md index b9c2bc7..5600780 100644 --- a/README.md +++ b/README.md @@ -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", @@ -41,7 +41,7 @@ module.exports = { }, ], plugins: plugins, -}; +} ``` ## API @@ -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()]) @@ -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] ``` diff --git a/.prettier.config.js b/prettier.config.js similarity index 99% rename from .prettier.config.js rename to prettier.config.js index d0e3a54..0883318 100644 --- a/.prettier.config.js +++ b/prettier.config.js @@ -22,4 +22,4 @@ module.exports = { }, }, ], -}; +} diff --git a/src/main.ts b/src/main.ts index b4de245..392baa1 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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" @@ -25,29 +25,26 @@ 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, - arr2: Array -): null | number { +function includesAny(arr1: Array, arr2: Array): 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( @@ -55,19 +52,14 @@ export function createPlugins( extraPlugins?: Array | boolean, extraPluginsDeprecated?: Array ) { - 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( @@ -75,10 +67,10 @@ export function createPlugins( 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])) } } @@ -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") } } @@ -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") { @@ -226,10 +215,10 @@ export function createPlugins( drop_console: false, }, }) - ); + ) } - return plugins; + return plugins } export function createConfig( @@ -251,5 +240,5 @@ export function createConfig( // loaded externally external: externals, plugins: plugins, - }; + } }