gulp构建react项目二:gulp-babel处理jsx

安装项目依赖

image.png

jsx脚本

import React from 'react'

export default class Test extends React.Component {
  state = {
    msg: 'hello, world'
  }

  render() {
    const { msg } = this.state
    return <div>{msg}</div>
  }
}

构建脚本

const path = require('path')
const gulp = require('gulp')
const babel = require('gulp-babel')
const { series, parallel } = require('gulp')
const del = require('del')

const resolveDir = (dir) => path.resolve(__dirname, dir)

const clean = async (done) => {
  await del('./dist')
  done()
}

const _script = () => {
  return gulp.src(resolveDir('./test.jsx'))
  .pipe(babel({
    presets: ['@babel/preset-env', '@babel/preset-react'],
    plugins: [
      '@babel/plugin-transform-runtime', 
      ["@babel/plugin-proposal-decorators", { "legacy": true }], 
      ["@babel/plugin-proposal-class-properties", { "loose": true }]
    ]
  }))
  .pipe(gulp.dest('dist'))
}

module.exports.build = series(clean, _script)

其中,@babel/plugin-proposal-decorators@babel/plugin-proposal-class-properties插件用于解析ES7语法

构建结果

"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));

var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));

var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));

var _react = _interopRequireDefault(require("react"));

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2["default"])(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2["default"])(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2["default"])(this, result); }; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }

var Test = /*#__PURE__*/function (_React$Component) {
  (0, _inherits2["default"])(Test, _React$Component);

  var _super = _createSuper(Test);

  function Test() {
    var _this;

    (0, _classCallCheck2["default"])(this, Test);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _super.call.apply(_super, [this].concat(args));
    _this.state = {
      msg: 'hello, world'
    };
    return _this;
  }

  (0, _createClass2["default"])(Test, [{
    key: "render",
    value: function render() {
      var _state = state,
          msg = _state.msg;
      return /*#__PURE__*/_react["default"].createElement("div", null, msg);
    }
  }]);
  return Test;
}(_react["default"].Component);

exports["default"] = Test;

上面代码种,很多地方使用了require引入模块的方式,在浏览器种肯定是不支持的,解决方案可以使用browserify来处理

gulp-babelgulp-react一样,无法处理模块化

阅读 161

推荐阅读