Adds support for ESM `tailwind.config.js` files #3544
Draft
+37
−4
Conversation
| try { | ||
| return require(config) | ||
| } catch (e) { | ||
| if (e.code === 'ERR_REQUIRE_ESM') { |
JKrol
Feb 8, 2021
What if it is a different exception? Right now you will return null, but I think you should throw the original exception.
What if it is a different exception? Right now you will return null, but I think you should throw the original exception.
| if (e.code === 'ERR_REQUIRE_ESM') { | ||
| try { | ||
| return import(config).then(mdl => mdl.default) | ||
| } catch (e) {} |
JKrol
Feb 8, 2021
Similar to my previous comment, maybe just throw the original exception in here if import also failed, instead of an empty catch?
Similar to my previous comment, maybe just throw the original exception in here if import also failed, instead of an empty catch?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
ESM adoption in Node is becoming more common—it would be really great for Tailwind to get out ahead of a lot of the issues people are about to run into as the ecosystem shifts to support ESM.
Currently, users who rely on Node ESM with "type": "module" (officially supported since node@12.17.0) must use a
tailwind.config.cjsfile in CJS format (#3181) inside of their ESM project. This is great! But the best solution would be to allow ESM projects to use the normaltailwind.config.jsfile in ESM format, as requested in discussion #2284.I thought this would be a bigger problem, but it turned out to be fairly simple. The magic here is a utility function that attempts to
requirethe config path (will work for CJS files),catchany errors withcode: "ERR_REQUIRE_ESM", and thenimportthe config path instead (will work for ESM files). Note that this util function is split into a separate file and excluded from thebabelconfig, otherwisebabeltranspiles out the nativeimportstatement.Two issues I'd love some assistance on:
getModuleDependenciesand maybe others?