2

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)]
        )
    )]
)

1 Answer 1

1

The reason why the following code gets emitted to es5...

import module from './module'
module()

...but this code doesn't...

import module from './module'

...is because while emitting, the compiler will remove any import declarations whose identifiers aren't used in an expression (try out that code with tsc and you will see this behaviour).

See "Why are imports being elided in my emit?"

Solution

The solution is to either use one of the imported identifiers in an expression or to inject an import declaration that only imports the module for the side effects:

// import "./module";
let importInject = ts.createImportDeclaration(
    undefined,
    undefined, 
    undefined,
    ts.createLiteral(moduleName));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.