I'm using typescript and requirejs. I want to import my library code as modules, to avoid making everything an ambient global. My problem is that any use of import or export in a typescript file turns the entire file into a module. In the case of my application code, this means it can't be invoked directly.
Here's a sample app.
index.html
<!DOCTYPE html>
<html>
<head>
<title>My Test App</title>
<script src="require.js"></script>
<script src="app1.js"></script>
<script src="app2.js"></script>
</head>
</html>
tsconfig.json
{
"compilerOptions": {
"module": "amd",
"target": "es5",
"noImplicitAny": false,
"sourceMap": false
}
}
greeter.ts
export function greet(name: string) {
alert(`Hello ${name}`);
}
app1.ts
import { greet } from './greeter';
greet('alice');
app2.ts
import { greet } from './greeter';
greet('bob');
tsc
runs with no problem, but here's one of the files I get as output.
tsc output: app1.js
define(["require", "exports", './greeter'], function (require, exports, greeter_1) {
"use strict";
greeter_1.greet('alice');
});
That doesn't work. From require.js
Be sure to load all scripts that call define() via the RequireJS API. Do not manually code script tags in HTML to load scripts that have define() calls in them.
So how can I convince the typescript compiler to produce output without a define()
call in it. In app1.ts, I don't intend to define a module, just consume other modules. require()
seems more appropriate for that. Is there a way to make import
compile to require()
?