简体   繁体   中英

Can't get browserify to use babel-plugin-transform-class-properties

I'm trying to use MobX in my project and I am attempting to use class properties. However, when I run through browserify (with Laravel's Elixir ). I get the error Missing class properties transform. while parsing file Missing class properties transform. while parsing file . Is there something I'm missing to get class properties to work with browserify?

Browserify Failed!: /Users/.../resources/assets/js/pages/Show/CampaignStore.js: Missing class properties transform. while parsing file: /Users/.../resources/assets/js/pages/Show/CampaignStore.js
 Missing class properties transform.
  2 |
  3 | class CampaignStore {
> 4 |   id = Math.random();
    |   ^
  5 |   @observable title = '';
  6 |   @observable messages = [];
  7 |

My .babelrc file:

{
  "presets": [
    "es2015",
    "react",
    "stage-0"
  ],
  "plugins": ["transform-decorators-legacy", "transform-class-properties"]
}

The class

import { observable, computed } from 'mobx';

class CampaignStore {
  id = Math.random();
  @observable title = '';
  @observable messages = [];

  // ...
}

gulpfile.js

require('dotenv').config();
var elixir = require('laravel-elixir');

var HOST = process.env.SERVER || 'http://localhost';

elixir.config.js.browserify.transformers.push({
    name: 'babelify',
    options: {
        presets: ["es2015", "react", "stage-1"],
        plugins: ["transform-class-properties"]
    }
});

elixir(function(mix) {
    mix.browserify('pages/Show.js', 'public/js/bundles/show.js');

    mix.sass('clean.scss');

    mix.browserSync({
        proxy: HOST
    });
});

You should override exactly babelify transformer:

elixir.config.js.browserify.transformers
  .find(transformer => transformer.name === 'babelify')
  .options = {
    presets: ['es2015', 'react', 'stage-1'],
    plugins: ['transform-class-properties'],
  };

or just use the same babel config file:

elixir.config.js.browserify.transformers
  .find(transformer => transformer.name === 'babelify')
  .options = require('./package.json').babel;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM