Skip to content

Commit

Permalink
feat: options can now be passed into the node version of the preset.
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven Truesdell committed Jun 3, 2017
0 parents commit 65f627c
Show file tree
Hide file tree
Showing 10 changed files with 1,085 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2016-2017 Steven Truesdell

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
63 changes: 63 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# babel-preset-boldr

Babel preset for [Boldr](https://github.com/strues/boldr)

Env default settings:
```
{
modules: false,
debug: false,
useBuiltIns: true,
targets: {
browsers: ['>1%', 'last 2 versions', 'safari >= 9'],
},
// if node
targets: {
node: 7.7
},
},
```


**Pass Options**
```json
{
"presets": [["boldr/node", {
"preset-env": {
"modules": "commonjs"
},
"loose": true
}]]
}
```

**Included Presets:**

- babel-preset-env
- babel-preset-react

**Included Plugins:**

- babel-plugin-syntax-dynamic-import
- babel-plugin-syntax-flow
- babel-plugin-transform-class-properties
- babel-plugin-transform-decorators-legacy
- babel-plugin-transform-runtime
- babel-plugin-transform-regenerator
- babel-plugin-transform-export-extensions
- babel-plugin-transform-object-rest-spread
- babel-plugin-transform-react-jsx
- babel-plugin-transform-flow-strip-types
- babel-plugin-dynamic-import-node
- babel-plugin-dynamic-import-webpack

**Production - React:**

- babel-plugin-transform-react-constant-elements
- babel-plugin-transform-react-inline-elements
- babel-plugin-transform-react-strip-prop-types

**Development - React:**

- babel-plugin-transform-react-jsx-self
- babel-plugin-transform-react-jsx-source
78 changes: 78 additions & 0 deletions browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
const path = require('path');

const env = process.env.BABEL_ENV || process.env.NODE_ENV;

module.exports = {
presets: [
[
require.resolve('babel-preset-env'),
{
modules: false,
debug: false,
useBuiltIns: true,
targets: {
browsers: ['> 2% in US', 'last 2 versions'],
},
exclude: ['transform-regenerator', 'transform-async-to-generator'],
},
],
require.resolve('babel-preset-react'),
],
plugins: [
require.resolve('babel-plugin-syntax-flow'),
require.resolve('babel-plugin-syntax-dynamic-import'),
[
require.resolve('fast-async'),
{
spec: true,
},
],
// class { handleClick = () => { } }
[
require.resolve('babel-plugin-transform-class-properties'),
{
spec: true,
},
],
require.resolve('babel-plugin-transform-decorators-legacy'),
// { ...param, completed: true }
[
require.resolve('babel-plugin-transform-object-rest-spread'),
{
useBuiltIns: true,
},
],
// Transforms JSX - Added so object-rest-spread in JSX uses builtIn
[
require.resolve('babel-plugin-transform-react-jsx'),
{
useBuiltIns: true,
},
],
],
};

if (env === 'production') {
const prodPlugins = [
[
require.resolve('babel-plugin-transform-react-remove-prop-types'),
{ removeImport: true },
],
require.resolve('babel-plugin-transform-react-constant-elements'),
require.resolve('babel-plugin-transform-react-inline-elements'),
require.resolve('babel-plugin-transform-flow-strip-types'),
];

module.exports.plugins.push(...prodPlugins);
}

if (env === 'development' || env === 'test') {
const devPlugins = [
// Adds component stack to warning messages
require.resolve('babel-plugin-transform-react-jsx-source'),
// Adds __self attribute to JSX which React will use for some warnings
require.resolve('babel-plugin-transform-react-jsx-self'),
];

module.exports.plugins.push(...devPlugins);
}
15 changes: 15 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const browser = require('./browser');
const node = require('./node');

const env = process.env.BABEL_ENV || process.env.NODE_ENV;

if (env !== 'development' && env !== 'test' && env !== 'production') {
throw new Error(
`babel-preset-boldr invalid \`NODE_ENV\` or \`BABEL_ENV\`.
Valid are development|production|test, instead received ${JSON.stringify(env)}`
);
}

module.exports = {};
module.exports.browser = browser;
module.exports.node = node;
71 changes: 71 additions & 0 deletions node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
const path = require('path');

const env = process.env.BABEL_ENV || process.env.NODE_ENV;
const validTargets = ['7', '7.6', '8', 'current'];

module.exports = function(context, opts) {
opts = opts || { };
const targetOption = String(opts.target);
// use indexOf to support node 4
if (targetOption && validTargets.indexOf(targetOption) === -1) {
throw new Error(`Preset boldr/node 'target' option must one of ${validTargets.join(', ')}.`);
}

const target = targetOption !== 'current' ? targetOption : process.versions.node;
const loose = opts.loose !== undefined ? opts.loose : false;
const modules = opts['preset-env'].modules !== undefined ? opts['preset-env'].modules : 'commonjs';

if (modules !== false && modules !== 'commonjs') {
throw new Error("Preset boldr/node 'modules' option must be 'false' to indicate no modules\n" +
"or 'commonjs' (default)");
}
if (typeof loose !== 'boolean') throw new Error("Preset boldr/node 'loose' option must be a boolean.");
const optsLoose = { loose };

const envOpts = {
debug: false,
modules: modules,
targets: {
node: target
},
useBuiltIns: true,
exclude: ['transform-regenerator', 'transform-async-to-generator'],
}
presets: [
[
require.resolve('babel-preset-env'),
Object.assign({ }, envOpts, opts['preset-env'] || { })
],
require.resolve('babel-preset-react')
],
plugins: [
require.resolve('babel-plugin-syntax-flow'),
require.resolve('babel-plugin-syntax-dynamic-import'),
[
require.resolve('fast-async'),
{
spec: true,
},
],
modules === 'commonjs' && [require('babel-plugin-transform-es2015-modules-commonjs'), optsLoose],
// class { handleClick = () => { } }
require.resolve('babel-plugin-transform-class-properties'),
// { ...param, completed: true }
[
require.resolve('babel-plugin-transform-object-rest-spread'),
{
useBuiltIns: true,
},
],
require.resolve('babel-plugin-transform-decorators-legacy'),

require.resolve('babel-plugin-dynamic-import-node'),
],
};
if (env === 'production') {
const prodPlugins = [
require.resolve('babel-plugin-transform-flow-strip-types'),
];

module.exports.plugins.push(...prodPlugins);
}
55 changes: 55 additions & 0 deletions package-lock.json

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

54 changes: 54 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"name": "babel-preset-boldr",
"version": "0.9.0",
"description": "Babel preset used by Boldr",
"license": "MIT",
"author": "Steven Truesdell <[email protected]>",
"bugs": {
"url": "https://github.com/boldr/babel-preset-boldr/issues"
},
"repository": {
"type": "git",
"url": "git+https://github.com/boldr/babel-preset-boldr.git"
},
"homepage": "https://github.com/boldr/babel-preset-boldr#readme",
"engines": {
"node": ">=7.0"
},
"scripts": {
"test": "echo \"No tests\" && exit 0"
},
"engineStrict": true,
"files": [
"browser.js",
"index.js",
"node.js"
],
"dependencies": {
"babel-plugin-dynamic-import-node": "^1.0.2",
"babel-plugin-syntax-dynamic-import": "^6.18.0",
"babel-plugin-syntax-flow": "^6.18.0",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-plugin-transform-es2015-modules-commonjs": "^6.24.1",
"babel-plugin-transform-flow-strip-types": "^6.22.0",
"babel-plugin-transform-object-rest-spread": "6.23.0",
"babel-plugin-transform-react-constant-elements": "^6.23.0",
"babel-plugin-transform-react-inline-elements": "^6.22.0",
"babel-plugin-transform-react-jsx": "^6.24.1",
"babel-plugin-transform-react-jsx-self": "6.22.0",
"babel-plugin-transform-react-jsx-source": "6.22.0",
"babel-plugin-transform-react-remove-prop-types": "^0.4.5",
"babel-preset-env": "^1.5.1",
"babel-preset-react": "^6.24.1",
"fast-async": "^6.2.2"
},
"devDependencies": {
"cz-conventional-changelog-emoji": "^0.1.0"
},
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog-emoji"
}
}
}
Loading

0 comments on commit 65f627c

Please sign in to comment.