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

Support ES Modules and async resolution for engine script #521

Merged
merged 11 commits into from
Apr 30, 2023
Merged

Conversation

yhatt
Copy link
Member

@yhatt yhatt commented Apr 22, 2023

--engine option now supports using ES Modules and async function engine.

ESM

// engine.mjs
import { marpPlugin } from 'marp-plugin-foobar';
export default ({ marp }) => marp.use(marpPlugin);
marp --engine engine.mjs markdown.md

Async function

with ES Modules

// engine.mjs
export default async ({ marp }) => {
  const { marpPlugin } = await import('marp-plugin-foobar');
  return marp.use(marpPlugin);
}
marp --engine engine.mjs markdown.md

with CommonJS

// engine.cjs
module.exports = async ({ marp }) => {
  const { marpPlugin } = await import('marp-plugin-foobar');
  return marp.use(marpPlugin);
}
marp --engine engine.cjs markdown.md

Backward compatibillity

In the most cases, a formerly used engine.js with CommonJS should keep working.

// engine.js
module.exports = ({ marp }) => marp.use(require('marp-plugin-foobar').marpPlugin)
marp --engine engine.js markdown.md

However, if a Node project that Marp CLI was installed has been marked as type: module in package.json, users may have to change the engine file extension from .js to .cjs for making clear of the intent of using CommonJS.

Notes

@yhatt yhatt merged commit b482388 into main Apr 30, 2023
@yhatt yhatt deleted the esm-engine branch April 30, 2023 02:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant