rvagg / polendina

Non-UI browser testing for JavaScript libraries from the command-line
Other
63 stars 6 forks source link

Allow package self reference? #8

Closed ctavan closed 4 years ago

ctavan commented 4 years ago

While working on https://github.com/multiformats/js-multiformats/pull/22 I realized that package self reference, which works well in Node.js, does not work with polendina yet.

Let's have a look at an example. So I have a module named multiformats which I want to test. I use package self reference to leverage pkg.exports like this:

import { coerce } from 'multiformats/bytes.js'

When running this code in a polendina test case, the underlying webpack will try to resolve that file in the following locations:

but what I really want (and what Node.js' resolution algorithm does) is ./multiformats/bytes.js.

I think the only way to change this behavior in webpack would be to define a resolve.alias?!

How about automatically setting up an alias for the module that is being tested?

rvagg commented 4 years ago

Yeah, I didn't even know self-reference was a thing tbh until Mikeal started doing this in js-multiformats. Would you mind making something work using --webpack-config to supply Polendina a config that will get merged in with the default. As a proof-of-concept that should demonstrate it working and we can consider whether to pull it into here as the default or not.

ctavan commented 4 years ago

@rvagg thanks for the hint, that worked well: https://github.com/multiformats/js-multiformats/pull/22/commits/ecc4541ca370796ddc86df70a79fe2765adb40f7 🎉

rvagg commented 4 years ago

ok, found the details of --experimental-resolve-self that was in 13.x and got unflagged in 14.x (and backported perhaps?)

@ctavan do you want to prepare a PR here to add it to the global config? It should probably happen here if this is possible in Node now.

ctavan commented 4 years ago

@rvagg I'm not entirely sure what you mean. I think the issue I was observing was with webpacks resolution algorithm. Where does Node.js' --experimental-resolve-self come into play?

rvagg commented 4 years ago

Where does Node.js' --experimental-resolve-self come into play?

That's just me being educated on the source of this self-reference superpower. It's not relevant here as it seems to be on by default now in the recent versions of Node.js. A change here would basically mean incorporating what you did for js-multiformats but making it generic for the package being tested.

ctavan commented 4 years ago

@rvagg I think this is already supported in webpack@5.

Here's what I tried using an example.js file in the js-multiformats repo with the following content:

import sha2 from 'multiformats/hashes/sha2.js';

Fails with webpack@4:

./node_modules/.bin/webpack --target web --entry ./example.js --output example.dist.js --mode production
Hash: b90546ef64707b10cd66
Version: webpack 4.43.0
Time: 278ms
Built at: 06/26/2020 2:02:04 PM
 1 asset
Entrypoint null = example.dist.js
[0] ./example.js 48 bytes {0} [built]

ERROR in ./example.js
Module not found: Error: Can't resolve 'multiformats/hashes/sha2.js' in '/Users/ct/cp/github/js-multiformats'
 @ ./example.js 1:0-47

Works with webpack@5:

./node_modules/.bin/webpack --target web --entry ./example.js --output example.dist.js --mode production
Hash: 66e4d57d71fedc65b5e1
Version: webpack 5.0.0-beta.18
Time: 391 ms
Built at: 2020-06-26 14:03:29
          Asset       Size
example.dist.js  209 bytes  [emitted]  [name: null]
Entrypoint null = example.dist.js
./example.js + 1 modules 358 bytes [built]

So maybe, since this is a pkg.exports-related feature in Node.js which will only be supported in webpack@5 (https://github.com/webpack/webpack/pull/10953) we'd rather wait for webpack@5 final?

rvagg commented 4 years ago

yeah ok, I guess I'd better start playing with webpack 5 to get ready, at least we have a workaround for now