rollup/rollup-plugin-typescript

Handling export with namespace

vogloblinsky opened this issue · 4 comments

Hi, since initial release, the fix function tried to handle the "TypeScript's broken handling of export class"

With typescript 2.0.3, i can compile this code without problem :

export namespace Engine {
    export class Html {
        constructor() {

        }
        render() {
            console.log('Html engine render');
        }
    }
};

and get this

"use strict";
var Engine;
(function (Engine) {
    class Html {
        constructor() {
        }
        render() {
            console.log('Html engine render');
        }
    }
    Engine.Html = Html;
})(Engine = exports.Engine || (exports.Engine = {}));

with version 0.8.1 i get this :

var Engine;
(function (Engine) {
    var Html = (function () {
        function Html() {
        }
        Html.prototype.render = function () {
            console.log('Html engine render');
        };
        return Html;
    }());
})(Engine || (Engine = {}));

export { Engine, Html };

Is it normal to export Engine namespace if there is nothing in it ?

When using TypeScript 2.2.2 with compiler option "module": "ES2015" i get the following output:

export var Engine;
(function (Engine) {
    var Html = (function () {
        function Html() {
        }
        Html.prototype.render = function () {
            console.log('Html engine render');
        };
        return Html;
    }());
    Engine.Html = Html;
})(Engine || (Engine = {}));
;

This seems to be ok, but this plugin outputs:

(function (exports) {
'use strict';



exports.Html = Html;

}((this.myLib = this.myLib || {})));

It works when i use the TypeScript compiler and rollup seprately, the output is:

(function (exports) {
'use strict';

(function (Engine) {
    var Html = (function () {
        function Html() {
        }
        Html.prototype.render = function () {
            console.log('Html engine render');
        };
        return Html;
    }());
    Engine.Html = Html;
})(exports.Engine || (exports.Engine = {}));

}((this.myLib = this.myLib || {})));

Does this plugin work with compiler option "module": "ES2015"?

import typescript from 'rollup-plugin-typescript';

export default {
  format: 'iife',
  moduleName: 'myLib',
  entry: './test.ts',
  dest: 'test.js',
  plugins: [
    typescript({
	  typescript: require('typescript')
	})
  ]
}

Additionally:

// test.ts
export namespace MODE {
	export abstract class MODE {
	}
}
// rollup.config.js
import typescript from 'rollup-plugin-typescript';
import * as ts from 'typescript'
export default {
	entry: './test.ts',

	plugins: [
		typescript({typescript: ts})
	]
}

results in

./test.ts → stdout...
[!] Error: Duplicate export 'MODE'
test.ts (9:9)
 7:     }());
 8: })(MODE || (MODE = {}));
 9: export { MODE };
             ^
10: //# sourceMappingURL=test.js.map
11: import { __assign, __awaiter, __extends, __decorate, __metadata, __param } from ' typescript-helpers';

I have something strange with namespace export too, given

export class Foo extends React.Component {
  render() {
    return <Foo.Component />;
  }
}
export namespace Foo {
  export class Component extends React.Component {
    render() {
      return 'foo';
    }
  }
}

I use Foo in some other component, only outer Foo component will be can be found in bundle, Foo.Component is not included.

Even more, if use export class from namespace in imported file, it got tree-shaked:

// Methods.tsx
export namespace Methods {
  export class Foo {
    name() { return 'foo' }
  }
}
/// index.tsx
import { Methods } from './Methods';
console.log((new Methods.Foo()).name());

Output with error:

var Methods;
(function (Methods) {
})(Methods || (Methods = {}));