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

Returning results from a promise within a shortcode #1434

Closed
noelforte opened this issue Oct 4, 2020 · 7 comments
Closed

Returning results from a promise within a shortcode #1434

noelforte opened this issue Oct 4, 2020 · 7 comments

Comments

@noelforte
Copy link

I'm trying to figure out what the best way is to return the results of an async function to be used as a shortcode. I've come across a few issues on this repository that suggest various ways to do this, but none that appear to match my case exactly— it's probably because of my lack of experience trying to wrap my head around promises, but alas here's what I'm trying to accomplish.

I'm using eleventy in combination with babel and terser to process my javascript files by:

a) using a shortcode that takes the path of a .js file
b) passing that path to a required module
c) the module exports a function that returns the transpiled + minified result

Example:

<module imports ...>

module.exports = async filepath => {
  const babelResult = await babel.transformFileAsync(filepath, {
      presets: [["@babel/preset-env"]],
  });
  const terserReuslt = await minify(babelResult.code);
  return terserResult.code;
};

However, when I run eleventy the resulting output from the shortcode is always [object Promise] even though I've awaited the promise to be resolved. What's puzzling is that doing a console.log(terserResult.code); yields the expected code. Am I missing something?

Some brief googling took me to this page on Eleventy Shortcodes, and further exploration took me to the deasync package on npm. If possible I'd like to run eleventy without extra dependencies, as well as without needing to rely on specific templates— I'd like my shortcodes to stay as universal as possible for flexibility.

@noelforte
Copy link
Author

An update on this: @louh commented over in the terser repo (terser/terser#801 (comment)) about the importance of synchronous minification within templating languages and synchronous code that is core to Eleventy. In the meantime, going to continue exploring the best way to deal with async operations within shortcodes, or find a way to minify my scripts synchronously.

@binyamin
Copy link
Member

binyamin commented Oct 9, 2020

Have you seen https://www.11ty.dev/docs/quicktips/inline-js/?

@noelforte
Copy link
Author

@binyamin, I have. The caveat with that setup is that Nunjucks is the only templating language that supports async operations, so not the most viable solution. I ended up going back to using babel-minify for the time being as I have the option to do sync stuff with babel, and can cut a few dependencies out of my project. I'll leave this issue open for a bit in case people want to discuss more.

@Joel-Torres
Copy link

Joel-Torres commented Oct 9, 2020 via email

@binyamin
Copy link
Member

binyamin commented Oct 9, 2020

@Joel-Torres This is not an email list; it's an issue on GitHub. It looks like GitHub is sending notifications to your email. Log into GitHub, and unsubscribe from whatever you want.

@noelforte
Copy link
Author

Going to mark this as closed since my issue was addressed and this appears to now be drifting off topic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants