Skip to content

Fix importing from ES module - #25

Closed
Rafał Chłodnicki (rchl) wants to merge 5 commits into
microsoft:mainfrom
rchl:fix/es-import
Closed

Fix importing from ES module#25
Rafał Chłodnicki (rchl) wants to merge 5 commits into
microsoft:mainfrom
rchl:fix/es-import

Conversation

@rchl

@rchl Rafał Chłodnicki (rchl) commented Jul 25, 2022

Copy link
Copy Markdown

This should fix the case when consumer of this package uses type: 'module' in package.json.

The issue with that was that in that case Node.js would resolve the import to the UMD bundle and then importing with import { URI } from 'vscode-uri' would not work. Only a hacky way like import vscodeUri from 'vscode-uri'; vscodeUri.URI(...); would work.

I can make an example repo to demonstrate the issue if you want.

(Note that the module field in package.json is ignored by Node.js so that's why I've added exports. The module field is only supported by bundlers like Webpack.)

BTW. Please sort out this repo first -- main is lagging behind as there is a 3.0.3 tag but changes from it are not merged to it.

(And this repo could have issues enabled as there are some type of issues like this one that don't belong in vscode repo itself.)

@rchl

Copy link
Copy Markdown
Author

Dirk Bäumer (@dbaeumer) any comments? :)

@dbaeumer

Copy link
Copy Markdown
Member

That's more Johannes Rieken (@jrieken)

Comment thread package.json Outdated
@rchl

Rafał Chłodnicki (rchl) commented Jan 26, 2023

Copy link
Copy Markdown
Author

Any interest in getting this?

It will likely be a breaking change for consumers with type: module set so might require major version to be fully proper.

@jrieken

Copy link
Copy Markdown
Contributor

Rafał Chłodnicki (@rchl) VS Code extension cannot really use ESM (yet) so I wonder where/how you would use this?

@remcohaszing

Copy link
Copy Markdown
Contributor

Some language servers / services use it. Also VSCode extensions are often bundled using esbuild. In this case CJS will result in boilerplate code in the bundle to provide CJS interoperability. In most cases this could be replaced with WHATWG URL though.

I think the question shouldn’t be where / how this would be used though. This package already contains ESM. If an npm package is published containing ESM, IMO it should contain real ESM, not faux. That’s what this PR is about.

@rchl

Rafał Chłodnicki (rchl) commented Jan 27, 2023

Copy link
Copy Markdown
Author

I guess I'll reiterate most of the points that Remco Haszing (@remcohaszing) made already but I'm using it in https://github.com/typescript-language-server/typescript-language-server/ and as Remco Haszing (@remcohaszing) pointed out, this package is already set up with ES bundle (the "module": "./lib/esm/index.js" property in package.json). I'm just adding extra properties so that the ES bundle also works correctly with native Node ES modules support rather than only with bundlers which tend to be more forgiving when it comes to handing incorrect exports.

@rchl

Copy link
Copy Markdown
Author

And to clarify more: this change doesn't make it a "ES modules only" package which would be the case if it had a "type": "module" property set in package.json. It just makes it compatible with consumers of this package that use "type": "module".

@Jason3S

Copy link
Copy Markdown

Johannes Rieken (@jrieken),

Any progress here?

The bug is that package.json contains "module": "./lib/esm/index.js",. Removing that line would allow it to work when used with a .mjs files.

Remco Haszing (remcohaszing) added a commit to remcohaszing/vscode-uri that referenced this pull request Aug 20, 2023
Previously only a umd main entrypoint. There was also the non-standard
`module` field in `package.json`, which causes issues with various
tools. Now the standard `package.json` field `exports` is used to
support both native ESM and CJS.

Additionally, the webpack build has been replaced with `tsc`. Instead of
bundling `path-browserify`, an import condition is used to select either
`path` or `path-browserify`. This means that browser users use
`path-browserify`, regardless of whether they use ESM or CJS, whereas
others use `path`. Also this means users can deduplicate
`path-browserify` if they use it elsewhere in their code base.

Closes microsoft#25
Closes microsoft#33
Closes microsoft#37
Remco Haszing (remcohaszing) added a commit to remcohaszing/vscode-uri that referenced this pull request Aug 20, 2023
Previously only a umd main entrypoint. There was also the non-standard
`module` field in `package.json`, which causes issues with various
tools. Now the standard `package.json` field `exports` is used to
support both native ESM and CJS.

Additionally, the webpack build has been replaced with `tsc`. Instead of
bundling `path-browserify`, an import condition is used to select either
`path` or `path-browserify`. This means that browser users use
`path-browserify`, regardless of whether they use ESM or CJS, whereas
others use `path`. Also this means users can deduplicate
`path-browserify` if they use it elsewhere in their code base.

Closes microsoft#25
Closes microsoft#33
Closes microsoft#37
@aeschli

Copy link
Copy Markdown
Contributor

Fixed by #39

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.

6 participants