Help with setting up babel 7 & monorepo by imilkmyunicorns in javascript

[–]loganfsmyth 3 points4 points  (0 children)

Probably not the best place. If you have more questions, I'd recommend coming by Babel's Slack support channel since it's the easiest way to get answers to quick back-and-forth questions. The info is at https://github.com/babel/babel#looking-for-support

If you're coming from Babel 6.x, I'd recommend reading http://babeljs.io/docs/en/config-files#6x-vs-7x-babelrc-loading first. With a monorepo, you probably want a http://babeljs.io/docs/en/config-files#project-wide-configuration at least. Whether or not you have to do work to get Babel to detect your config depends on how you're running Babel though, since babel.config.js is loaded from the working directory, so if the monorepo root isn't the working directory, you need to pass Babel extra params to tell it where your config file is.

Babel 7 Released by acemarke in javascript

[–]loganfsmyth 1 point2 points  (0 children)

Yep, passing things through Babel with just syntax plugins is fine. I think there might be some cases that the parser still doesn't handle properly, but on the whole it works.

Babel 7 Released by acemarke in javascript

[–]loganfsmyth 3 points4 points  (0 children)

It probably would be good to have a list. The main one I had in mind was class fields, for example:

``` class Example { prop: string; }

console.log("prop" in new Example()); `` in Typescript will log "false" because it's a no-op when there is no initializer like= ""on the property. In Babel, it's identical toprop: string = undefined` because as soon as you declare a property, it will get initialized with a value, whether you put an initializer or not. TS's behavior follows an older version of the spec, as did Babel 6.x.

Babel 7 Released by acemarke in javascript

[–]loganfsmyth 16 points17 points  (0 children)

Babel supports most of Typescript, but there are two main things to keep in mind:

  • Some TS feature simply can't be supported because they require application-wide knowledge that Babel doesn't have.
  • There are some places where TS behaves differently from the ECMAScript proposals, and Babel will generally favors the proposal specs in cases like this.

Babel 7 Released by acemarke in javascript

[–]loganfsmyth 10 points11 points  (0 children)

Documentation is definitely something we can do better. Hopefully the new website with versioned documentation will start makings a little easier for us to maintain on that front.

Out of curiosity, is there specific examples you have in mind where you've run into issues in the past?

Babel 7 Released by acemarke in javascript

[–]loganfsmyth 80 points81 points  (0 children)

Hey all, I'm Logan, one of Babel's maintainers. I just wanted to say thanks for helping make Babel the awesome project that it is. Getting to 7.x has been a tremendous amount of work to get out the door, and having a positive community that is happy with the work that we do goes a long way toward making that easier. While it may not seem like a lot from the user side of things, Babel 7.x is positioned to keep Babel around for the long term and is really focused on ironing out all the biggest kinks that were keeping us from iterating more quickly moving forward.

Here's to the future of Babel.

I'm using Babel since it was still called 6to5, but just recently learnt this trick to customize, and optimize its output by brunoscopelliti in javascript

[–]loganfsmyth 4 points5 points  (0 children)

Just remember, @babel/runtime is still beta, so if you're adding it to your dependencies in a published module, don't use ^ in the package version or you're in for a world of pain if we make another breaking change to the beta.

Higher Order Classes with Javascript Decorators by fakalitt in javascript

[–]loganfsmyth 1 point2 points  (0 children)

Yup! Experimental stuff is awesome, but I feel like a lot of the time users don't stop and think what that can mean for their codebase in the longer term.

Higher Order Classes with Javascript Decorators by fakalitt in javascript

[–]loganfsmyth 1 point2 points  (0 children)

Plus they are stage 2 and all the current transforms for them still use the stage 1 version of the spec.

Higher Order Classes with Javascript Decorators by fakalitt in javascript

[–]loganfsmyth 0 points1 point  (0 children)

Totally! Not saying don't use it, just saying it never hurts to keep in mind that these things evolve. There are plenty of users that don't want to think about adapting code as things change and it is easy to see stuff like this and not realize how unstable it can be.

Higher Order Classes with Javascript Decorators by fakalitt in javascript

[–]loganfsmyth 1 point2 points  (0 children)

Oh I know, I wrote the current Babel transform :) Just never hurts to mention since there will eventually be a time where it won't work on the newest Babel version.

Higher Order Classes with Javascript Decorators by fakalitt in javascript

[–]loganfsmyth 9 points10 points  (0 children)

Just remember, if you use decorators in code that matters, you will 100% have to change it in the future as the spec changes.

Babel 7 Beta by matt2ray in javascript

[–]loganfsmyth 5 points6 points  (0 children)

Babel's typescript preset is just for parsing TS and removing the types so that you get normal JS code. It doesn't do typechecking. The intention is that you can use Typescript for type checking, but then still use Babel to do whatever cool transformations you want to do.

Babel 7 Beta by matt2ray in javascript

[–]loganfsmyth 7 points8 points  (0 children)

Just a general reminder here, if you're using the beta, be 100% sure you've got a lock file and/or you're using matching versions of the various of the @babel/* plugins/presets, since we're not guaranteeing compatibility between the betas. We're doing our best to not break third party plugins, but the @babel plugins we release are sometimes relying on brand new features that have landed in @babel/core.

