-
Notifications
You must be signed in to change notification settings - Fork 257
Switch to ESM modules #6603
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
Switch to ESM modules #6603
Conversation
1f04210
to
2744031
Compare
1569572
to
3d673b1
Compare
aad6218
to
2bd04f8
Compare
2bd04f8
to
165573c
Compare
165573c
to
7503cce
Compare
@@ -0,0 +1,6 @@ | |||
audience: developers | |||
level: major |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
although clients are mostly untouched, and are still CJS, worth making a major
@@ -23,3 +26,17 @@ exports.withRestoredEnvVars = () => { | |||
} | |||
}); | |||
}; | |||
|
|||
const ROOT_DIR = path.resolve(__dirname, '../../..'); | |||
const suiteName = () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is copied from lib/testing to avoid having to implicitly depend on package from outside.
clients/client/test/retry_test.js
Outdated
message: 'Internal error of sorts', | ||
}); | ||
}); | ||
// suite(testing.suiteName(), function() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as this whole test depends on "internal" libraries, it shouldn't be here.
figuring out where to put it
@@ -0,0 +1,35 @@ | |||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this was added to keep clients/client
clean, and separate tests that used internal libraries from the client itself.
698de97
to
960d829
Compare
To reduce footprint of this refactoring let's keep helper as it was before. Helper.js defines and exports a `helper` object the same way that it was doing before, but instead of `exports` it is using `helper` object. This allows to tests remain untouched (besides imports)
Import type assertions are still experimental and they disappoint eslint Reverting to loading files with fs.readFile instead eslint/eslint#15305
node client was using implicit dependencies, which makes it hard to keep CJS because those dependencies are ESM already. Some of those dependencies can be easily rewritten, but `retry_test.js` heavily relies on the application logic, so doesn't belong there.
Retry logic tests from node client relied on implicit internal dependencies that broken CJS/ESM interop. clients/client remain CJS and can be used in both CJS and ESM packages. Therefore we extract those tests into separate package
cleaned up test-coverage.js
befe0a4
to
63cf97b
Compare
closed in favor of #6627 |
Motivation
In node.js ESM (
"type": "module"
) package can import both CommonJS packages, and ESM packages. However, commonjs package cannot import newer ESM packages.As more and more dependencies switch to ESM-only distribution, making upgrades (especially security upgrades) become hard.
Taskcluster platform consists of multiple utility (library) packages, services themselves, and clients.
Library packages:
db
,api
,app
,config
,iterate
,loader
,monitor
,postgres
,pulse
,references
,testing
,validate
Services:
auth
,built-in-workers
,github
,hooks
,index
,notify
,object
,purge-cache
,queue
,secrets
,web-server
,worker-manager
Clients that are published:
taskcluster-client
,taskcluster-client-web
.It is not possible to only convert services packages to ESM, as they depend on library packages, which also consume 3rd-party dependencies.
Clients, however, do not depend on those, so could possibly remain in CommonJS format to avoid shipping both ESM/CJS builds.
taskcluster-client
is a published standalone package, but uses unlisted dependencies for testing:taskcluster-lib-*
,taskcluster-testing
. Those are not listed in package dependencies, as those packages are not installed, but are internal.This was refactored, and implicit dependencies were removed, and tests that depends on those were moved into separate
client/client-test
folderimporting
ESM requires that files would be imported with extensions.
For the same reason (I believe) running
node services/auth/src/main server
will not work, butnode services/auth/src/main.js server
will work__dirname
,__filename
Can now be used with the help of
import.meta.url
:module.parent
no longer availableNot possible to do:
This will work:
Testing
Since ESM require exports to be statically analyzable, test helper functions are being rewritten:
instead of
exports.xx
and dynamic injections of helper functions,helper.js
now exposes singlehelper = {}
object that is being modified by utility functions likewithDb()
,withPulse()
Exports
Taskcluster internal libraries expose everything as named export and as default to allow mixed usage:
Can be used both ways:
Import assertions
Node.js allows to specify import assertions since v17:
However, it is still experimental and comes with:
Also eslint doesn't implement it yet, waiting for it to be analysed.
Alternatives:
Files were migrated in dumb way
npx cjs-to-es6 path/to/files
and does not result in a working code, there are still many steps to follow:TODO
db/
infrastructure/tooling
libraries/api
libraries/app
libraries/config
libraries/iterate
libraries/loader
libraries/monitor
libraries/postgres
libraries/pulse
libraries/references
libraries/testing
libraries/validate
services/auth
services/built-in-workers
services/github
services/hooks
services/index
services/notify
services/object
services/purge-cache
services/queue
services/secrets
services/web-server
services/worker-manager
Fixes #4260