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

fix: vendor hexoid until v2 is released #978

Closed
wants to merge 1 commit into from

Conversation

icholy
Copy link
Contributor

@icholy icholy commented Jul 30, 2024

Problem:

The v1.0.0 release of hexoid is incompatible with webpack.

main.js

require('formidable');

webpack.config.js

module.exports = {
	target: 'node',
	entry: './main.js',
	optimization: {
		minimize: false
	}
}

Output:

/home/icholy/src/playground/hexoid-webpack/dist/main.js:1381
const toHexoId = hexoid(25);
                 ^

TypeError: hexoid is not a function
    at 211 (/home/icholy/src/playground/hexoid-webpack/dist/main.js:1381:18)
    at __webpack_require__ (/home/icholy/src/playground/hexoid-webpack/dist/main.js:2121:41)
    at /home/icholy/src/playground/hexoid-webpack/dist/main.js:2158:1
    at Object.<anonymous> (/home/icholy/src/playground/hexoid-webpack/dist/main.js:2160:12)
    at Module._compile (node:internal/modules/cjs/loader:1376:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
    at Module.load (node:internal/modules/cjs/loader:1207:32)
    at Module._load (node:internal/modules/cjs/loader:1023:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12)
    at node:internal/main/run_main_module:28:49

Root Cause:

Webpack prefers the module entry over the main entry in package.json by default, but hexoid exports incompatible APIs between the commonjs & esm modules. The esm module uses a default export which is not equivalent to the module.exports assignment in the commonjs module.

Solution 1:

The hexoid repo has fixed the inconsistency in their master branch, but haven't published a release in 4 years. I've requested a v2 be published with the new exports. Once this happens, we can upgrade to that: lukeed/hexoid#7

Solution 2:

In the meantime, I propose that we vendor the hexoid code as a helper function.

@mobeigi
Copy link

mobeigi commented Oct 5, 2024

Would be a great addition, the hexoid issue is very annoying with webpack. @GrosSacASac thoughts?

As a current workaround I am using a local master build of hexoid via yarn pack:

  "resolutions": {
    "hexoid": "file:./packages/hexoid-v1.0.0-master.tgz"
  }

And then patching formidable with patch-package to import it correctly:

diff --git a/node_modules/formidable/src/Formidable.js b/node_modules/formidable/src/Formidable.js
index 0542700..72191fa 100644
--- a/node_modules/formidable/src/Formidable.js
+++ b/node_modules/formidable/src/Formidable.js
@@ -5,7 +5,7 @@

const os = require('os');
const path = require('path');
-const hexoid = require('hexoid');
+const { hexoid } = require('hexoid');
const once = require('once');
const dezalgo = require('dezalgo');
const { EventEmitter } = require('events');

Works but not ideal :)

@icholy
Copy link
Contributor Author

icholy commented Oct 5, 2024

@mobeigi there's a simpler work-around:

module.exports = {
	// ...
	resolve: {
		alias: {
			hexoid: path.resolve(__dirname, 'node_modules/hexoid/dist/index.js'),
		},
	}
};

@mobeigi
Copy link

mobeigi commented Oct 5, 2024

@icholy That approach gives me build warnings when doing a production next.js build (although I am using latest canary builds etc). The patch approach does not. Ideally though your PR is merged in and this solves the problem easily for everyone. 🙏

@icholy
Copy link
Contributor Author

icholy commented Oct 12, 2024

lukeed/hexoid#7 v2 of hexoid has been released.

@icholy
Copy link
Contributor Author

icholy commented Oct 12, 2024

Closing in favor of #981

@icholy icholy closed this Oct 12, 2024
@icholy icholy mentioned this pull request Oct 12, 2024
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

Successfully merging this pull request may close these issues.

2 participants