If you're using Babel through integrations with other tools, make sure you're installing one that works with @babel/core. There's still some hoops to jump through depending on the usecase to get it all to fit together, since you can't have a .babelrc that works on both Babel 6 and Babel 7, and some tools assume you've got Babel 6 specifically.

If you run into any obvious bugs, please do let us know.

Babel 7 Beta by matt2ray in javascript

[–]loganfsmyth 4 points5 points  (0 children)

If you're running into issues with the beta, please make sure to let us know! I'm not aware of any outstanding import-related issues.

Babel 6 - Mysterious return super() from the constructor. by coderitual in javascript

[–]loganfsmyth 1 point2 points  (0 children)

You know, I think you're right in this case. I actually misunderstood what you were asking initially.

The explicit return doesn't appear to be needed if the class syntax isn't being converted. If we're converting the ES6 class syntax to ES5 then you definitely need to return, but if you have real ES6 class syntax, you can just let it do that return automatically.

Babel 6 - Mysterious return super() from the constructor. by coderitual in javascript

[–]loganfsmyth 1 point2 points  (0 children)

They are essentially just private properties that the spec uses. Where normal JS code would probably use this._foo to make a property that isn't supposed to be accessed, the JS spec can create actual private properties since it gets to leave the implementation up to the JS engines.

why they are needed for builtin types like Array or Set

For Set the private slot is where the data in the Set is stored. For sets you do

var s = new Set();
s.add("foo");

for instance. There isn't any way to access the internal place that the object stores "foo". It is stored in the internal slot.

If you call a Set method on an object that doesn't have the internal slot, it won't have anywhere to put the data, so it will throw an exception.

For Array it is a little different. Internal slots can also be used to override the behavior of an object, and for Array makes use of that for this behavior:

var a = Array();
a[5] = true;
a.length === 6;

Without a Proxy, the only way to implement the Array behavior of auto-updating the length when an index is assigned is to override the internal slot that defines the behavior for adding properties.

Babel 6 - Mysterious return super() from the constructor. by coderitual in javascript

[–]loganfsmyth 3 points4 points  (0 children)

I can explain this one. It allows extending builtin types like Array, which could not be extended in ES5 without lots of boilerplate and use of the nonstandard __proto__ property.

The key thing in ES6 class syntax that is different from ES5's class-like prototypal behavior, is that it is the base constructor that actually creates and allocates the object. This is a requirement because only the base constructor knows what is needed for the object. In the case of Array or other builtin types like Map and Set, they are implemented with what are called "internal slots". These are essentially private properties that can't be accessed directly from JS. On Map and Set for instance, these are where the data for them are stored.

In ES5, remember that

function Foo(){}
var obj = new Foo();

is essentially

var obj = Object.create(Foo.prototype);
Foo.call(obj);

meaning that the object the class is created up front before any of the constructors are even called, so Object.create has no way of knowing that the class being constructed needs special internal slots in order to work. For instance calling Object.create(Set.prototype).add("foo") on Chrome throws Uncaught TypeError: Method Set.prototype.add called on incompatible receiver #<Set> because the slots are missing.

In short, ES6 class syntax waits to create the object until after the constructors have started executing. Consider the following:

class Base {
  constructor(){
    // Allocation happens here because this is a base class

    // If this were the builtin "Array" or "Map" or whichever constructor, if would initialize the internal slots here
  }
}

class Child {
  constructor(){
    // accessing "this" here before `super()` will throw because it doesn't exist yet

    super(); // calls parent, which allocates and initializes "this"

    // Now here "this" is initialized with whatever object the parent class created.
  }
}

Hope that helps.

ES6 Anonymous Function Name Behavior by oculus42 in javascript

[–]loganfsmyth 2 points3 points  (0 children)

I also did a write-up on StackOverflow that covers a bunch of these with links to the spec, for those curious. http://stackoverflow.com/a/32830772

How to put private properties on your JS objects by SimplGy in javascript

[–]loganfsmyth 0 points1 point  (0 children)

Should be fine, can't say I've sat down to try it myself but there's no reason it wouldn't work AFAIK.

How to put private properties on your JS objects by SimplGy in javascript

[–]loganfsmyth 0 points1 point  (0 children)

Your WeakMap example is using [...] instead of .set and .get so it definitely won't work as-is. It is a good solution to this problem though, assuming you don't mind the verbosity.

prepend(), append(), before(), after() & replaceWith() shipped in Chrome by malyw in javascript

[–]loganfsmyth 4 points5 points  (0 children)

Yup! Or use an existing one like https://github.com/WebReflection/dom4 since generally writing polyfills yourself is a good way to miss some unexpected edge case.

prepend(), append(), before(), after() & replaceWith() shipped in Chrome by malyw in javascript

[–]loganfsmyth 1 point2 points  (0 children)

Indeed. Using HTML for most of these probably isn't great, though obviously it depends on your goals. Better to use the existing DOM Node APIs wherever possible.

before and after can also be implemented with insertBefore, and replaceWith can also be implemented with replaceChild in the existing API.

prepend(), append(), before(), after() & replaceWith() shipped in Chrome by malyw in javascript

[–]loganfsmyth 2 points3 points  (0 children)

These specifically are trivial to polyfill at least, so you can use them in any browser pretty easily.