angular-cn/aio/tools/stackblitz-builder
Sonu Kapoor 77c53559d0 build(docs-infra): specify more relevant landing files for StackBlitz projects (#34553)
The StackBlitz API (which we use to generate StackBlitz projects on the
fly, when a user clicks on a live example link in the docs) allows
specifying the file to open in the editor by passing a query param. If
no file is specified, StackBlitz opens a default one.

This commit specifies relevant landing files for some of the examples to
make it easier to engage with the examples more quickly, and to see what
the example is attempting to demonstrate.

Fixes #22357.

PR Close #34553
2020-01-22 13:45:23 -08:00
..
README.md docs(docs-infra): fix typos and spelling in the stackblitz-builder/README.md (#34009) 2019-11-25 11:39:05 -05:00
builder.js build(docs-infra): specify more relevant landing files for StackBlitz projects (#34553) 2020-01-22 13:45:23 -08:00
generateStackblitz.js build(aio): migrate plunker to stackblitz (#20165) 2018-01-23 21:30:27 -08:00

README.md

Overview

Stackblitz is an online tool for creating, collaborating and sharing ideas. In AIO we use it to share one or more runnable versions of our examples.

Stackblitz can be used both on a separate page and in an embedded form.

  • generateStackblitz.js - executes each of the StackblitzBuilder to generate a stackblitz file for each example.

Stackblitz generation

Both forms are created within builder.js. How is a stackblitz created? What is the process from a directory with files to a link with a stackblitz.

An "executable" stackblitz is an HTML file with a <form> that makes a post to stackblitz on submit. It contains an <input> element for each file we need in the stackblitz.

The form will be submitted on load, so you can either double click the HTML file or open it with an anchor tag to open the stackblitz.

So the builder.js job is to get all the needed files from an example and build this HTML file for you.

Customizing the generation per example basis

How does this tool know what is an example and what is not? It will look for all folders containing a stackblitz.json file. If found, all files within the folder and subfolders will be used in the stackblitz, with a few generic exceptions that you can find at builder.js.

You can use the stackblitz.json to customize the stackblitz generation. For example:

{
  "description": "Tour of Heroes: Part 6",
  "files":[
    "!**/*.d.ts",
    "!**/*.js",
    "!**/*.[1,2].*"
  ],
  "tags": ["tutorial", "tour", "heroes", "http"]
}

Here you can specify a description for the stackblitz, some tags and also a files array where you can specify extra files to add or to ignore.

Executing the stackblitz generation

generateStackblitz.js will create a stackblitz for each stackblitz.json it finds.

Where? At src/generated/live-examples/.

Then the <live-example> embedded component will look at this folder to get the stackblitz it needs for the example.

Appendix: Why not generating stackblitz at runtime?

At AngularJS, all the plunker examples were generated at runtime. The downside was that all the example code had to be deployed as well and would no longer be useful after the plunker was generated.

This StackblitzBuilder tool takes a few seconds to run, and the end result is only 3mb~.