Transform plugin vs Syntax plugin in Babel

Sander Garretsen

I want to use Class properties in my webpack setup. Following along with this book (www.survivejs.com), I noticed the author adds 2 plugins to the .babelrc file: babel-plugin-syntax-class-propertiesas well as babel-plugin-transform-class-properties.

Looking at the babel docs of the syntax-class-properties it states:

Syntax only

This plugin only allows Babel to parse this syntax. If you want to transform it then see transform-class-properties.

What's the difference? And do I need both? My code seem to run fine just with the transform plugin.

Felix Kling

The transformation is a three step process:

  1. Parse the source code into an AST
  2. Change / transform the AST
  3. Print the AST (convert to source code)

Syntax plugins are necessary for step 1: Proposals such as class properties introduce a new syntax, which cannot be parsed by current JavaScript parsers. Syntax plugins extend the parser so it understands the new syntax.

Example: Imagine I introduce a new token @, such as in

@.foo();

A JavaScript parser wouldn't be able to parse this code into an AST, because currently the @ is invalid in that position. So I'd have to extend the parser to parse it.

Transform plugins are necessary for step 2: Now that the source was parsed, we need to convert the AST nodes of the new feature into something that is valid in current JavaScript.

Example: The @ in my previous example is a new way to refer to this. To make it work in current environments which don't understand @, I need to transform and replace it with this, so that the above example becomes

this.foo();

And do I need both?

If you want to convert your code to ES5, yes.

My code seem to run fine just with the transform plugin.

You might be using a preset of something that already includes the syntax plugin.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

@babel/plugin-proposal-class-properties vs babel-plugin-transform-class-properties

From Javascript

Where is babel plugin syntax defined?

From Dev

Babel plugin: what's the difference between "transform-object-rest-spread" and "syntax-object-rest-spread"

From Dev

Babel plugin - How to transform JSX without react

From Dev

ReferenceError: Unknown plugin "babel-plugin-transform-builtin-extend"

From Dev

`@babel/runtime` and `@babel/plugin-transform-runtime` versions

From Dev

Babel plugin transform-remove-console not working with Vue CLI 4 @vue/cli-plugin-babel/preset?

From Dev

Babel: can't get "@babel/plugin-transform-destructuring" plugin to work

From Dev

Cannot find module 'babel-plugin-transform-decorators-legacy'

From Dev

Babel plugin "transform-runtime" only works after first compile

From Dev

Can't Install babel Plugin `transform-es2015-destructuring`

From Dev

babel-plugin-transform-async-to-module-method without regenerator

From Dev

No compatible version found: babel-plugin-transform-decorators@^6.13.0

From Dev

Cannot find module '@babel/preset-plugin-transform-object-assign'

From Dev

babel-core 'transform' function cannot find plugin

From Dev

babel-eslint vs eslint-plugin-babel vs eslint-plugin-react?

From Dev

Webpacker error related to module babel-plugin-syntax-dynamic-import

From Dev

@babel/plugin-syntax-dynamic-import not working in imported node module

From Dev

What does @babel/plugin-transform-block-scoping do that isn't part of babel's core behavior?

From Dev

Babel plugin not found

From Dev

Still receive syntax error for dynamic import with babel-plugin-syntax-dynamic-import when running with mocha

From Dev

Not able to spread an object with babel-plugin-transform-object-rest-spread added for vue cli 3 setup

From Dev

react native console log not showing after install babel-plugin-transform-remove-console

From Dev

How to fix the warning when use @babel/plugin-transform-typescript to compile typescript

From Dev

TypeScript 'declare' fields must first be transformed by @babel/plugin-transform-typescript

From Dev

Webpack & Babel-Loader - Unexpected Token on Object Rest Operator, with transform-object-rest-spread Plugin Installed

From Dev

How to fix regeneratorRuntime error async method with babel 7.5 + runtime + plugin-transform-runtime

From Dev

Babel plugin (babel-plugin-module-alias) with React Native

From Dev

Babel object assign plugin in Gulp

Related Related

  1. 1

    @babel/plugin-proposal-class-properties vs babel-plugin-transform-class-properties

  2. 2

    Where is babel plugin syntax defined?

  3. 3

    Babel plugin: what's the difference between "transform-object-rest-spread" and "syntax-object-rest-spread"

  4. 4

    Babel plugin - How to transform JSX without react

  5. 5

    ReferenceError: Unknown plugin "babel-plugin-transform-builtin-extend"

  6. 6

    `@babel/runtime` and `@babel/plugin-transform-runtime` versions

  7. 7

    Babel plugin transform-remove-console not working with Vue CLI 4 @vue/cli-plugin-babel/preset?

  8. 8

    Babel: can't get "@babel/plugin-transform-destructuring" plugin to work

  9. 9

    Cannot find module 'babel-plugin-transform-decorators-legacy'

  10. 10

    Babel plugin "transform-runtime" only works after first compile

  11. 11

    Can't Install babel Plugin `transform-es2015-destructuring`

  12. 12

    babel-plugin-transform-async-to-module-method without regenerator

  13. 13

    No compatible version found: babel-plugin-transform-decorators@^6.13.0

  14. 14

    Cannot find module '@babel/preset-plugin-transform-object-assign'

  15. 15

    babel-core 'transform' function cannot find plugin

  16. 16

    babel-eslint vs eslint-plugin-babel vs eslint-plugin-react?

  17. 17

    Webpacker error related to module babel-plugin-syntax-dynamic-import

  18. 18

    @babel/plugin-syntax-dynamic-import not working in imported node module

  19. 19

    What does @babel/plugin-transform-block-scoping do that isn't part of babel's core behavior?

  20. 20

    Babel plugin not found

  21. 21

    Still receive syntax error for dynamic import with babel-plugin-syntax-dynamic-import when running with mocha

  22. 22

    Not able to spread an object with babel-plugin-transform-object-rest-spread added for vue cli 3 setup

  23. 23

    react native console log not showing after install babel-plugin-transform-remove-console

  24. 24

    How to fix the warning when use @babel/plugin-transform-typescript to compile typescript

  25. 25

    TypeScript 'declare' fields must first be transformed by @babel/plugin-transform-typescript

  26. 26

    Webpack & Babel-Loader - Unexpected Token on Object Rest Operator, with transform-object-rest-spread Plugin Installed

  27. 27

    How to fix regeneratorRuntime error async method with babel 7.5 + runtime + plugin-transform-runtime

  28. 28

    Babel plugin (babel-plugin-module-alias) with React Native

  29. 29

    Babel object assign plugin in Gulp

HotTag

Archive