1 | # babel-plugin-transform-es2015-classes
|
2 |
|
3 | > Compile ES2015 classes to ES5
|
4 |
|
5 | ## Caveats
|
6 |
|
7 | Built-in classes such as `Date`, `Array`, `DOM` etc cannot be properly subclassed
|
8 | due to limitations in ES5 (for the [es2015-classes](http://babeljs.io/docs/plugins/transform-es2015-classes) plugin).
|
9 | You can try to use [babel-plugin-transform-builtin-extend](https://github.com/loganfsmyth/babel-plugin-transform-builtin-extend) based on `Object.setPrototypeOf` and `Reflect.construct`, but it also has some limitations.
|
10 |
|
11 | ## Installation
|
12 |
|
13 | ```sh
|
14 | npm install --save-dev babel-plugin-transform-es2015-classes
|
15 | ```
|
16 |
|
17 | ## Usage
|
18 |
|
19 | ### Via `.babelrc` (Recommended)
|
20 |
|
21 | **.babelrc**
|
22 |
|
23 | ```js
|
24 | // without options
|
25 | {
|
26 | "plugins": ["transform-es2015-classes"]
|
27 | }
|
28 |
|
29 | // with options
|
30 | {
|
31 | "plugins": [
|
32 | ["transform-es2015-classes", {
|
33 | "loose": true
|
34 | }]
|
35 | ]
|
36 | }
|
37 | ```
|
38 |
|
39 | ### Via CLI
|
40 |
|
41 | ```sh
|
42 | babel --plugins transform-es2015-classes script.js
|
43 | ```
|
44 |
|
45 | ### Via Node API
|
46 |
|
47 | ```javascript
|
48 | require("babel-core").transform("code", {
|
49 | plugins: ["transform-es2015-classes"]
|
50 | });
|
51 | ```
|
52 |
|
53 | ## Options
|
54 |
|
55 | ### `loose`
|
56 |
|
57 | `boolean`, defaults to `false`.
|
58 |
|
59 | #### Method enumerability
|
60 |
|
61 | Please note that in loose mode class methods **are** enumerable. This is not in line
|
62 | with the spec and you may run into issues.
|
63 |
|
64 | #### Method assignment
|
65 |
|
66 | Under loose mode, methods are defined on the class prototype with simple assignments
|
67 | instead of being defined. This can result in the following not working:
|
68 |
|
69 | ```javascript
|
70 | class Foo {
|
71 | set bar() {
|
72 | throw new Error("foo!");
|
73 | }
|
74 | }
|
75 |
|
76 | class Bar extends Foo {
|
77 | bar() {
|
78 | // will throw an error when this method is defined
|
79 | }
|
80 | }
|
81 | ```
|
82 |
|
83 | When `Bar.prototype.foo` is defined it triggers the setter on `Foo`. This is a
|
84 | case that is very unlikely to appear in production code however it's something
|
85 | to keep in mind.
|