grant / ts2gas

A function that transpiles TypeScript to Google Apps Script.

Home Page:http://npmjs.com/ts2gas

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

function imports are strange

smcjones opened this issue · comments

commented

I wrote a test:

    printBeforeAndAfter(
      `import {foo} from './another_place';

foo()`,
    );

Here's the output:

# testImport
v--TS--v
import {foo} from './another_place';

foo()
---
// Compiled using ts2gas 4.2.0 (TypeScript 4.9.3)
var exports = exports || {};
var module = module || { exports: exports };
//import {foo} from './another_place';
(0, another_place_1.foo)();
^--GS--^

Expected output:

# testImport
v--TS--v
import {foo} from './another_place';

foo()
---
// Compiled using ts2gas 4.2.0 (TypeScript 4.9.3)
var exports = exports || {};
var module = module || { exports: exports };
//import {foo} from './another_place';
foo();
^--GS--^

Alternatively, if another_place.ts exports files, a different expectation would be for another_place.ts to define another_place_1 as its export variable:

var another_place_1 = another_place_1 || {};
var exports = exports || {};

// ...

exports.foo = another_place_1.foo = 'foo';

Or a namespace...

another_place.ts

namespace another_place_1 {
    export function foo() {}
}

main.ts

// import {foo} from './another_place';
const {foo} = another_place_1;

@smcjones I was facing this same issue and have proposed a PR to potentially fix this issue in #80.

Here is an example typescript input:

import { func } from 'bob';
func();

and with the PR, the produced output looks like this:

// Compiled using ts2gas 4.2.0 (TypeScript 4.9.5)
var exports = exports || {};
var module = module || { exports: exports };
var bob_1 = bob_1 || { func: func }; //import { func } from 'bob';
(0, bob_1.func)();

It's not the same solution you were proposing, but I think it would work for you and be the most compatible with how the typescript compiler generates its code.

Hey! I think this may or may not be related to an issue I have been encountering recently. I created a separate, simple project to see if I could replicate the issue and indeed I did. On my local machine, we have two typescript files, one that imports the other:

// functionA.ts

export function functionA(): void {
    console.log('functionA')
}
// functionB.ts

import { functionA } from "./functionA";

export function functionB(): void {
    functionA();
    console.log('functionB');
}

After running a clasp push...

// functionA.gs

// Compiled using ts2gas-test 1.0.0 (TypeScript 4.9.5)
var exports = exports || {};
var module = module || { exports: exports };
exports.functionA = void 0;
function functionA() {
    console.log('functionA');
}
exports.functionA = functionA;
// functionB.gs

// Compiled using ts2gas-test 1.0.0 (TypeScript 4.9.5)
var exports = exports || {};
var module = module || { exports: exports };
exports.functionB = void 0;
//import { functionA } from "./functionA";
function functionB() {
    (0, functionA_1.functionA)();
    console.log('functionB');
}
exports.functionB = functionB;

If you try to run functionB using the manual run button in the GAS IDE, you'll get this error:

ReferenceError: functionA_1 is not defined
functionB	@ functionB.gs:7

Would be happy to connect with anyone about this issue, been a huge bummer for me lately. Curious to see if PR #80 might fix this

I posted a sub-optimal workaround on Stack Overflow to get around this issue. It's sub-optimal because you need to rewrite your functions as classes, which is obviously a huge pain in the neck.