Project struct
Proj
--wwwroot/scripts
--scripts
--tsconfig.json
----Account
------app.ts
--gulpfile.js
Given the following TypeScript in scripts/Account/app.ts:
function sayHello() {
alert('hello world');
}
a tsconfig.json file of
{
"compilerOptions": {
"noImplicitAny": true,
"noEmitOnError": true,
"sourceMap": true,
"target": "es6",
"lib": [ "es6", "dom" ]
},
"compileOnSave": false
}
and a gulpfile.js of
var gulp = require('gulp');
var browserify = require('browserify');
var source = require('vinyl-source-stream');
var tsify = require('tsify');
gulp.task('default', function () {
return browserify({
basedir: '.',
entries: ['scripts/Account/app.ts'],
cache: {},
packageCache: {}
})
.plugin(tsify)
.bundle()
.pipe(source('scripts/Account/app.js'))
.pipe(gulp.dest("wwwroot/scripts"));
});
I get an outputted file in wwwroot/scripts/app.js. But the function can't be called from the web pages that use it because it isn't available globally as it appears to be wrapped in a function:
So what am I missing in my compilation steps to be able to call a basic typescript function? I've tried exporting the function, in which case it just adds it to the exports object:
Configuration : Specify a name to be used as standalone lib
gulp.task('default', function () {
return browserify({
basedir: '.',
entries: ['scripts/Account/app.ts'],
cache: {},
packageCache: {},
standalone : "XLib", // specify a name to be used as standalone lib
})
.plugin(tsify)
.bundle()
.pipe(source('scripts/Account/app.js'))
.pipe(gulp.dest("wwwroot/scripts"));
});
Expose the function :
export function sayHello() {
alert('hello world');
}
Consume the library in browser:
<script>
XLib.sayHello();
</script>
Demo :