From 1a7cb1ae6174ed5726c4e5d48b2eaae6e529ebe9 Mon Sep 17 00:00:00 2001 From: Amin Yahyaabadi Date: Wed, 31 Mar 2021 10:54:45 -0500 Subject: [PATCH] chore: add prettier + format --- .prettierignore | 5 +++ README.md | 52 ++++++++++++++++------------ index.js | 91 +++++++++++++++++++++++++++++++++---------------- package.json | 4 ++- pnpm-lock.yaml | 15 ++++++++ 5 files changed, 116 insertions(+), 51 deletions(-) create mode 100644 .prettierignore diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 0000000..80d2c46 --- /dev/null +++ b/.prettierignore @@ -0,0 +1,5 @@ +node_modules +pnpm-lock.yaml +package-lock.json +CHANGELOG.md +dist diff --git a/README.md b/README.md index 66bcaf2..8fea3fa 100644 --- a/README.md +++ b/README.md @@ -3,43 +3,47 @@ This includes the babel configuration used for JavaScript packages in atom-ide-community. ## Installation + ``` npm install --save-dev babel-preset-atomic ``` You should also install the peer dependencies: + ``` npm install -save-dev "@babel/core" npm install -save-dev "@babel/cli" ``` ## Usage + Create a `babel.config.js` file at the root of the project with the following content: + ```js -let presets = [ - "babel-preset-atomic", -]; +let presets = ["babel-preset-atomic"] -let plugins = []; +let plugins = [] module.exports = { presets: presets, plugins: plugins, exclude: "node_modules/**", sourceMap: "inline", -}; +} ``` ## Options -1) `keepModules` +1. `keepModules` If you want to keep the ES modules as they are (not transforming `import` to `require`), set `BABEL_KEEP_MODULES` environment variable to `true`. This is useful with bundlers which need you to keep ES6 modules intact. By default the ES6 modules are transformed to ES5 (the value is `false`) + ``` cross-env BABEL_KEEP_MODULES=true ``` To permanently set this option, you can add it to your babel config (which disables environment variable effectiveness): + ```js let presets = [ [ @@ -48,12 +52,13 @@ let presets = [ keepModules: true, }, ], -]; +] ``` -2) `targets` +2. `targets` To change the target of `preset-env` plugin. By default this is configured for Electron. + ```js let presets = [ [ @@ -61,13 +66,13 @@ let presets = [ { targets: { electron: 6, - } + }, }, ], -]; +] ``` -3) `addModuleExports`: +3. `addModuleExports`: Allows to `require` a ES6 module that has exported a single thing as `default`, in a ES5 fashion without `require().default`. This is `true` by default for backward compatibility with Atom packages. @@ -76,13 +81,13 @@ let presets = [ [ "babel-preset-atomic", { - addModuleExports: false + addModuleExports: false, }, ], -]; +] ``` -4) `addModuleExportsDefaultProperty`: +4. `addModuleExportsDefaultProperty`: ```js let presets = [ @@ -90,43 +95,45 @@ let presets = [ "babel-preset-atomic", { addModuleExports: true, - addModuleExportsDefaultProperty: true + addModuleExportsDefaultProperty: true, }, ], -]; +] ``` Adds `default` property to `module.exports` so the ES6 module can be required in the ES6 fashion as well (by `require().default`). This is `false` by default. -6) `react` +6. `react` Enable `"@babel/preset-react"`. `true` by default. -7) `flow` +7. `flow` Enable `"@babel/preset-flow"`. `true` by default. -7) `typescript` +7. `typescript` Enable `"@babel/preset-typescript"`. `true` by default. -9) `removeAllUseStrict` +9. `removeAllUseStrict` Remove all `'use strict'` from all files. Passed to [`babel-plugin-transform-not-strict`](https://github.com/atom-ide-community/babel-plugin-transform-not-strict#usage-remove-all). This is `false` by default. -10) `notStrictDirectiveTriggers` and `notStrictCommentTriggers` +10. `notStrictDirectiveTriggers` and `notStrictCommentTriggers` These specify `"not strict"` triggers. Passed to [`babel-plugin-transform-not-strict`](https://github.com/atom-ide-community/babel-plugin-transform-not-strict#usage-extra-directive-or-comment-triggers). ## Behind the scenes It includes the following presets: + - `"@babel/preset-env"` (configured for `electron`) - `"@babel/preset-react"` - `"@babel/preset-flow"` - `"@babel/preset-typescript"` It also includes all the proposal plugins such as: + - `"@babel/plugin-proposal-optional-chaining"` - `"@babel/plugin-proposal-nullish-coalescing-operator"` - `"@babel/plugin-proposal-export-default-from"` @@ -134,11 +141,14 @@ It also includes all the proposal plugins such as: - ... It includes the plugins for compile time code generation: + - `"babel-plugin-codegen"` - `"babel-plugin-preval"` It has the preset that automatically adds default export for older Node versions (so no `require().default` is needed). + - `"babel-plugin-add-module-exports"` It has the plugin for removing `'use strict'`: + - `"babel-plugin-transform-not-strict"` diff --git a/index.js b/index.js index 0f99cff..5fd9de3 100644 --- a/index.js +++ b/index.js @@ -5,7 +5,18 @@ if (process.env.BABEL_KEEP_MODULES === "true") { } function handleOptions(options) { - let {targets, keepModules, addModuleExports, addModuleExportsDefaultProperty, react, flow, typescript, removeAllUseStrict, notStrictDirectiveTriggers, notStrictCommentTriggers } = options + let { + targets, + keepModules, + addModuleExports, + addModuleExportsDefaultProperty, + react, + flow, + typescript, + removeAllUseStrict, + notStrictDirectiveTriggers, + notStrictCommentTriggers, + } = options // use Electron 5 targets by default if (targets == null) { @@ -45,45 +56,60 @@ function handleOptions(options) { removeAllUseStrict = false } if (notStrictDirectiveTriggers == null) { - notStrictDirectiveTriggers = ['use babel'] + notStrictDirectiveTriggers = ["use babel"] } if (notStrictCommentTriggers == null) { - notStrictCommentTriggers = ['@babel', '@flow', '* @babel', '* @flow'] + notStrictCommentTriggers = ["@babel", "@flow", "* @babel", "* @flow"] } - return {targets, keepModules, addModuleExports, addModuleExportsDefaultProperty, react, flow, typescript, removeAllUseStrict, notStrictDirectiveTriggers, notStrictCommentTriggers } + return { + targets, + keepModules, + addModuleExports, + addModuleExportsDefaultProperty, + react, + flow, + typescript, + removeAllUseStrict, + notStrictDirectiveTriggers, + notStrictCommentTriggers, + } } module.exports = (api, options, dirname) => { - - const {targets, keepModules, addModuleExports, addModuleExportsDefaultProperty, react, flow, typescript, removeAllUseStrict, notStrictDirectiveTriggers, notStrictCommentTriggers } = handleOptions(options) + const { + targets, + keepModules, + addModuleExports, + addModuleExportsDefaultProperty, + react, + flow, + typescript, + removeAllUseStrict, + notStrictDirectiveTriggers, + notStrictCommentTriggers, + } = handleOptions(options) let presets = [ [ require("@babel/preset-env"), { targets: targets, - modules: keepModules ? false : "commonjs" + modules: keepModules ? false : "commonjs", }, ], - ]; + ] if (react) { - presets.push(...[ - require("@babel/preset-react"), - ]); + presets.push(...[require("@babel/preset-react")]) } if (flow) { - presets.push(...[ - require("@babel/preset-flow"), - ]); + presets.push(...[require("@babel/preset-flow")]) } if (typescript) { - presets.push(...[ - require("@babel/preset-typescript"), - ]); + presets.push(...[require("@babel/preset-typescript")]) } let plugins = [ @@ -112,29 +138,36 @@ module.exports = (api, options, dirname) => { require("babel-plugin-preval"), // not strict - [require("babel-plugin-transform-not-strict"), {removeAll: removeAllUseStrict, directiveTriggers: notStrictDirectiveTriggers, commentTriggers: notStrictCommentTriggers}], + [ + require("babel-plugin-transform-not-strict"), + { + removeAll: removeAllUseStrict, + directiveTriggers: notStrictDirectiveTriggers, + commentTriggers: notStrictCommentTriggers, + }, + ], // reserved keywords - require("@babel/plugin-transform-reserved-words") - ]; + require("@babel/plugin-transform-reserved-words"), + ] // transform modules (e.g when without Rollup) if (!keepModules) { - plugins.push(...[ - require("@babel/plugin-transform-modules-commonjs"), - require("@babel/plugin-syntax-dynamic-import"), - ]); + plugins.push( + ...[require("@babel/plugin-transform-modules-commonjs"), require("@babel/plugin-syntax-dynamic-import")] + ) if (addModuleExports) { - plugins.push(...[ - [require("babel-plugin-add-module-exports"), {addDefaultProperty: addModuleExportsDefaultProperty}] // atom needs this - ]); + plugins.push( + ...[ + [require("babel-plugin-add-module-exports"), { addDefaultProperty: addModuleExportsDefaultProperty }], // atom needs this + ] + ) } - } return { presets: presets, - plugins: plugins + plugins: plugins, } } diff --git a/package.json b/package.json index dc9c409..1ce0efd 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "test.lint": "eslint .", "bump": "ncu -u" }, + "prettier": "prettier-config-atomic", "dependencies": { "@babel/plugin-proposal-class-properties": "^7.13.0", "@babel/plugin-proposal-decorators": "^7.13.5", @@ -55,6 +56,7 @@ "devDependencies": { "@babel/cli": "7.11.6", "@babel/core": "7.11.6", - "npm-check-updates": "11.3.0" + "npm-check-updates": "11.3.0", + "prettier-config-atomic": "^1.0.1" } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ec04a93..c6607db 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -29,6 +29,7 @@ devDependencies: '@babel/cli': 7.11.6_@babel+core@7.11.6 '@babel/core': 7.11.6 npm-check-updates: 11.3.0 + prettier-config-atomic: 1.0.1 lockfileVersion: 5.2 packages: /@babel/cli/7.11.6_@babel+core@7.11.6: @@ -4084,6 +4085,19 @@ packages: node: '>=4' resolution: integrity: sha1-6SQ0v6XqjBn0HN/UAddBo8gZ2Jc= + /prettier-config-atomic/1.0.1: + dependencies: + prettier: 2.2.1 + dev: true + resolution: + integrity: sha512-bNW8oMkuuVZI0OXEwwfbGGpdh1Jv4QfOzSMnmueBkSCYcCAnA9iHy+wRVsAeVRZPfB1hjqB9UtxGTnrflITtyg== + /prettier/2.2.1: + dev: true + engines: + node: '>=10.13.0' + hasBin: true + resolution: + integrity: sha512-PqyhM2yCjg/oKkFPtTGUojv7gnZAoG80ttl45O6x2Ug/rMJw4wcc9k6aaf2hibP7BGVCCM33gZoGjyvt9mm16Q== /process-nextick-args/2.0.1: dev: true resolution: @@ -5042,3 +5056,4 @@ specifiers: babel-plugin-preval: ^5.0.0 babel-plugin-transform-not-strict: ^0.3.1 npm-check-updates: 11.3.0 + prettier-config-atomic: ^1.0.1