Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature - scope babel 7 support #252

Closed
wants to merge 9 commits into from

Conversation

noygal
Copy link

@noygal noygal commented Nov 14, 2017

I've expended the options to support babel option, this options apply only to the transform function as the other two uses were removed for babel 7, this is not the best approach as we have two separate babel packages running in the package - babel-core for configuration setup and the optional babel for transform, but I think that the solution would come from a proper babel 7 integration.

For the time being, it is a pretty simple solution for early babel 7 adaptors.

Added:

  • Test (basic - option passing and compile)
  • devDependancies - for testing (@babel/core, @babel/preset-env)
  • Documentation (FAQ section)

Update: I've noticed this PR - #244, and removed the babel-core dependancies.

@tex0l
Copy link

tex0l commented Nov 27, 2017

It seems I have a bug with this PR. Your delete opts.babel breaks babelify when there are multiple files to go through. The first file uses @babel/core, but the next ones use the default babel-core which breaks the whole interest of your PR ^^.

I worked around by modifying the following line:

  return function (filename) {
    var extname = path.extname(filename)
    if (extensions.indexOf(function (ext) {return ext === extname}) > -1) {
      return stream.PassThrough();
    }

    var _opts = sourceMapsAbsolute
      ? Object.assign({sourceFileName: filename}, opts)
      : Object.assign({}, opts); // <-- this was previously `opts`

    return new Babelify(filename, _opts);
  };

@arantes555
Copy link

Seeing the same problem as @tex0l . Another, cleaner option would be to modify the constructor as such :

function Babelify(filename, opts) {
  opts = Object.assign({}, opts); // <-- adding this
  if (!(this instanceof Babelify)) {
    return Babelify.configure(opts)(filename);
  }

  stream.Transform.call(this);
  this._data = "";
  this._filename = filename;
  this._babel = opts.babel || babel
  delete opts.babel
  this._opts = Object.assign({filename: filename}, opts);
}

so that the function does not have side-effects on its arguments, which is very ugly

@noygal
Copy link
Author

noygal commented Nov 27, 2017

@tex0l @arantes555 , missed that - I only work with single file.

I've added the bugfix, can you check it out?

@tex0l
Copy link

tex0l commented Nov 27, 2017

@noygal works for me :) thanks!

package.json Outdated
@@ -16,6 +16,8 @@
"babel-core": "6 || 7 || ^7.0.0-alpha || ^7.0.0-beta || ^7.0.0-rc"
Copy link
Member

Choose a reason for hiding this comment

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

It seems like if we're going to update this for 7.x, we might as well change this to @babel/core directly as a breaking change.

```js
browserify().transform("babelify", {
presets: ["@babel/preset-env"],
babel: require("@babel/core")
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this is a good idea. If we're going to support 7, we should just add it directly. The utilities that were removed can easily be implemented inside this module on their own, if we actually want to support them.

Copy link
Author

Choose a reason for hiding this comment

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

I didn't want to break the current implementation as there were breaking changes in babel 7 (everything became scope), maybe we can add it to the next major version.

index.js Outdated
@@ -23,7 +27,7 @@ Babelify.prototype._transform = function (buf, enc, callback) {

Babelify.prototype._flush = function (callback) {
try {
var result = babel.transform(this._data, this._opts);
var result = this._babel.transform(this._data, this._opts)
Copy link
Member

Choose a reason for hiding this comment

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

It'd be great to update this to use the callback version that landed in beta.32. e.g.

babel.transform(this._data, this._opts, (err, result) => {

Copy link
Author

Choose a reason for hiding this comment

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

nice, but will need to check babel version before using the new API, I'll update it and run the tests.

index.js Outdated
@@ -62,7 +68,8 @@ Babelify.configure = function (opts) {
if (opts.presets && opts.presets._) opts.presets = opts.presets._;

return function (filename) {
if (!babel.util.canCompile(filename, extensions)) {
var extname = path.extname(filename)
if (extensions.indexOf(function (ext) {return ext === extname}) > -1) {
Copy link
Member

Choose a reason for hiding this comment

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

Is this intended to be .filter?

Copy link

Choose a reason for hiding this comment

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

I believe it should be extensions.includes(path.extname(filename)), right?

Copy link
Member

Choose a reason for hiding this comment

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

Node 4 is still supported by Babel, so we'll want to stick to features that work there.

Copy link

Choose a reason for hiding this comment

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

That's what I was just checking...

Copy link
Author

Choose a reason for hiding this comment

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

that why I went with indexOf, I don't think there's any node/browser version that don't support that.

Choose a reason for hiding this comment

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

@noygal except indexOf takes a value, not a function, as argument :/

Copy link
Author

Choose a reason for hiding this comment

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

😳 ...
didn't use for a while now

@niftylettuce
Copy link

Can we have this merged yet please? Thank you

@niftylettuce
Copy link

niftylettuce commented Dec 26, 2017

@noygal you need to update package.json - and remove babel-core and fix the peerDependencies

e.g. ladjs@a76a30c

@niftylettuce
Copy link

Also need to update the top of the file to use @babel/core instead of babel-core. The tests as well would need updated, e.g. ladjs@165d668#diff-168726dbe96b3ce427e7fedce31bb0bcR4

@hzoo
Copy link
Member

hzoo commented Apr 6, 2018

Sorry for the super long delay 😅, going to go with #255 instead! Thanks again.

@hzoo hzoo closed this Apr 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants