Skip to content

Commit

Permalink
feat: enable loose option (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
Paulo Marcos authored and satazor committed May 28, 2019
1 parent f9b8a69 commit 9b4afe1
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ Below, you may find a list containing all options you may tweak:
| lodash | Transform to cherry-pick Lodash modules | boolean/[Object](https://github.com/lodash/babel-plugin-lodash#usage) | true |||
| modules | Transform ES6 module syntax to another module type | [string/boolean](https://www.npmjs.com/package/babel-preset-env#modules) | Based on `process.env.BABEL_ENV`, `commonjs` if unspecified |||
| dynamicImport | Adds support for `import()` statements | boolean | true |||
| loose | Enable "loose" transformations for any plugins that allow them | boolean | true |||
| targets | The output targets, see bellow for a more detailed explanation | Array/[Object](https://babeljs.io/docs/en/next/babel-preset-env.html#targets) | ['browsers', 'node'] |||
| env | The environment (`development`, `production` or `test`) | string | Based on `process.env.NODE_ENV` |||
| namedDefaultExport | Use [add-module-exports](https://github.com/59naga/babel-plugin-add-module-exports) plugin to get around [babel/babel#2212](https://github.com/babel/babel/issues/2212) | boolean | true if modules is `commonjs` |||
Expand Down
5 changes: 3 additions & 2 deletions end-project.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const addLodashSupport = require('./lib/lodash');

module.exports = (context, options) => {
options = Object.assign({
loose: true,
react: false,
lodash: true,
dynamicImport: true,
Expand All @@ -30,7 +31,7 @@ module.exports = (context, options) => {
// actually required based on the targets
useBuiltIns: 'entry',
// Produce less and more readable code (although not as faithful to the semantics)
loose: true,
loose: options.loose,
// Set modules options
modules: options.modules,
// Set the browser support to be the same used by Google (https://www.npmjs.com/package/browserslist-config-google)
Expand All @@ -46,7 +47,7 @@ module.exports = (context, options) => {
// The plugins bellow activate stage 3 features that babel hasn't added to the stage 3 preset yet
config.plugins.push(
// Allows class { handleClick = () => { } static propTypes = { foo: PropTypes.string } }
[require.resolve('@babel/plugin-proposal-class-properties'), { loose: true }]
[require.resolve('@babel/plugin-proposal-class-properties'), { loose: options.loose }]
);

config.plugins.push(['@babel/plugin-transform-runtime', {
Expand Down
54 changes: 54 additions & 0 deletions test/__snapshots__/end-project.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,60 @@ Object {
}
`;

exports[`loose should disable loose if options.loose is disabled 1`] = `
Object {
"plugins": Array [
Array [
"<PROJECT_ROOT>/node_modules/@babel/plugin-proposal-class-properties/lib/index.js",
Object {
"loose": true,
},
],
Array [
"@babel/plugin-transform-runtime",
Object {
"absoluteRuntime": "<PROJECT_ROOT>/node_modules/@babel/runtime",
"corejs": false,
"helpers": false,
"regenerator": true,
"useESModules": false,
},
],
"<PROJECT_ROOT>/node_modules/babel-plugin-dynamic-import-node/lib/index.js",
Array [
"<PROJECT_ROOT>/node_modules/babel-plugin-lodash/lib/index.js",
Object {
"id": Array [
"lodash",
"lodash-es",
"lodash-compat",
"lodash/fp",
],
},
],
],
"presets": Array [
Array [
"<PROJECT_ROOT>/node_modules/@babel/preset-env/lib/index.js",
Object {
"corejs": 3,
"loose": true,
"modules": "commonjs",
"shippedProposals": true,
"targets": Object {
"browsers": Array [
"extends browserslist-config-google",
],
"node": "8.9",
},
"useBuiltIns": "entry",
},
],
],
"sourceType": "unambiguous",
}
`;

exports[`modules should set modules to false by default if BABEL_ENV is es 1`] = `
Object {
"plugins": Array [
Expand Down
6 changes: 6 additions & 0 deletions test/end-project.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ describe('modules', () => {
});
});

describe('loose', () => {
it('should disable loose if options.loose is disabled', () => {
expect(preset(null, { loose: true })).toMatchSnapshot();
});
});

describe('react', () => {
it('should enable react plugins if options.react is enabled and enable development goodies', () => {
expect(preset(null, { react: true })).toMatchSnapshot();
Expand Down

0 comments on commit 9b4afe1

Please sign in to comment.