clasp compile file not able to import in generated code

66 views Asked by At

I have the following typescript files

src/main.ts


function main(): void {
    console.log('main');
    hello(); 
}

src/other.ts

    console.log('hello world');
}

which generates the following in my Google App Project src/main.gs

var module = module || { exports: exports };
//import {hello} from "./other";
function main() {
    console.log('main');
    (0, other_1.hello)();
}

src/other.gs

var exports = exports || {};
var module = module || { exports: exports };
exports.hello = void 0;
function hello() {
    console.log('hello world');
}
exports.hello = hello;

When I try to run the the main() function in main.gs I get the following error: enter image description here

How can I make imports work in a clasp project?

1

There are 1 answers

2
Tanaike On BEST ANSWER

Modification points:

  • From //import {hello} from "./other"; and (0, other_1.hello)();, I'm worried that in this case, the reason for your current issue might be due to that Google Apps Script doesn't support import yet.

  • And also, in the case of Google Apps Script, for example, when function hello() {console.log("hello world");} is put into a script file of other.gs, this function can be directly called from another script file like main.gs.

From these points, how about the following modification patterns?

Pattern 1:

main.ts

function main(): void {
  console.log("main");
  hello();
}

other.ts

function hello(): void {
  console.log("hello world");
}
  • By this modification, when main is run, console.log("main") in main function and console.log("hello world") in hello function are run with the script editor of Google Apps Script.

Pattern 2:

In this pattern, the Class object is used.

main.ts

import { Sample } from "./other";

function main(): void {
  console.log("main");
  new Sample().hello();
}

other.ts

export class Sample {
  public hello(): void {
    console.log("hello world");
  }
}
  • By this modification, when main is run, console.log("main") in main function and console.log("hello world") in hello function are run with the script editor of Google Apps Script.