Skip to content

Commit d0ea551

Browse files
author
Jared Palmer
committed
Add custom babel example, update readme, bump ver
1 parent b05b2ec commit d0ea551

File tree

10 files changed

+135
-16
lines changed

10 files changed

+135
-16
lines changed

README.md

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ Backpack is minimalistic build system for Node.js. Inspired by Facebook's [creat
1212
- Live reload (on saves, add/delete file, etc.)
1313
- Zero-config, one dependency.
1414

15-
HOWEVER, you can configure Backpack to your project's needs. You can [modify the underlying Webpack 2 configuration](#custom-configuration).
16-
15+
HOWEVER, you can configure Backpack to your project's needs by extending [the underlying Webpack 2 configuration](#custom-configuration).
1716

1817
## How to use
1918

@@ -50,18 +49,20 @@ Successful builds will show a console like this. *Note: screenshot taken from ru
5049

5150
### Custom configuration
5251

53-
For custom advanced behavior, you can create a `backpack.config.js` in the root of your project's directory (next to `package.json`). Note: `backpack.config.js` is a regular Node.js module, not a JSON file. It gets used by the Backpack build phase, but does not itself go through babel transformation. So only use JS that's supported by your current Node.js version.
52+
For custom advanced behavior, you can create a `backpack.config.js` in the root of your project's directory (next to `package.json`).
5453

5554
```js
5655
// backpack.config.js
56+
// IMPORTANT: This file is not going through babel transformation.
57+
// You can however use the ES2015 features supported by your Node.js version.
5758
module.exports = {
5859
/* config options here */
5960
}
6061
```
6162

62-
### Customizing Webpack
63+
### Customizing webpack config
6364

