2

您好我正在尝试将 js 代码转换为浏览器可以执行的东西。

实用程序/Logger.js

'use strict';

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

/**
 *
 * Logger class
 **/

var Logger = function () {

  /**
   * Logger constructor
   * @param   {String} channel - Logger channel
   **/

  function Logger(channel) {
    _classCallCheck(this, Logger);

    /**
     * Logger channel
     * @type    {String}
     * @private
     **/
    this._channel = channel;
  }

  _createClass(Logger, [{
    key: 'log',


    /**
     * Log text
     * @param {String} text - String to print
     **/
    value: function log(text) {
      console.log('[' + this._channel + ']' + text);
    }
  }]);

  return Logger;
}();
//# sourceMappingURL=logger.js.map

jet.js

'use strict';

require('./utils/Logger');

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

/**
 * Jet Framework main class
 **/

var jet =

/**
 * Jet constructor
 * @param   {String} element
 **/
function jet(element) {
  _classCallCheck(this, jet);

  /**
   * Element html layer
   * @type    {HTMLElement}
   * @private
   **/
  this._element = document.getElementById(element);

  /**
   * Logger instance
   * @type    {Logger}
   * @private
   **/
  this._logger = new Logger(this.channel);
};

;

/**
 * Channel constant
 * @type    {String}
 * @const
 **/
jet.prototype.channel = 'JET';
//# sourceMappingURL=jet.js.map

描述

当我把它jet.js放在浏览器中时,它会出现以下错误:

Uncaught ReferenceError: require is not defined

我在这里读了一个问题,我决定使用browserify+babelify组合。当我在终端中引入下一个命令时,它显示了一个依赖错误。

我究竟做错了什么?我需要什么才能正确运行它?

贾斯特。

命令

browserify dist/src/jet.js -o out/bundle.js -t [ babelify --presets [ es2015 react ] ]

输出

Error: Cannot find module './utils/Logger' from '/home/ismael/projects/js-evolve/es-2015/dist/src'
    at /usr/local/lib/node_modules/browserify/node_modules/resolve/lib/async.js:55:21
    at load (/usr/local/lib/node_modules/browserify/node_modules/resolve/lib/async.js:69:43)
    at onex (/usr/local/lib/node_modules/browserify/node_modules/resolve/lib/async.js:92:31)
    at /usr/local/lib/node_modules/browserify/node_modules/resolve/lib/async.js:22:47
    at FSReqWrap.oncomplete (fs.js:82:15)
4

0 回答 0