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

Use a Node module in the prelude #45

Open
thorn0 opened this issue Sep 20, 2015 · 10 comments
Open

Use a Node module in the prelude #45

thorn0 opened this issue Sep 20, 2015 · 10 comments

Comments

@thorn0
Copy link
Contributor

thorn0 commented Sep 20, 2015

I'd like to load a Node module in the prelude, but things like require, global, etc. don't exist there. Would be great to have this possibility. (Currently, I'm concatenating files to create a working prelude).

@thorn0 thorn0 changed the title I'd like to use (require) a Node module in the prelude Use a Node module in the prelude Sep 20, 2015
@bterlson
Copy link
Member

The difficulty here is having require do the right thing inside a new VM context since you can't just pass in the parent context's require function...

@thorn0
Copy link
Contributor Author

thorn0 commented Sep 20, 2015

The problem is that the parent context's require would look for modules at the wrong path, right?

@bterlson
Copy link
Member

Possibly. I assume that if the module modified globals it would modify the parent realm too.

@ljharb
Copy link
Member

ljharb commented Sep 25, 2017

ping @bterlson - this is the major barrier to being able to use test262-harness inside a polyfill/shim module as part of normal tests.

@rwaldron
Copy link
Contributor

@ljharb do you have any thoughts/suggestions for how to address the issues discussed above?

@ljharb
Copy link
Member

ljharb commented Sep 25, 2017

No, I'm not sure; but i believe you could use something like https://www.npmjs.com/package/require-for to pass a require into runInNewContext that was scoped specifically to the dirname of the prelude file?

@jugglinmike
Copy link
Contributor

@thorn0 @ljharb Would it be possible for you folks to use a tool like Webpack to bundle your polyfills into a string that could be injected via the prelude option? And in your experience, do you think this approach would generalize for any polyfill?

I'm asking this not (just) because I'm lazy but because that approach is host-independent. Polyfill authors could use it to vet their libraries across hosts--not just Node.js.

@ljharb
Copy link
Member

ljharb commented Oct 7, 2017

It might be possible but it would be inordinately complex, and the goal is to make using the harness easy.

If the hostType is node, I would assume I could access node features properly in the prelude.

@astlaurent1
Copy link

I was attempting to run a test against the promise.allsettled shim and ran into this issue. require was defined, but it could not resolve the module

I went with @jugglinmike's proposed solution of using Webpack to bundle what I needed into a prelude file. The webpack config and bundled file is here: astlaurent1/Promise.allSettled@631acd6. I suppose I could have done that in my fork of test262

After doing all that, in hindsight, it would seem relatively straightforward to support something like --webpack-prelude <prelude-entry.js>, where <prelude-entry.js> imports whatever modules it needs to and runs necessary bootstrapping code. In this case, the <prelude-entry.js> imports the shim, then calls the .shim() method to make Promise.allSettled available in the context of the test drivers

Pre-packaging the prelude like this also serves the added benefit of not "polluting" test cases with module/import semantics. The bundling provides a nice boundary between what you're preparing and what you're testing

(P.S. See tc39/test262#2112 for discussions on testing Promise.allSettled)

@ptomato
Copy link

ptomato commented Apr 22, 2020

Would it be possible for you folks to use a tool like Webpack to bundle your polyfills into a string that could be injected via the prelude option? And in your experience, do you think this approach would generalize for any polyfill?

This is what we're doing for the polyfill in proposal-temporal. It works, although it makes test runs painfully slow (I suspect) because the prelude has to be parsed and executed for every test file.

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

7 participants