我正在用打字稿编写一个小游戏引擎,当我将其编译为 javascript 时,运行 javascript 时出现错误。它编译也没有错误。
我的主入口文件(main.ts)以这两行开头:
require('./core/Obj');
require('./core/Component');
它构建得很好,但是当我运行它时,第二个 require 有一些问题并给出了这个错误:
Uncaught TypeError: Class extends value undefined is not a function or null
核心/对象.ts
namespace GameEngine {
export class Obj {
// Some functions/methods
}
}
核心/组件.ts
namespace GameEngine {
export class Component extends Obj {
}
}
然后编译后,它看起来像这样:
(function (exports, require, module, __filename, __dirname, process, global) { (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[
function(require,module,exports){
var GameEngine;
(function (GameEngine) {
class Component extends GameEngine.Obj { // Error is here
}
GameEngine.Component = Component;
})(GameEngine || (GameEngine = {}));
},{}],
5:[function(require,module,exports){
var GameEngine;
(function (GameEngine) {
class Obj {
}
GameEngine.Obj = Obj;
})(GameEngine || (GameEngine = {}));
},{}]
});
这是我正在运行的 gulp 任务:
gulp.task('compile-engine', function () {
return browserify()
.add('./GameEngine/main.ts')
.plugin(tsify, {})
.bundle()
.on('error', function (error) { throw error; })
.pipe(source('gameEngine.js'))
.pipe(buffer())
.pipe(gulp.dest('build/'));
});