Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot require a customized js file #285

Open
slackwareer opened this issue May 14, 2024 · 6 comments
Open

Cannot require a customized js file #285

slackwareer opened this issue May 14, 2024 · 6 comments

Comments

@slackwareer
Copy link

slackwareer commented May 14, 2024

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?

@AnWeber
Copy link
Owner

AnWeber commented May 14, 2024

### 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

@slackwareer
Copy link
Author

### 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...

@AnWeber
Copy link
Owner

AnWeber commented May 14, 2024

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

@slackwareer
Copy link
Author

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 ......

@AnWeber
Copy link
Owner

AnWeber commented May 15, 2024

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.

@alekdavisintel
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants