Skip to content

Commit df0e92b

Browse files
denysdovhanpaulmillr
authored andcommitted
Refactoring, env, closing a bunch of issues (#53)
* Remove presets and plugin mapping. Babel does this just fine. * Less brackets * Have no idea what is this for * Add explanation comment * Require instead of plain reading and parsing * Add ESLint and eslint-config-brunch * Fix linting errors in index.js * Lint all files * Fix linting errors in test.js * Fix for old NPMs * Add config lookup function * Fix README.md * Use OptionManager * Add babelConfig checking * indexOf -> includes * Adjust tests * Node.js <6 doesn't support Array#includes * babel-preset-latest -> babel-preset-env * assert -> chai.should, because of @shvaikalesh * should.be.a() -> respondTo * Remove extras from tests * Detect UglifyJS and set corresponding target * Don't return Assertion to Mocha * Better: useUglify -> targetUglify * Update and clean-up README.md * Remove useless checking * Explicitly return false if it's not production, but UglifyJS present * Update Travis configuration
1 parent 2cff6ce commit df0e92b

File tree

6 files changed

+197
-240
lines changed

6 files changed

+197
-240
lines changed

.eslintrc.json

-84
This file was deleted.

.travis.yml

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
sudo: false
21
language: node_js
32
node_js:
4-
- '6'
5-
- '5'
6-
- '4'
3+
- node
4+
- 6
5+
- 4

README.md

+38-19
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,42 @@
11
## babel-brunch
22

3-
Brunch plugin using [babel](https://github.com/babel/babel) to turn latest
3+
Brunch plugin using [Babel](https://github.com/babel/babel) to turn latest
44
ECMAScript standard code into vanilla ES5 with no runtime required.
55

66
All the `.js` files in your project will be run through the babel compiler,
77
except those it is configured to ignore, unless you use the `pattern` option.
88

9-
Additionally, starting Brunch 2.7, babel-brunch will also compile NPM dependencies.
9+
Additionally, starting Brunch 2.7, `babel-brunch` will also compile NPM dependencies.
1010

1111
## Installation
1212

13-
`npm install --save-dev babel-brunch`
13+
```
14+
npm install --save-dev babel-brunch
15+
```
1416

1517
## Configuration
1618

17-
[babel-preset-latest](https://babeljs.io/docs/plugins/preset-latest/) (es2015, es2016, es2017) **is used by default**, you don't need to adjust config to have it.
19+
[babel-preset-env](https://github.com/babel/babel-preset-env) (a Babel preset that can automatically determine the plugins and polyfills you need based on your supported environments) **is used by default**.
20+
21+
The default behavior without options runs all transforms (behaves the same as [babel-preset-latest](https://babeljs.io/docs/plugins/preset-latest/)).
22+
23+
Optionally, you can configure the preset for your needs:
24+
25+
```js
26+
module.exports.plugins = {
27+
babel: {
28+
presets: [['env', {
29+
targets: {
30+
browsers: ['last 2 versions', 'safari >= 7']
31+
}
32+
}]]
33+
}
34+
}
35+
```
36+
37+
Read more about [`env`'s options](https://github.com/babel/babel-preset-env#options).
1838

19-
### Using React; or any other plugin
39+
### Using React or any other plugin
2040

2141
Install a plugin:
2242

@@ -26,23 +46,20 @@ npm install --save-dev babel-preset-react
2646

2747
Then, make sure Brunch sees it:
2848

29-
```javascript
30-
exports.plugins = {
49+
```js
50+
module.exports.plugins = {
51+
// ...
3152
babel: {
32-
presets: ['latest', 'react']
53+
presets: ['env', 'react']
3354
}
3455
}
3556
```
3657

37-
Optionally, you can configure the preset:
38-
39-
`presets: [ 'latest', ['transform-es2015-template-literals', {spec: true}] ]`
40-
41-
4258
### Ignoring node modules
4359

44-
```
45-
exports.plugins = {
60+
```js
61+
module.exports.plugins = {
62+
// ...
4663
babel: {
4764
ignore: [
4865
/^node_modules/,
@@ -52,21 +69,23 @@ exports.plugins = {
5269
}
5370
```
5471

55-
### Changing which files would be compiled by babel
72+
### Changing which files would be compiled by Babel
5673

57-
```
58-
exports.plugins = {
74+
```js
75+
module.exports.plugins = {
76+
// ...
5977
babel: {
6078
pattern: /\.es7$/ // By default, JS|JSX|ES6 are used.
6179
}
6280
}
6381
```
6482

65-
Set [babel options](https://babeljs.io/docs/usage/options) in your brunch
83+
Set [Babel options](https://babeljs.io/docs/usage/options) in your Brunch
6684
config (such as `brunch-config.js`) except for `filename` and `sourceMap`
6785
which are handled internally.
6886

6987
## Change Log
88+
7089
[See release notes page on GitHub](https://github.com/babel/babel-brunch/releases)
7190

7291
## License

index.js

+31-27
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,36 @@
22

33
const babel = require('babel-core');
44
const anymatch = require('anymatch');
5-
const resolve = require('path').resolve;
65
const logger = require('loggy');
76

87
const reIg = /^(bower_components|vendor)/;
98

109
const warns = {
11-
ES6to5: 'Please use `babel` instead of `ES6to5` option in your config file.'
10+
ES6to5: 'Please use `babel` instead of `ES6to5` option in your config file.',
1211
};
1312

14-
const prettySyntaxError = (err) => {
13+
const prettySyntaxError = err => {
1514
if (!(err._babel && err instanceof SyntaxError)) return err;
16-
const msg = err.message.replace(/\(\d+:\d+\)$/, '').replace(/^([\./\w]+\:\s)/, '');
15+
const msg = err.message
16+
.replace(/\(\d+:\d+\)$/, '')
17+
.replace(/^([./\w]+:\s)/, '');
1718
const error = new Error(`L${err.loc.line}:${err.loc.column} ${msg}\n${err.codeFrame}`);
1819
error.name = '';
1920
error.stack = err.stack;
2021
return error;
2122
};
2223

24+
const targetUglify = () => {
25+
try {
26+
const isProduction = process.env.NODE_ENV === 'production';
27+
const isUglify = require('uglify-js-brunch');
28+
if (isProduction && isUglify) return true;
29+
return false;
30+
} catch (e) {
31+
return false;
32+
}
33+
};
34+
2335
class BabelCompiler {
2436
constructor(config) {
2537
if (!config) config = {};
@@ -38,33 +50,12 @@ class BabelCompiler {
3850
}, {});
3951

4052
opts.sourceMap = !!config.sourceMaps;
41-
if (!opts.presets) {
42-
opts.presets = ['latest'];
43-
// ['env', opts.env]
44-
}
45-
if (!opts.plugins) opts.plugins = [];
46-
47-
// this is needed so that babel can locate presets when compiling node_modules
48-
const mapOption = type => data => {
49-
const resolvePath = name => {
50-
// for cases when plugins name do not match common convention
51-
// for example: `babel-root-import`
52-
const plugin = name.startsWith('babel-') ? name : `babel-${type}-${name}`;
53-
return resolve(config.paths.root, 'node_modules', plugin);
54-
};
55-
if (typeof data === 'string') return resolvePath(data);
56-
return [resolvePath(data[0]), data[1]];
57-
};
58-
const mappedPresets = opts.presets.map(mapOption('preset'));
59-
const mappedPlugins = opts.plugins.map(mapOption('plugin'));
60-
opts.presets = mappedPresets;
61-
opts.plugins = mappedPlugins;
62-
if (opts.presets.length === 0) delete opts.presets;
63-
if (opts.plugins.length === 0) delete opts.plugins;
53+
6454
if (opts.pattern) {
6555
this.pattern = opts.pattern;
6656
delete opts.pattern;
6757
}
58+
6859
this.isIgnored = anymatch(options.ignore || reIg);
6960
this.options = opts;
7061
}
@@ -74,6 +65,19 @@ class BabelCompiler {
7465
this.options.filename = file.path;
7566
this.options.sourceFileName = file.path;
7667

68+
if (!this.options.presets) {
69+
const babelConfig = new babel.OptionManager().init(this.options);
70+
const hasConfig =
71+
babelConfig.presets && babelConfig.presets.length ||
72+
babelConfig.plugins && babelConfig.plugins.length;
73+
74+
if (!hasConfig) {
75+
this.options.presets = [['env', {
76+
targets: targetUglify() ? {uglify: true} : {},
77+
}]];
78+
}
79+
}
80+
7781
return new Promise((resolve, reject) => {
7882
let compiled;
7983

package.json

+10-5
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,26 @@
2222
"6to5"
2323
],
2424
"scripts": {
25-
"test": "eslint index.js && mocha"
25+
"test": "eslint *.js && mocha"
2626
},
2727
"files": [
2828
"index.js"
2929
],
3030
"dependencies": {
3131
"anymatch": "^1.0.0",
32-
"babel-core": "^6.0.0",
33-
"babel-preset-latest": "^6.22.0",
32+
"babel-core": "^6.24.0",
33+
"babel-preset-env": "^1.2.2",
3434
"loggy": "~1.0.1"
3535
},
3636
"devDependencies": {
3737
"babel-plugin-transform-es2015-template-literals": "^6.8.0",
3838
"babel-plugin-transform-node-env-inline": "^6.1.18",
39-
"mocha": "^2.0.0",
40-
"eslint": "^2.0.0"
39+
"chai": "^3.5.0",
40+
"eslint": "^3.17.1",
41+
"eslint-config-brunch": "^1.0.0",
42+
"mocha": "^2.0.0"
43+
},
44+
"eslintConfig": {
45+
"extends": "brunch"
4146
}
4247
}

0 commit comments

Comments
 (0)