可以't让browserfy使用babel-plugin-transform类属性

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

本文关键字:使用 babel-plugin-transform 属性 browserfy 可以      更新时间:2023-09-26

我正在尝试在我的项目中使用MobX,并尝试使用类属性。然而,当我浏览浏览时(使用Laravel的Elixir)。我得到错误Missing class properties transform. while parsing file。在使用browserfy获取类属性时,我是否缺少一些东西?

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 = [];
  // ...
}

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

您应该完全覆盖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;