I am having issues with this game.js code that the game.ts file compiles to:
var GameObjects = require("./GameObjects")
I have set up my page index.html js imports as so:
<script language="javascript" src="javascripts/require.js" type="text/javascript"> </script>
<script language="javascript" src="javascripts/jquery-1.8.2.min.js" type="text/javascript"> </script>
<script language="javascript" src="ts/GameObjects.js" type="text/javascript"> </script>
<script language="javascript" src="ts/game.js" type="text/javascript"> </script>
and this is the error in chrome:
Uncaught ReferenceError: exports is not defined GameObjects.js:82
Uncaught Error: Module name "GameObjects" has not been loaded yet for context: _. Use require([]) http://requirejs.org/docs/errors.html#notloaded
any ideas friends?
I'm not sure where the idea of hand-editing the JavaScript files has come from. If you compile your TypeScript with the --module amd flag, you shouldn't need to edit the JavaScript files as the import statements will be converted into require statements.
The only script tag you should need on your page is this:
<script src="javascripts/require.js" data-main="ts/game.js"></script>
Once require.js has loaded, it will then load game.js, and whenever it comes across a require statement (which is your import statement in your TypeScript file) it will then load that script and then execute the code.
You can load in jQuery and GameObjects and any other modules just by adding import statements in your TypeScript file.
You have no data-main attribute in your require.js script tag. Please read the RequireJS documentation carefully.
In a nuthshell: you should be loading ts/GameObjects.js from a top-level require call, which you put in the file specified in the data-main attribute. e.g. (from the docs):
<!DOCTYPE html>
<html>
<head>
<title>My Sample Project</title>
<!-- data-main attribute tells require.js to load
javascripts/main.js after require.js loads. -->
<script data-main="javascripts/main" src="javascripts/require.js"></script>
</head>
<body>
<h1>My Sample Project</h1>
</body>
</html>
Then in javascripts/main.js (you can actually call it whatever you want, as long as it matches what you put in data-main), you call require and load your modules and do whatever you want with them:
require(["ts/GameObjects.js", "ts/game.js"], function(GameObjects, Game) {
... use 'GameObjects' and 'Game' here ...
});
Remember that in ts/GameObjects.js and ts/game.js you need to wrap your code in define() calls, so they are interpreted as modules.
Hope that helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With