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

Set type: "module" for esm/ and test/ in all packages & update to mocha@9 #567

Merged
merged 5 commits into from
Jul 22, 2021

Conversation

eemeli
Copy link
Member

@eemeli eemeli commented Jul 21, 2021

While the top-level package.json files include a "type": "commonjs" specification, the esm/ directories can (and ought) to either have their contents use the .mjs extension, or include their own package.json file with the correct "type".

This adds the package.json files while keeping the extensions as .js, as this is significantly easier for the TypeScript interop; proper .mjs support is currently an unresolved issue.

For context, I noticed/encountered this issue while updating Mocha to its latest version; I'll file another PR on that separately if/once this is merged.

Edit: Mocha update is now included in this PR, as the old version wasn't able to handle the "module" types.

While the top-level package.json files include a "type": "commonjs"
specification, the esm/ directories can (and ought) to either have
their contents use the .mjs extension, or include their own
package.json file with the correct "type".

This adds the package.json while keeping the extensions as .js, as
this is significantly easier for the TypeScript interop; proper .mjs
support is currently an unresolved issue:
microsoft/TypeScript#18442
@eemeli eemeli requested a review from gregtatum July 21, 2021 14:27
@eemeli
Copy link
Member Author

eemeli commented Jul 21, 2021

Bother, it looks like the Mocha update needs to be done simultaneously. I'll include that here & rephrase the description.

While extensionless imports are supported by TypeScript as well as
the esm package, the extensions are required by Node.js when
resolving imports. TypeScript's tsc is also completely fine with
using .js extensions when referring to .ts files.
Copy link
Member

@gregtatum gregtatum left a comment

Choose a reason for hiding this comment

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

Setting as "Request changes" until the mocha issue is resolved. Please re-request review from me when you are ready.

The consideration of test/ files as ES modules requires a few of them
to use import.meta, which isn't supported by the default parser of
eslint@6. So this is a minimal patch to get linting to continue
working until ESLint itself is updated.
@eemeli eemeli changed the title Add an esm/package.json to all packages with { "type": "module" } Set type: "module" for esm/ and test/ in all packages & update to mocha@9 Jul 22, 2021
@eemeli eemeli requested a review from gregtatum July 22, 2021 10:29
@eemeli
Copy link
Member Author

eemeli commented Jul 22, 2021

Tooling updates are fun. Also needed to patch the ESLint configs, which to be clear are explicitly using babel-eslint rather than @babel/eslint-parser, as the latter requires a newer ESLint version.

Each of the commits should explain themselves. I'll submit the actual ESLint update separately, as that looks like it'll require more changes to account for changed recommendations.

Comment on lines +5 to +9
export * from "./ast.js";
export * from "./errors.js";
export * from "./parser.js";
export * from "./serializer.js";
export * from "./visitor.js";
Copy link
Contributor

Choose a reason for hiding this comment

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

Why does it import .js files from .ts file?

Copy link
Member Author

Choose a reason for hiding this comment

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

Because this is the cleanest way of producing valid ES module code in the esm/ directories. Node.js resolution requires the .js extension for import calls, and TypeScript automatically resolves e.g. ./ast.js as ./ast.ts.

Without these, we'd need a separate build step after the tsc call that would modify all of the relative paths to use fully qualified extensions. It'd be nice if TypeScript supported that natively, but, well, it doesn't. This is also why we need the esm/package.json files, as TypeScript only handles the .js extension like this, and not e.g. .mjs.

Copy link
Member

@gregtatum gregtatum left a comment

Choose a reason for hiding this comment

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

The changes all look reasonable to me, and CI is passing, so I'm happy to give my approval. Thanks for the maintenance work! I left one optional suggestion, and a few minor comments.

import {FluentBundle} from '../esm/bundle';
import {FluentResource} from '../esm/resource';
import {FluentType, FluentNumber, FluentDateTime} from '../esm/types';
import {FluentBundle} from '../esm/bundle.js';
Copy link
Member

Choose a reason for hiding this comment

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

Question: If someone regresses this by changing it back to '../esm/bundle' will our CI catch it?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, the Mocha tests will fail then. The exact error they fail with is a bit misleading though:

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /Users/eemeli/code/fluent-js/fluent-bundle/test/arguments_test.js
require() of ES modules is not supported.
require() of /Users/eemeli/code/fluent-js/fluent-bundle/test/arguments_test.js from /Users/eemeli/code/fluent-js/node_modules/mocha/lib/esm-utils.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename arguments_test.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /Users/eemeli/code/fluent-js/fluent-bundle/test/package.json.

There's a try { ... } catch block around the import() here which falls back to require(), and logs its error if both fail. I may end up filing an upstream bug about this.

"nyc": "^15.1.0",
"prettyjson": "^1.2.1",
"rollup": "^1.9.3",
"typedoc": "^0.20.34",
"typescript": "^4.2.3"
},
"engines": {
"node": ">=10.0.0"
"node": ">=12.0.0"
Copy link
Member

Choose a reason for hiding this comment

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

Thought: I double checked that 12 is the current LTS, so this change looks good to me.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, this is a result of the changes in #557 which weren't completely reflected in the lockfiles.

@@ -1,4 +1,5 @@
{
"parser": "babel-eslint",
Copy link
Member

Choose a reason for hiding this comment

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

Suggestion (optional): It would be nice to document why this parser is needed in case it can be removed, however this is a JSON configuration file which doesn't support comments. I'm not sure if it's feasible to convert it to a .js configuration file, so I'm marking this suggestion as optional.

Copy link
Member

Choose a reason for hiding this comment

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

Ah, I believe this comment is outdated by your next PR which converts it to .yml.

Copy link
Member Author

Choose a reason for hiding this comment

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

This dependency gets updated to @babel/eslint-parser by #568. That PR also ports the ESLint configs to YAML in order to allow them to be later commented.

@eemeli eemeli merged commit d874a75 into projectfluent:master Jul 22, 2021
@eemeli eemeli deleted the add-esm-packages branch July 22, 2021 19:45
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.

None yet

3 participants