Skip to content

Commit

Permalink
chore: add prettier + format
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya committed Mar 31, 2021
1 parent d27d8db commit 1a7cb1a
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 51 deletions.
5 changes: 5 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
pnpm-lock.yaml
package-lock.json
CHANGELOG.md
dist
52 changes: 31 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
[
Expand All @@ -48,26 +52,27 @@ 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 = [
[
"babel-preset-atomic",
{
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.

Expand All @@ -76,69 +81,74 @@ let presets = [
[
"babel-preset-atomic",
{
addModuleExports: false
addModuleExports: false,
},
],
];
]
```

4) `addModuleExportsDefaultProperty`:
4. `addModuleExportsDefaultProperty`:

```js
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"`
- `"@babel/plugin-proposal-export-namespace-from"`
- ...

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"`
91 changes: 62 additions & 29 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 = [
Expand Down Expand Up @@ -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,
}
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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"
}
}
15 changes: 15 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1a7cb1a

Please sign in to comment.