尝试将 Mobx 装饰器与 Babelify 7.3.0 和 Browserify 14.4.0 一起使用。诚然,这是非常新的,这是该堆栈的第一次设置。从 babel 获取装饰器的语法错误。
{ SyntaxError: /home/forrest/code/noat/views/js/main.js: Unexpected token (24:23)
22 |
23 | class incrementStore {
> 24 | @observable number = 0;
| ^
25 |
26 | constructor() {
27 | mobx.autorun(() => console.log(this.report));
这是有问题的代码
const React = require('react');
const ReactDOM = require('react-dom');
const r = require('r-dom')
const $ = global.jQuery = require('jquery');
//bootstrap = require('bootstrap')
const rbs = require('react-bootstrap');
import {observer} from 'mobx-react';
//an example react component to render stuff
@observer
class Incrementer extends React.Component {
//@observer
render() {
const store = this.props.store;
return(r.div([
r.h1("#{this.props.count}"),
r(rbs.Button, {bsStyle: 'info', onClick:Incrementer.increment}[r.span('Increment')])
]))
}
}
//an example model to hold onto data
class incrementStore {
@observable number = 0;
constructor() {
mobx.autorun(() => console.log(this.report));
}
@computed get getNumber() {
return number;
}
increment() {
number += 1;
}
}
let mainElement = r(Incrementer);
$(document).ready(()=> {
ReactDOM.render(mainElement, $('#react-container')[0]);
});
这是我的构建系统:
const gulp = require('gulp');
const browserify = require('browserify');
const babelify = require('babelify');
const source = require('vinyl-source-stream');
let browserifyOptions = {entries: './js/main.js', extensions: ['.js'], debug: true}
gulp.task('build', function () {
return browserify()
.transform(babelify)
.bundle()
.pipe(source('bundle.js'))
.pipe(gulp.dest('dist'));
});
gulp.task('watch', ['build'], function () {
gulp.watch('*.js', ['build']);
});
gulp.task('default', ['watch']);
*/
const watchify = require('watchify');
const browserify = require('browserify');
const babelify = require('babelify');
const gulp = require('gulp');
const uglify = require('gulp-uglify');
const source = require('vinyl-source-stream');
const buffer = require('vinyl-buffer');
const gutil = require('gulp-util');
const sourcemaps = require('gulp-sourcemaps');
const assign = require('lodash.assign');
let customBrowserifyOpts = {
entries: ['./views/js/main.js'],
debug: true//process.env.NODE_ENV !== 'production'
};
let browserifyOpts = assign({}, watchify.args, customBrowserifyOpts);
let b = watchify(browserify(browserifyOpts));
bundle = function() {
return b
.transform(babelify)
.bundle()
.on('error', gutil.log.bind(gutil, 'Browserify Stack Threw Error'))
.pipe(source('bundle.js'))
.pipe(buffer())
.pipe(sourcemaps.init({loadMaps: true}))
.pipe(uglify())
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('./public'));
};
gulp.task('js', bundle);
b.on('update', bundle);
b.on('log', gutil.log);
bundle();
最后是我的 .babelrc,我确认它可以被 babel 正确读取。
{
"presets": ["es2015"],
"plugins": ["transform-decorators"],
"sourceMaps": true
}