64-
[Example](https://github.com/palmerhq/backpack/tree/master/examples/with-custom-webpack-config)
65+
[Example](https://github.com/palmerhq/backpack/tree/master/examples/with-custom-webpack-config)
6566

6667
To extend webpack, you can define a function that extends its config via `backpack.config.js`.
6768

@@ -76,8 +77,31 @@ module.exports = {
7677
}
7778
```
7879

80+
### Customizing babel config
81+
82+
[Example](https://github.com/palmerhq/backpack/tree/master/examples/with-custom-babel-config)
83+
84+
To extend our usage of `babel`, you can define a `.babelrc` file at the root of your app. This file is optional.
85+
86+
If found, Backpack will consider it to be the *source of truth*. Thus it must define what Backpack needs as well, which is the `backpack-core/babel` preset.
87+
88+
This is designed so that you are not surprised by modifications we could make to the default `babel` configurations.
89+
90+
Here's an example `.babelrc` file:
91+
92+
```js
93+
{
94+
"presets": [
95+
"backpack-core",
96+
"stage-0"
97+
],
98+
}
99+
```
100+
101+
*Note: This works [exactly like Next.js does](https://github.com/zeit/next.js#customizing-babel-config).*
79102

80103
### Building for Production
104+
81105
Add a npm script for the build step:
82106

83107
```json

examples/basic/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "backpack-examples-basic",
3-
"version": "0.0.5",
3+
"version": "0.0.6",
44
"scripts": {
55
"start": "node ./build/main.js",
66
"dev": "backpack dev",
@@ -12,6 +12,6 @@
1212
"express": "^4.14.0"
1313
},
1414
"devDependencies": {
15-
"backpack-core": "0.0.5"
15+
"backpack-core": "0.0.6"
1616
}
1717
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"presets": [
3+
"backpack-core/babel",
4+
"stage-0"
5+
]
6+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
build
3+
npm-debug.log
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Custom Babel Configuration Example
2+
3+
## How to use
4+
Download the example (or clone the whole project)[https://github.com/palmerhq/backpack.git]:
5+
```bash
6+
curl https://codeload.github.com/palmerhq/backpack/tar.gz/master | tar -xz --strip=2 backpack-master/examples/with-custom-babel-config
7+
cd with-custom-babel-config
8+
```
9+
Install it and run:
10+
```bash
11+
npm install
12+
npm run dev
13+
```
14+
15+
## Idea behind the example
16+
This demonstrates how extend the default Backpack babel configuration with a custom `.babelrc` file.
17+
More specifically, this example shows how to add all `stage-0` transformations through `babel-preset-stage-0`.
18+
19+
Be sure to include `backpack-core/babel` in your `.babelrc` as the first item in the presets array:
20+
21+
```json
22+
{
23+
presets: [
24+
"backpack-core/babel",
25+
"..."
26+
]
27+
}
28+
```
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "backpack-examples-with-custom-babel-config",
3+
"version": "0.0.6",
4+
"scripts": {
5+
"start": "node ./build/main.js",
6+
"dev": "backpack dev",
7+
"build": "backpack build",
8+
"postinstall": "backpack build"
9+
},
10+
"license": "MIT",
11+
"dependencies": {
12+
"express": "^4.14.0"
13+
},
14+
"devDependencies": {
15+
"babel-preset-stage-0": "^6.16.0",
16+
"backpack-core": "0.0.6"
17+
}
18+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const randomNo = Math.ceil(Math.random() * 100)
2+
3+
// `do` expressions are a new syntax proposal.
4+
// @see: https://babeljs.io/docs/plugins/transform-do-expressions/
5+
const message = do {
6+
if (randomNo < 30) {
7+
'Do not give up. Try again.'
8+
} else if (randomNo < 60) {
9+
'You are a lucky guy'
10+
} else {
11+
'You are soooo lucky!'
12+
}
13+
}
14+
15+
console.log(`number: ${randomNo}`)
16+
console.log(`message: ${message}`)

examples/with-custom-webpack-config/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "backpack-examples-with-custom-webpack-config",
3-
"version": "0.0.5",
3+
"version": "0.0.6",
44
"scripts": {
55
"start": "node ./build/main.js",
66
"dev": "backpack dev",
@@ -12,6 +12,6 @@
1212
"express": "^4.14.0"
1313
},
1414
"devDependencies": {
15-
"backpack-core": "0.0.5"
15+
"backpack-core": "0.0.6"
1616
}
1717
}

examples/with-jest/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "backpack-examples-with-jest",
3-
"version": "0.0.5",
3+
"version": "0.0.6",
44
"scripts": {
55
"start": "node ./build/main.js",
66
"test": "NODE_ENV=test jest",
@@ -14,7 +14,7 @@
1414
},
1515
"devDependencies": {
1616
"babel-jest": "^18.0.0",
17-
"backpack-core": "0.0.5",
17+
"backpack-core": "0.0.6",
1818
"jest-cli": "^18.1.0",
1919
"supertest": "^2.0.1"
2020
},

packages/backpack-core/README.md

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ Backpack is minimalistic build system for Node.js. Inspired by Facebook's [creat
1212
- Live reload (on saves, add/delete file, etc.)
1313
- Zero-config, one dependency.
1414

15-
HOWEVER, you can configure Backpack to your project's needs. You can [modify the underlying Webpack 2 configuration](#custom-configuration).
16-
15+
HOWEVER, you can configure Backpack to your project's needs by extending [the underlying Webpack 2 configuration](#custom-configuration).
1716

1817
## How to use
1918

@@ -50,18 +49,20 @@ Successful builds will show a console like this. *Note: screenshot taken from ru
5049

5150
### Custom configuration
5251

53-
For custom advanced behavior, you can create a `backpack.config.js` in the root of your project's directory (next to `package.json`). Note: `backpack.config.js` is a regular Node.js module, not a JSON file. It gets used by the Backpack build phase, but does not itself go through babel transformation. So only use JS that's supported by your current Node.js version.
52+
For custom advanced behavior, you can create a `backpack.config.js` in the root of your project's directory (next to `package.json`).
5453

5554
```js
5655
// backpack.config.js
56+
// IMPORTANT: This file is not going through babel transformation.
57+
// You can however use the ES2015 features supported by your Node.js version.
5758
module.exports = {
5859
/* config options here */
5960
}
6061
```
6162

62-
### Customizing Webpack
63+
### Customizing webpack config
6364

64-
[Example](https://github.com/palmerhq/backpack/tree/master/examples/with-custom-webpack-config)
65+
[Example](https://github.com/palmerhq/backpack/tree/master/examples/with-custom-webpack-config)
6566

6667
To extend webpack, you can define a function that extends its config via `backpack.config.js`.
6768

@@ -76,8 +77,31 @@ module.exports = {
7677
}
7778
```
7879

80+
### Customizing babel config
81+
82+
[Example](https://github.com/palmerhq/backpack/tree/master/examples/with-custom-babel-config)
83+
84+
To extend our usage of `babel`, you can define a `.babelrc` file at the root of your app. This file is optional.
85+
86+
If found, Backpack will consider it to be the *source of truth*. Thus it must define what Backpack needs as well, which is the `backpack-core/babel` preset.
87+
88+
This is designed so that you are not surprised by modifications we could make to the default `babel` configurations.
89+
90+
Here's an example `.babelrc` file:
91+
92+
```js
93+
{
94+
"presets": [
95+
"backpack-core",
96+
"stage-0"
97+
],
98+
}
99+
```
100+
101+
*Note: This works [exactly like Next.js does](https://github.com/zeit/next.js#customizing-babel-config).*
79102

80103
### Building for Production
104+
81105
Add a npm script for the build step:
82106

83107
```json

0 commit comments

Comments
 (0)