Eleventy: Trigger builds via JavaScript API without the cli

Created on 23 Jul 2019  路  14Comments  路  Source: 11ty/eleventy

Is it possible to trigger a build via JavaScript without using the cli _(i.e. npx eleventy)_?

I've wrapped Eleventy in an Electron application, I would like to trigger new builds on button click, after updating data via form entry--but I'm not sure how to trigger builds via API without running the cli.

enhancement needs-votes

Most helpful comment

If you do build a normal API, it would be really nice if 11ty could produce a stream that can be consumed by Gulp. As it stands, one must built to a temp directory first....

All 14 comments

Have you looked at cmd.js?

Have you looked at cmd.js?

I hadn't looked at it yet, but I think you're right that it could potentially have what I need. Thanks! If I'm able to trigger builds with some of that code, I'll post an update.

I came here to request this feature so that I can use the step-through debugger.

Have you looked at cmd.js?

This isn't a very ergonomic solution, as I want to be able to just require() the package and be done. Would a pull request for this functionality be welcome?

This isn't a very ergonomic solution, as I want to be able to just require() the package and be done. Would a pull request for this functionality be welcome?

Right. I haven't had success triggering builds from my app via JavaScript, using something like require(). I think it might be possible if I were to include 11ty in my project, not as a dependency, but copied into my source code in its entirety (dependencies and all?), but that seems like silly stupid.

I did post a quick example of this here https://twitter.com/zachleat/status/1167115558552244226 but work is ongoing here for something official

So I got this to run from .eleventy.js by calling the enclosing function from a setTimeout:

/**
 * Set up a listener to rebuild on changes made in Sanity.
 * Add a token to .env.development to rebuild on all changes.
 */
async function preview() {
  const client = require('./utils/sanityClient')
  const Eleventy = require("@11ty/eleventy");
  let inst = new Eleventy();
  await inst.init();
  await client.listen('*').subscribe(async doc => {
    await inst.write();
  })
}
setTimeout(() => preview(), 200)

It now rebuilds when the listener register changes via the API.

@kmelve Do you think that setTimeout is something we鈥檙e doing or how Sanity is doing its thing?

One of my colleagues suggested it is because we have modules/files that鈥檚 reference each other. So it has nothing to do with Sanity, or Eleventy, but this: https://nodejs.org/api/modules.html#modules_cycles.

I think I鈥檒l just bring the preview function into a separate file and run it concurrently from package.json. There鈥檚 no reason it has to be in the config file outside of having stuff in one place.

If you do build a normal API, it would be really nice if 11ty could produce a stream that can be consumed by Gulp. As it stands, one must built to a temp directory first....

So @zachleat has this example here:

https://gist.github.com/zachleat/5b5ae0ddadbeba6169f18e5b4aa58a66

Problem is, AFAICT, that the config still comes from a .js file.

this.config = config.getConfig();

https://github.com/11ty/eleventy/blob/master/src/Eleventy.js#L30

In a cloud function this could be a problem since you may need to determine paths dynamically at runtime.

Is there a way to pass the config to the Eleventy constructor instead?

Note that #136 had an additional 37 votes at time of closing.

This repository is now using lodash style issue management for enhancements. This means enhancement issues will now be closed instead of leaving them open.

View the enhancement backlog here. Don鈥檛 forget to upvote the top comment with 馃憤!

For anyone that may still be interested, I've found an answer to @PierBover's question

So @zachleat has this example here:

https://gist.github.com/zachleat/5b5ae0ddadbeba6169f18e5b4aa58a66

Problem is, AFAICT, that the config still comes from a .js file.

this.config = config.getConfig();

https://github.com/11ty/eleventy/blob/master/src/Eleventy.js#L30

In a cloud function this could be a problem since you may need to determine paths dynamically at runtime.

Is there a way to pass the config to the Eleventy constructor instead?

It seems the config property is set in the Eleventy constructor but not accessed until init is called, so you can create the config yourself and set it directly on the instance before calling init, e.g.:

const Eleventy = require("@11ty/eleventy");

(async function() {
    let inst = new Eleventy();
        inst.config = { /*my custom config*/ };
    await inst.init();
    await inst.write();
})();

@PierBover and @blake-mealey, you can also use the setConfigPathOverride() method before calling init():

(async function() {
  const ssg = new Eleventy();
  // Pass a JavaScript file which exports an object:
  ssg.setConfigPathOverride('./config/vendor/eleventy.js');
  await ssg.init();
  await ssg.write();
})();
Was this page helpful?
0 / 5 - 0 ratings

Related issues

ndaidong picture ndaidong  路  4Comments

zellwk picture zellwk  路  3Comments

zac-heisey picture zac-heisey  路  3Comments

nebrelbug picture nebrelbug  路  3Comments

zachleat picture zachleat  路  3Comments