JavaScript environment

Sketch runs JavaScript code in JavaScriptCore, the JavaScript engine that powers Safari, with full ES6 support.

Polyfills

For network, I/O operations and other operations there are Node.js compatible polyfills available. Many of these modules come preinstalled with Sketch.

You can find all the official polyfills on GitHub.

To see which modules are installed for a specific Sketch version, 50 and later, head to core-modules/package.json within the Sketch API and select the release branch for the version e.g. release/53.2.

Use macOS frameworks and dynamic Sketch runtime

All macOS frameworks and the internal Sketch APIs are made available to JavaScript by CocoaScript. For a more detailed overview see the CocoaScript documentation.

Asynchronous operations

The JavaScript context of a Sketch plugin is short-lived. Whenever a plugin is run, a new JavaScript environment is initialized and destroyed as soon as the script finished. To run and wait for asynchronous operations to complete, use fibers to keep the JavaScript environment alive.

let fiber = require('sketch/async').createFiber()

longRunningAsyncTask(function(res) {
  // after completion, tell the fiber we're done
  fiber.cleanup()
})