浏览器无法使用babel-plugin-transform-class-properties

时间:2016-05-10 17:19:34

标签: javascript browserify babeljs laravel-elixir

我正在尝试在我的项目中使用MobX,并且我正在尝试使用类属性。但是,当我浏览browserify时(使用Laravel's Elixir)。我收到错误Missing class properties transform. while parsing file。有没有什么我想让类属性与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 |

我的.babelrc文件:

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

班级

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
    });
});

1 个答案:

答案 0 :(得分:2)

你应该完全覆盖babelify变换器:

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

或者只使用相同的babel配置文件:

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