TypeStrong / tsify

Browserify plugin for compiling TypeScript
344 stars 75 forks source link

Dont wrap bundle inside anonymous function #247

Closed tqwewe closed 5 years ago

tqwewe commented 5 years ago

I'm trying to bundle some TypeScript code into one single file, and after coming across tsify, it looked perfect but unfortunately tsify wraps the bundle inside an anonymous function causing exposed functions in the main entry file to be inside it's own scope.

For example:

// main.ts
class MyClass {
}

Will be transformed into this:

// bundle.js
(function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
class MyClass {
}
},{}]},{},[1]);

And in my particular use case, I really need MyClass to not be wrapped inside anything. Is there an option for this or anything I can do to get passed this?

cartant commented 5 years ago

This is how Browserify works. Tsify is a Browserify plugin and there's nothing that can be done about this.

tqwewe commented 5 years ago

I thought so, thanks for such a quick reply. I was able to solve it in this case by just assigning MyClass to window.

cartant commented 5 years ago

For a more 'official' way of doing it, have a look at https://stackoverflow.com/a/41518610/6680611

tqwewe commented 5 years ago

Thank you, thats exactly what I needed