Decorators with babel, unexpected token

Lee

I'm trying to use decorators on classes in React, using babelify. I have the 'es7.decorators' option applied in babel, but I keep getting an 'unexpected token' error when it encounters the '@' character.

Anyone have any ideas? A simple example is below.

Decorator:

export default function(Component) {
  return class extends Component {
    constructor() {...}
  }
}

Class:

import myDecorator from 'decorator';

@myDecorator
class MyClass{...}

I'm using babelify (Browserify transform for Babel):

browserify().transform(babelify.configure({
  optional: ['es7.decorators']
})
Lee

Thanks to @LeonidBeschastny for mentioning .babelrc file, using the config file decorators work correctly, using the setup described on the babelify readme doesn't work, for whatever reason (not sure if my setup or something else).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Babel transpiling es7 class decorators Unexpected token error

Babel Decorators transform, constantly get unexpected token (with react project) no matter what I try

Django + Babel + Webpack unexpected token

React Unexpected token < - babel loader

babel watch SyntaxError: Unexpected token

Babel, Unexpected token (15:33) ::

Webpack/babel Unexpected token, expected ";"

babel submodule unexpected token import

Unexpected token with react jsx and babel

Unexpected Token @ when using babel decorator

Unexpected token in jsdom when used with babel and npx

webpack + babel - react, unexpected token 'import'

unexpected token import when using Mocha with Babel

babel-loader jsx SyntaxError: Unexpected token

Webpack, React, JSX, Babel - Unexpected token <

SyntaxError: Unexpected token using async and babel in koa

Babel unexpected token compiling react component

Babel: replaceWithSourceString giving Unexpected token (1:1)

Webpack - Babel - Parsing JSX: SyntaxError: Unexpected token

Babel 6 CLI: Unexpected token export?

React Serverside rendering Unexpected token, JSX and Babel

Babel7 Jest unexpected token export

Laravel 8 babel unexpected token '<' and ';' expected on className

Unexpected token '@' when using ES6 decorators

ES7 Mobx Decorators with Babelify 7 and browserify, unexpected token @

Unexpected token 'const' When running webpack, babel and karma

Babel/browserify throws "Error while parsing JSON - Unexpected token o"

babel-preset-env SyntaxError: Unexpected token with spread properties

Browserify, Babel 6, Gulp - Unexpected token on spread operator