Skip to content
This repository has been archived by the owner on Jan 5, 2020. It is now read-only.

Add partial support for “modules” preset option #8

Closed
wants to merge 1 commit into from

Conversation

dinoboff
Copy link

Only support transforming ES module to “commonjs” or to disable the module transformation. "babel-preset-env" supports other legacy module types which are not relevant for AVA.

@dinoboff dinoboff force-pushed the feat/modules branch 2 times, most recently from ca12ed0 to 48530d2 Compare September 13, 2017 14:24
Copy link
Member

@novemberborn novemberborn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, this is great!

Could you document the option in the README?

https://github.com/avajs/babel-preset-stage-4/blob/699ae236caea1b529e8ec8f527bb385325d84d17/package-hash.js#L4:L5 should be updated to always include the babel-plugin-transform-es2015-modules-commonjs plugin.

index.js Outdated
@@ -1,7 +1,10 @@
'use strict';
/* eslint-disable import/no-dynamic-require */
module.exports = () => {
const plugins = require(`./plugins/best-match`)
const modulePlugins = require('./plugins/module');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps rename the file to modules, and import it into a determineModulesPlugin() variable?

@@ -0,0 +1,11 @@
'use strict';

module.exports = function (opts) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's stay consistent and name this options.

'use strict';

module.exports = function (opts) {
const modules = opts && opts.modules !== null ? opts.modules : true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we treat this the same as https://github.com/babel/babel-preset-env#modules? E.g. the only allowed values are 'commonjs' or false.

throw new Error('@ava/stage-4 only support commonjs modules');
}

return modules ? require('./commonjs.json') : [];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to return ['babel-plugin-transform-es2015-modules-commonjs'] directly. The JSON files are there so the plugin lists are easier to manage per Node.js version, but that doesn't apply here.

@dinoboff dinoboff force-pushed the feat/modules branch 3 times, most recently from ead7129 to 0ab8850 Compare September 17, 2017 23:19
Only support transforming ES module to “commonjs” or to disable the module transformation. "babel-preset-env" supports other legacy module types which are not relevant for AVA.
@dinoboff
Copy link
Author

@novemberborn Can you review the changes?

Copy link
Member

@novemberborn novemberborn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @dinoboff, apologies for taking so long in getting back to you.

Could you merge in master? I've made some updates in #9. I think there's at least one test that will fail when using the latest AVA.

Codecov wasn't working, but it is now. Could you try and get back to a 100% test coverage?


const plugins = require('./plugins/best-match')
const plugins = Object.keys(determineModulesPlugin.supported)
.map(k => determineModulesPlugin.supported[k])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just starts with [babel-plugin-transform-es2015-modules-commonjs] and run .concat(require('./plugins/best-match') on that.

throw new Error('@ava/stage-4 only supports commonjs module transformation.');
}

return [plugin];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say this is clearer:

if (!options || options.modules === false) {
    return [];
}

if (options.modules !== 'commonjs') {
     throw new Error('@ava/stage-4 only supports commonjs module transformation.');
}

return ['babel-plugin-transform-es2015-modules-commonjs'];

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does babel always provides options, even when the preset is set without any?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah no, my bad. We should default to commonjs if there are no options.


`"commonjs" | false`, defaults to `"commonjs"`.

Enable transformation of ES6 module syntax to another module type.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer ES2015 over ES6. That also corresponds with the plugin package name.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, it was taken from env preset.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants