use import wherever possible to load code #2334
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
🤔 What's changed?
Previously our means of supporting ESM configuration files, formatters and snippet syntaxes was just to attempt a
require()
and then fall back toawait import()
in a catch block if it failed. This was a temporary measure, which this PR replaces with something more permanent.Due the different structures of ESM vs CommonJS when imported, this has a little complexity. For formatters and snippet syntaxes, we can switch to
await import()
and just walk a couple of levels down the object looking for adefault
key that is a function.For config, the logic above would be too fuzzy (profile keys of "default" etc), so we use
await import()
orrequire()
as appropriate based on the file extension. For.js
files we check the nearestpackage.json
for the type and take that hint on whether to userequire()
orawait import()
. Also, throws if the extension isn't one of our supported ones, and adds some debug logging.Also, we've been able to remove the hacky
importer.js
mechanism, which was added to get around TypeScript's behaviour (at the time) of transpiling allawait import()
statements down to Promise-wrappedrequire()
s - the module modenode16
now solves this.This will be a breaking change for any users who were relying on our internal usage of
require()
to dynamically transpile configuration files, formatters or snippet syntaxes (i.e. by registering a transpiler first). The suggested change will be to transpile beforehand and load the output file.⚡️ What's your motivation?
#2059:
🏷️ What kind of change is this?
📋 Checklist:
This text was originally generated from a template, then edited by hand. You can modify the template here.