yunabe / tslab

Interactive JavaScript and TypeScript programming with Jupyter
Apache License 2.0
701 stars 44 forks source link

Can not use named imported symbols #1

Closed yunabe closed 4 years ago

yunabe commented 4 years ago

How to reproduce

Expected

path.join("a", "b") is executed.

Actual An error like "Cannot read property 'join' of undefined"

Reasons

The first cell is converted into JS code like

const path_1 = require("path");,
exports.join = path_1.join;

and the second cell is converted into JS like:

path_1.join("a", "b");

But when the second converted cell is executed, path_1 is undefined because these two cells are executed as isolated modules.

Proposals

I think there are two solutions.

  1. Convert .ts file into ES2015 module JS. Then, execute it with Node vm module.
  2. Convert .ts file into ES2015 module JS. Types are checked in this phase. Then, convert the ES2015 module JS file into CommonJS module.

I decided to go with (2) becuase ES module support of vm module is still under experimental as of Sep, 2019.