项目作者: mobxjs

项目描述 :
Reduces `action` and `runInAction` boilerplates
高级语言: JavaScript
项目地址: git://github.com/mobxjs/babel-plugin-mobx-deep-action.git
创建时间: 2016-12-01T19:41:10Z
项目社区:https://github.com/mobxjs/babel-plugin-mobx-deep-action

开源协议:

下载


babel-plugin-mobx-deep-action

Build Status
npm version

Allow to reduce boilerplate of writing async actions.
Based on assumption, that all code created inside an action,
should be handled as action too.

This plugin scans for all functions, marked as actions, and then marks all
nested functions, which created inside actions as actions too.

Example

In

  1. import { action } from "mobx";
  2. action(function doSome() {
  3. fetch("/api/list").then(function(response) {
  4. this.items = response.dta;
  5. });
  6. });

Out

  1. "use strict";
  2. import { action } from "mobx";
  3. action(function doSome() {
  4. fetch("/api/list").then(action(function(response) {
  5. this.items = response.dta;
  6. }));
  7. });

Caveats

Plugin support only ES6 imports. Only these imports are supported:

  1. import {action} from "mobx";
  1. import {action as actionAlias} from "mobx";
  1. import * as mobx from "mobx";
  1. import * as mobxAlias from "mobx";

For example, these cases are not supported:

  1. const mobx = require("mobx")
  1. const {action} = require("mobx")
  1. import * as mobx from "my-mobx-alias"
  1. import * as mobx from "mobx";
  2. const {action} = mobx;
  3. action(function() {});

Installation

  1. $ npm install babel-plugin-mobx-deep-action

Usage

.babelrc

  1. {
  2. "plugins": ["mobx-deep-action"]
  3. }

Via CLI

  1. $ babel --plugins mobx-deep-action script.js

Via Node API

  1. require("babel-core").transform("code", {
  2. plugins: ["mobx-deep-action"]
  3. });

Usage for async and generator functions." class="reference-link"> Usage for async and generator functions.

see https://github.com/Strate/babel-plugin-mobx-async-action

Typescript decorators." class="reference-link"> Typescript decorators.

This plugin could handle decorators code, emitted from typescript, such as:

  1. import * as tslib_1 from "tslib";
  2. import { action } from "mobx";
  3. export default class Class2 {
  4. async method() {
  5. const a = (other) => { };
  6. return a(function () { });
  7. }
  8. }
  9. tslib_1.__decorate([
  10. action
  11. ], Class2.prototype, "method", null);

To get this code worked, you should enable importHelpers
compiler option, and get tslib package installed. Also, typescript
should emit es6 modules, so, you should target your compiler to es2015+. That’s all,
plugin detect import from “tslib” and handle typescript decorators.

Use other package." class="reference-link"> Use other package.

If you use wrapper for “mobx” package, you can pass it’s name to plugin:

.babelrc

  1. {
  2. "plugins": [
  3. ["mobx-deep-action", {
  4. "mobx-package": "mobx-custom"
  5. }]
  6. ]
  7. }