Skip to content

Tree Shaking #28

@asg017

Description

@asg017

This has been an idea in the back of my head for a while, but this issue made me put it into better words.

Basically, with .moduleToESModule() and .module(), it would be nice to be able to specify a list of cells, and the compiled output only defines the cells needed to execute up to the specified cells. For example, if the original source is like:

a = 1; 
b = 2; 
c = a + b; 

x = 2; 
y = 4; 
z = x - y;

import {d3, chart} with {c as height} from "@d3/bar-chart"; 

Then we could specify the following to get different outputs.

c, z:

export default function define(runtime, observer) {
  const main = runtime.module();

  main.variable(observer("a")).define("a", function(){return(
1
)});
  main.variable(observer("b")).define("b", function(){return(
2
)});
  main.variable(observer("c")).define("c", ["a","b"], function(a,b){return(
a + b
)});
  main.variable(observer("x")).define("x", function(){return(
2
)});
  main.variable(observer("y")).define("y", function(){return(
4
)});
  main.variable(observer("z")).define("z", ["x","y"], function(x,y){return(
x - y
)});
  return main;
}

a, x

export default function define(runtime, observer) {
  const main = runtime.module();

  main.variable(observer("a")).define("a", function(){return(
1
)});
  main.variable(observer("x")).define("x", function(){return(
2
)});
  return main;
}

chart

import define1 from "https://api.observablehq.com/@d3/bar-chart.js?v=3";

export default function define(runtime, observer) {
  const main = runtime.module();

  main.variable(observer("a")).define("a", function(){return(
1
)});
  main.variable(observer("b")).define("b", function(){return(
2
)});
  main.variable(observer("c")).define("c", ["a","b"], function(a,b){return(
a + b
)});
  const child1 = runtime.module(define1).derive([{"name":"c","alias":"height"}], main);
  main.import("d3", "d3", child1);
  main.import("chart", "chart", child1);
  return main;
}

To do it, we would probably need to create the cell's DAG and determine which cells to include/exclude. Also to remove unnecessary import statements (while being careful with edge import with cases), and maybe unnecessary FileAttachments too, but that may be harder...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions