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

Node esm loader support #1728

Closed
wants to merge 2 commits into from
Closed

Node esm loader support #1728

wants to merge 2 commits into from

Conversation

DylanPiercey
Copy link
Contributor

@DylanPiercey DylanPiercey commented Aug 7, 2021

Description

Resolves #1727

Adds support for loading Marko files directly as ESM via a node ESM Loader hook.

This means you can run node like

node --experimental-loader @marko/compiler/loader.mjs ./server.js

And importing Marko files will work as is.


Primarily this PR contains two main changes.

  1. Adds a loader.mjs file to the compiler package.
  2. Updating any imports added when compiling a Marko template to have the filename (including extension) to be more native esm friendly.

Checklist:

  • I have read the CONTRIBUTING document and have signed (or will sign) the CLA.
  • I have updated/added documentation affected by my changes.
  • I have added tests to cover my changes.

@codecov
Copy link

codecov bot commented Aug 7, 2021

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.29%. Comparing base (040179f) to head (97dc376).
Report is 1134 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1728      +/-   ##
==========================================
- Coverage   90.31%   90.29%   -0.03%     
==========================================
  Files         224      224              
  Lines        6828     6819       -9     
==========================================
- Hits         6167     6157      -10     
- Misses        661      662       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link

@eyelidlessness eyelidlessness left a comment

Choose a reason for hiding this comment

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

I'm just taking a peek at Marko internals/activity, spotted this PR, and wanted to give a heads up about this API change.

Comment on lines 18 to 22
export function getFormat(url, context, defaultGetFormat) {
return extensionRegex.test(url)
? format
: defaultGetFormat(url, context, defaultGetFormat);
}

Choose a reason for hiding this comment

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

Heads up that the loader hooks API has changed in LTS 16.12.0+ along with 17.x, which will presumably provide the initial API for 18.x. The change eliminates getFormat, moving its responsibility to the two remaining functions1:

  • resolve, as a hint
  • load, which can use that hint but has the final say

You'll likely want to call this in resolve (assuming you need to support 14.x), with the fallback deferring to the result from defaultResolve.

Footnotes

  1. besides globalPreload, which is intended for setup not the dynamic runtime

@tigt
Copy link
Contributor

tigt commented Jun 23, 2022

Marking as blocked because:

  • Node’s loaders are/were experimental
  • Using Marko without a bundler in general is something we don’t want to encourage anymore

@tigt tigt added the blocked Cannot be completed before another issue. The blocking issue should be linked. label Jun 23, 2022
@tigt
Copy link
Contributor

tigt commented Jun 23, 2022

Note this will still probably happen because sometimes computers are terrible, but likely with a disclaimer about using it (and the require() hook).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blocked Cannot be completed before another issue. The blocking issue should be linked.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Marko 5 ESM Loading/Registering
4 participants