vscode-httpyac icon indicating copy to clipboard operation
vscode-httpyac copied to clipboard

Cannot require a customized js file

Open slackwareer opened this issue 1 year ago • 6 comments

A js file which exports some functions is required by a http script, however the requiring result is only an empty object.

JS file: test.js

const TestFunc = () => {
  console.log("test");
};

exports.TestFunc = TestFunc

test.http file

### Test Common Functions
{{
    const test = require('./test.js')
    console.log(test);
}}

Guess what is printed... It turns out to be just an empty object. image

So how can I write a js file which can be required by the http file?

slackwareer avatar May 14 '24 02:05 slackwareer

### Test Common Functions 
{{ 
const test = require('./test.js')

console.log(test.TestFunc()); 
}}

You need to call your method

See https://github.com/httpyac/httpyac.github.io/blob/main/examples/script/scriptRequire.http

AnWeber avatar May 14 '24 02:05 AnWeber

### Test Common Functions 
{{ 
const test = require('./test.js')

console.log(test.TestFunc()); 
}}

You need to call your method

I called the method with following codes:

### Test Common Functions
{{
    const test = require('./test.js')
    test.TestFunc()
}}

Unfortunately I got nothing output...

slackwareer avatar May 14 '24 02:05 slackwareer

It works, but not the way you expect it to. It works directly on the command line. In vscode it would also work, but the console output is not the output channel but really stdout.

works

For scripts that I execute directly, I overwrite the console output so that it appears in the OutputChannel. For require scripts I have not yet found a trick for this, so it writes directly to stdout. Pass the function the reference to the console then it would work.

### Test Common Functions 
{{ 
const test = require('./test.js')

console.log(test.TestFunc(console)); 
}}
const TestFunc = (c) => {
  c.log("test");
};

exports.TestFunc = TestFunc

AnWeber avatar May 14 '24 17:05 AnWeber

It works, but not the way you expect it to. It works directly on the command line. In vscode it would also work, but the console output is not the output channel but really stdout.

works works

For scripts that I execute directly, I overwrite the console output so that it appears in the OutputChannel. For require scripts I have not yet found a trick for this, so it writes directly to stdout. Pass the function the reference to the console then it would work.

### Test Common Functions 
{{ 
const test = require('./test.js')

console.log(test.TestFunc(console)); 
}}
const TestFunc = (c) => {
  c.log("test");
};

exports.TestFunc = TestFunc

Yeah ,passing the outer console object to the required function works, it print to OutputChannel and the log can be read in the httpyac Console.

And if there is a way to overwrite the global console.log method or overwrite the origin required script to override the console.log method in the script ? Or maybe it's a problem that if it's worthy to do such stuff ......

slackwareer avatar May 15 '24 03:05 slackwareer

And if there is a way to overwrite the global console.log method or overwrite the origin required script to override the console.log method in the script

PR is welcome. I've been looking into NodeJS vm for a while, but haven't found it. If you have an idea, please feel free.

AnWeber avatar May 15 '24 03:05 AnWeber

@AnWeber Quote of the day: "It works, but not the way you expect it to." :-)

alekdavisintel avatar May 15 '24 16:05 alekdavisintel