Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Helpers missing from output file for a TS script (instead of module) #48

Closed
mohd-akram opened this issue Dec 25, 2017 · 6 comments
Closed
Assignees
Labels
kind: bug Something isn't working properly solution: workaround available There is a workaround available for this issue

Comments

@mohd-akram
Copy link

I get a Uncaught ReferenceError: __awaiter is not defined when compiling a file with async. I'm using the latest TypeScript (2.6.2), rollup (0.53.0) and plugin (0.9.0).

main.ts

async function main() {
}
main();

rollup.config.js

import typescript from 'rollup-plugin-typescript2';

export default {
  output: {
    format: 'iife'
  },
  plugins: [
    typescript()
  ]
}

tsconfig.json

{
  "compilerOptions": {
    "lib": [
      "es2015"
    ]
  }
}

main.js:

(function () {
'use strict';

function main() {
    return __awaiter(this, void 0, void 0, function () {
        return __generator(this, function (_a) {
            return [2 /*return*/];
        });
    });
}
main();

}());
@ezolenko ezolenko self-assigned this Dec 25, 2017
@ezolenko
Copy link
Owner

ezolenko commented Dec 25, 2017

Looks like you are hitting this one: microsoft/TypeScript#12167 -- TS treats this as global script, not a module, so it doesn't generate tslib imports.

When I add any import or export to main.ts, I get __awaiter/__generator imported.

The plugin forces "noEmitHelpers": true, "importHelpers": true to avoid multiple copies of tslib. Let me know if you can't make your code into a module for some reason, so far I haven't seen a reason those options should not be forced.

@mohd-akram
Copy link
Author

Did a quick test — seems like setting importHelpers: true alone is sufficient to prevent duplicates while fixing this problem.

@ezolenko
Copy link
Owner

ezolenko commented Dec 26, 2017

If TS emits helpers in each js file (like it would do when "noEmitHelpers": false), then rollup will happily rename them when making a bundle, so you'd end up with __generator$1, __generator$2, etc.

Could you post the test? Did you use 2 separate modules? Last time I checked it did that anyway, I'll try again later.

@mohd-akram
Copy link
Author

I modified rollup-plugin-typescript2.cjs.js in node_modules to remove noEmitHelpers: true and didn't find any duplicates of __awaiter. I then removed importHelpers: true which caused __awaiter$1 to be added to the output. Both modules had async functions in them.

To test with tsc:

tsc main.ts --importHelpers --module es2015 --lib es2015 --moduleResolution node

main.ts:

import f from './module';

async function main() {
  await f();
}
main();

module.ts:

async function f() {
}
export default f;

main.js:

import * as tslib_1 from "tslib";
import f from './module';
function main() {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        return tslib_1.__generator(this, function (_a) {
            switch (_a.label) {
                case 0: return [4 /*yield*/, f()];
                case 1:
                    _a.sent();
                    return [2 /*return*/];
            }
        });
    });
}
main();

module.js

import * as tslib_1 from "tslib";
function f() {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        return tslib_1.__generator(this, function (_a) {
            return [2 /*return*/];
        });
    });
}
export default f;

@ezolenko
Copy link
Owner

ezolenko commented Jan 5, 2018

Thanks, I'll test on a few versions of typescript and remove noEmitHelpers override if it works everywhere.

@ezolenko
Copy link
Owner

ezolenko commented Jan 15, 2018

This is in 0.10.0

@agilgur5 agilgur5 changed the title Helpers missing from output file Helpers missing from output file when using CJS (CTS) May 25, 2022
@agilgur5 agilgur5 added kind: bug Something isn't working properly solution: workaround available There is a workaround available for this issue labels May 25, 2022
Repository owner locked as resolved and limited conversation to collaborators May 25, 2022
@agilgur5 agilgur5 changed the title Helpers missing from output file when using CJS (CTS) Helpers missing from output file for a TS script (instead of module) May 25, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
kind: bug Something isn't working properly solution: workaround available There is a workaround available for this issue
Projects
None yet
Development

No branches or pull requests

3 participants