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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

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

Merging #1728 (97dc376) into main (040179f) will decrease coverage by 0.02%.
The diff coverage is n/a.

@@            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     
Impacted Files Coverage Δ
packages/compiler/src/index.js 75.67% <0.00%> (-5.96%) ⬇️
packages/translator-default/src/index.js 92.91% <0.00%> (-1.58%) ⬇️
packages/compiler/src/babel-plugin/parser.js 96.96% <0.00%> (-0.76%) ⬇️
packages/compiler/src/config.js 55.17% <0.00%> (ø)
packages/compiler/src/taglib/index.js 97.05% <0.00%> (ø)
...s/marko/src/runtime/components/event-delegation.js 95.45% <0.00%> (+0.21%) ⬆️
...ges/compiler/src/taglib/loader/loadTagFromProps.js 85.63% <0.00%> (+0.57%) ⬆️
.../compiler/src/taglib/loader/loadTaglibFromProps.js 88.23% <0.00%> (+0.73%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 040179f...97dc376. Read the comment docs.

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