I am trying to inject a module
import statement by transformer.ts
(* I used ttypescript and it works,
But some statements are uninterpreted.)
and I succeeded in inserting the import statement
but the import statement that I injected
was not translated intermittently into the es5 grammar.
This is the code that I used in transfomer.
transformer.ts
let moduleName = 'infinite.js'
let variableName = ts.createUniqueName('infinite')
ctx.hoistVariableDeclaration('infinite')
// Here are just a few key codes summarised.
let importInject = ts.createImportDeclaration(
undefined,
undefined,
ts.createImportClause(variableName, undefined),
ts.createLiteral(moduleName))
sourceFile = ts.updateSourceFileNode(sourceFile, ts.createNodeArray([
importInject,
...sourceFile.statements
]))
source.js
// Nothing before it.
source.js (injected import statement)
import infinite_1 from"infinite.js"
Code injection worked normally,
Although the compiler target option that defined es5,
But the code that I inserted is having a problem that doesn't translated.
However, if I add an import statement that
has nothing to do with the module import code
I will inject to the source code before module injection,
the translation of the code I inserted is also successful.
source.js
import module from './module'
module()
source.js (injected import statement)
"use strict";
exports.__esModule = true;
var infinite_js_1 = require("infinite.js");
var module_1 = require("./module");
module_1["default"]();
Hmm... is this compiling option problem...?
If so, which compiling options should be modified?
I don't know which code to modify... (x_x)
Have anyone ever had a problem like this?
If you know the solution, please let me know.
Additionally, I am currently using on a temporary solution as below.
let importInject =
ts.createVariableStatement(
sourceFile.modifiers,
[ts.createVariableDeclaration(
variableName,
undefined, // Type
ts.createCall(
ts.createIdentifier('require'),
undefined,
[ts.createLiteral(moduleName)]
)
)]
)