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 conditional exports for node.js #131

Merged
merged 2 commits into from
Jul 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@
"types": "dist/index.d.ts",
"main": "dist/haws.umd.js",
"module": "dist/index.js",
"exports": {
"node": {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have to make this exclusive to node?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to take the lightest touch solution, i.e., only modify for node.

"import": "./dist/index.js",
"require": "./dist/haws.cjs"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't we use the UMD build here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any compatible format would work. The file extension is the thing that needs to be different so node stops treating it as an ES module.

@balloob said you had a possible solution to use two package.json which I also did get to work but was unsure what was the best approach on the directory structure.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The exports would be best, we didn't go that route because it caused problems before, might be solved by now... Would be nice if we can find a changelog somewhere that fixed it.

I don't think the extension is needed anymore with exports, so we could just use dist/haws.umd.js.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The directory structure with a second package.json would be an issue indeed, as it would be a breaking change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, the error about it being an ES Module is thrown if you use ./dist/haws.umd.js in place of ./dist/haws.cjs

Copy link
Contributor Author

@zachowj zachowj Jul 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice if we can find a changelog somewhere that fixed it.

The previous error reported here can be replicated by using dist/haws.cjs instead of ./dist/haws.cjs

Error [ERR_INVALID_PACKAGE_TARGET]: Invalid "exports" main target "dist/haws.cjs" defined in the package config /node-red-contrib-home-assistant-websocket/node_modules/home-assistant-js-websocket\package.json; targets must start with "./"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, the error about it being an ES Module is thrown if you use ./dist/haws.umd.js in place of ./dist/haws.cjs

That makes no sense, require can only be CommonJS but ok... Node... :-/

}
},
"repository": {
"url": "https://github.com/home-assistant/home-assistant-js-websocket.git",
"type": "git"
},
"scripts": {
"watch": "tsc --watch",
"build": "tsc && rollup dist/index.js --format umd --name HAWS --file dist/haws.umd.js",
"build": "tsc && rollup -c",
"test": "tsc && mocha",
"prepublishOnly": "rm -rf dist && yarn build && npm test"
},
Expand Down
14 changes: 14 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export default {
input: "dist/index.js",
output: [
{
file: "dist/haws.cjs",
format: "cjs",
},
{
file: "dist/haws.umd.js",
format: "umd",
name: "HAWS",
},
],
};