Skip to content

Commit 3220b94

Browse files
authored
Create babel-preset-backpack. Closes #17 (#40)
* Create babel-preset-backpack. Closes #17 * Fix test-only plugins
1 parent abbd47a commit 3220b94

File tree

5 files changed

+114
-55
lines changed

5 files changed

+114
-55
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# babel-preset-backpack
2+
3+
This package includes the [Babel](https://babeljs.io) preset used by [Backpack](https://github/com/palmerhq/backpack)
4+
5+
## Usage in Backpack Projects
6+
7+
The easiest way to use this configuration is with Backpack, which includes it by default. **You don’t need to install it separately in Backpack projects.**
8+
9+
## Usage Outside of Backpack
10+
11+
If you want to use this Babel preset in a project not built with Backpack, you can install it with following steps.
12+
13+
First, [install Babel](https://babeljs.io/docs/setup/).
14+
15+
Then create a file named `.babelrc` with following contents in the root folder of your project:
16+
17+
```js
18+
{
19+
"presets": ["backpack"]
20+
}
21+
```
22+
23+
This preset uses the `useBuiltIns` option with [transform-object-rest-spread](http://babeljs.io/docs/plugins/transform-object-rest-spread/), which assumes that `Object.assign` is available or polyfilled.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
const path = require('path')
2+
3+
const preset = {
4+
presets: [
5+
[require('babel-preset-env').default, {
6+
target: {
7+
node: 'current'
8+
},
9+
// Webpack takes care of modules, so we don't have to.
10+
modules: false
11+
}]
12+
],
13+
plugins: [
14+
// class { handleThing = () => { } }
15+
require.resolve('babel-plugin-transform-class-properties'),
16+
17+
// The following two plugins use Object.assign directly, instead of Babel's
18+
// extends helper. Note that this assumes `Object.assign` is available.
19+
// { ...todo, completed: true }
20+
[require.resolve('babel-plugin-transform-object-rest-spread'), {
21+
useBuiltIns: true
22+
}],
23+
24+
[require.resolve('babel-plugin-transform-regenerator'), {
25+
// Async functions are converted to generators by babel-preset-env (which
26+
// is based on babel-preset-latest)
27+
async: false
28+
}],
29+
30+
31+
// This is so we don't need to add `babel-polyfill` to our webpack `entry`.
32+
// Unlike `babel-polyfill`, `babel-runtime` + the transform do not pollute
33+
// the global namespace. Yay.
34+
// @see https://medium.com/@jcse/clearing-up-the-babel-6-ecosystem-c7678a314bf3#.7j10g8yn0
35+
[require.resolve('babel-plugin-transform-runtime'), {
36+
helpers: false,
37+
polyfill: false,
38+
regenerator: true,
39+
// Resolve the Babel runtime relative to the config.
40+
moduleName: path.dirname(require.resolve('babel-runtime/package'))
41+
}]
42+
]
43+
}
44+
45+
if (process.env.NODE_ENV === 'test' || process.env.BABEL_ENV === 'test') {
46+
preset.plugins.push.apply(preset.plugins, [
47+
// We always include this plugin regardless of environment
48+
// because of a Babel bug that breaks object rest/spread without it:
49+
// https://github.com/babel/babel/issues/4851
50+
require.resolve('babel-plugin-transform-es2015-parameters'),
51+
52+
// Jest needs this to work properly with import/export syntax
53+
[require.resolve('babel-plugin-transform-es2015-modules-commonjs'), {loose: true}]
54+
])
55+
}
56+
57+
module.exports = preset
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "babel-preset-backpack",
3+
"version": "0.0.8",
4+
"description": "Babel preset for Backpack projects",
5+
"repository": "palmerhq/backpack",
6+
"author": "jaredpalmer",
7+
"license": "MIT",
8+
"homepage": "https://github.com/palmerhq/backpack#readme",
9+
"bugs": {
10+
"url": "https://github.com/palmerhq/backpack/issues"
11+
},
12+
"files": [
13+
"index.js"
14+
],
15+
"dependencies": {
16+
"babel-plugin-transform-class-properties": "^6.19.0",
17+
"babel-plugin-transform-es2015-modules-commonjs": "^6.18.0",
18+
"babel-plugin-transform-es2015-parameters": "^6.21.0",
19+
"babel-plugin-transform-object-rest-spread": "^6.20.2",
20+
"babel-plugin-transform-regenerator": "^6.22.0",
21+
"babel-plugin-transform-runtime": "^6.15.0",
22+
"babel-preset-env": "^1.1.4",
23+
"babel-runtime": "^6.22.0"
24+
},
25+
"keywords": [
26+
"babel",
27+
"es6",
28+
"node",
29+
"backpack"
30+
]
31+
}

packages/backpack-core/babel.js

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1 @@
1-
const path = require('path')
2-
3-
const preset = {
4-
presets: [
5-
[require('babel-preset-env').default, {
6-
'target': {
7-
'node': 'current'
8-
},
9-
"modules": false
10-
}]
11-
],
12-
plugins: [
13-
// class { handleThing = () => { } }
14-
require.resolve('babel-plugin-transform-class-properties'),
15-
16-
// async => (..)
17-
require.resolve('babel-plugin-transform-async-to-generator'),
18-
19-
// The following two plugins use Object.assign directly, instead of Babel's
20-
// extends helper. Note that this assumes `Object.assign` is available.
21-
// { ...todo, completed: true }
22-
[require.resolve('babel-plugin-transform-object-rest-spread'), {
23-
useBuiltIns: true
24-
}],
25-
26-
[require.resolve('babel-plugin-transform-runtime'), {
27-
helpers: false,
28-
polyfill: false,
29-
regenerator: true,
30-
// Resolve the Babel runtime relative to the config.
31-
moduleName: path.dirname(require.resolve('babel-runtime/package'))
32-
}]
33-
]
34-
}
35-
36-
if (process.env.NODE_ENV || process.env.BABEL_ENV === 'test') {
37-
preset.plugins.push.apply(preset.plugins, [
38-
// We always include this plugin regardless of environment
39-
// because of a Babel bug that breaks object rest/spread without it:
40-
// https://github.com/babel/babel/issues/4851
41-
require.resolve('babel-plugin-transform-es2015-parameters'),
42-
// Jest needs this to work properly with import/export syntax
43-
require.resolve('babel-plugin-transform-es2015-modules-commonjs')
44-
])
45-
}
46-
47-
module.exports = preset
1+
module.exports = require('babel-preset-backpack')

packages/backpack-core/package.json

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,9 @@
1212
"backpack": "./bin/backpack"
1313
},
1414
"dependencies": {
15-
"babel-core": "^6.21.0",
15+
"babel-core": "^6.22.0",
1616
"babel-loader": "^6.2.10",
17-
"babel-plugin-transform-async-to-generator": "^6.16.0",
18-
"babel-plugin-transform-class-properties": "^6.19.0",
19-
"babel-plugin-transform-es2015-modules-commonjs": "^6.18.0",
20-
"babel-plugin-transform-es2015-parameters": "^6.21.0",
21-
"babel-plugin-transform-object-rest-spread": "^6.20.2",
22-
"babel-plugin-transform-runtime": "^6.15.0",
23-
"babel-preset-env": "^1.1.4",
17+
"babel-preset-backpack": "0.0.8",
2418
"chokidar": "^1.6.1",
2519
"cross-spawn": "^5.0.1",
2620
"friendly-errors-webpack-plugin": "^1.1.2",

0 commit comments

Comments
 (0)