@hpcc-js/observablehq-compiler 1.1.4 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/ojscc.mjs +4 -5
- package/dist/index.esm.js +132 -84
- package/dist/index.esm.js.map +1 -1
- package/dist/index.esm.min.js +3 -3
- package/dist/index.esm.min.js.map +1 -1
- package/dist/index.js +132 -84
- package/dist/index.js.map +1 -1
- package/dist/index.min.js +3 -3
- package/dist/index.min.js.map +1 -1
- package/package.json +3 -3
- package/src/__package__.ts +2 -2
- package/src/__tests__/node.ts +1 -1
- package/src/compiler.ts +130 -94
- package/src/index.ts +1 -1
- package/src/util.ts +8 -4
- package/types/__package__.d.ts +2 -2
- package/types/compiler.d.ts +108 -39
- package/types/compiler.d.ts.map +1 -1
- package/types/index.d.ts +1 -1
- package/types/index.d.ts.map +1 -1
- package/types/util.d.ts +0 -1
- package/types/util.d.ts.map +1 -1
- package/types-3.4/__package__.d.ts +2 -2
- package/types-3.4/compiler.d.ts +108 -42
- package/types-3.4/index.d.ts +1 -1
- package/types-3.4/util.d.ts +0 -1
package/bin/ojscc.mjs
CHANGED
|
@@ -10,7 +10,7 @@ if (!globalThis.fetch) {
|
|
|
10
10
|
globalThis.Response = Response;
|
|
11
11
|
}
|
|
12
12
|
import { promises as fs } from "fs";
|
|
13
|
-
import { compile, download
|
|
13
|
+
import { compile, download } from "../dist/index.js";
|
|
14
14
|
import yargsMode from "yargs/yargs";
|
|
15
15
|
|
|
16
16
|
async function doDownload(url, filePath) {
|
|
@@ -25,12 +25,11 @@ async function doDownload(url, filePath) {
|
|
|
25
25
|
async function doCompile(url, filePath) {
|
|
26
26
|
const nb = await download(url);
|
|
27
27
|
const define = await compile(nb, process.cwd());
|
|
28
|
-
const
|
|
29
|
-
define.write(w);
|
|
28
|
+
const js = define.toString();
|
|
30
29
|
if (filePath) {
|
|
31
|
-
fs.writeFile(filePath,
|
|
30
|
+
fs.writeFile(filePath, js);
|
|
32
31
|
} else {
|
|
33
|
-
console.info(
|
|
32
|
+
console.info(js);
|
|
34
33
|
}
|
|
35
34
|
}
|
|
36
35
|
|
package/dist/index.esm.js
CHANGED
|
@@ -6618,8 +6618,13 @@ class ModuleParser extends CellParser {
|
|
|
6618
6618
|
}
|
|
6619
6619
|
}
|
|
6620
6620
|
function splitModule(input) {
|
|
6621
|
-
|
|
6622
|
-
|
|
6621
|
+
return ModuleParser
|
|
6622
|
+
.parse(input, { ecmaVersion: "latest" })
|
|
6623
|
+
.cells.map((cell) => ({
|
|
6624
|
+
text: input.substring(cell.start, cell.end),
|
|
6625
|
+
start: cell.start,
|
|
6626
|
+
end: cell.end
|
|
6627
|
+
}));
|
|
6623
6628
|
}
|
|
6624
6629
|
function parseCell$2(input) {
|
|
6625
6630
|
return parseCell$1(input);
|
|
@@ -6677,7 +6682,7 @@ function createParsedOJS(ojs, offset, inlineMD) {
|
|
|
6677
6682
|
error
|
|
6678
6683
|
};
|
|
6679
6684
|
}
|
|
6680
|
-
function
|
|
6685
|
+
function splitOmd(_) {
|
|
6681
6686
|
const retVal = [];
|
|
6682
6687
|
// Load Markdown ---
|
|
6683
6688
|
const re = /(```(?:\s|\S)[\s\S]*?```)/g;
|
|
@@ -6712,20 +6717,24 @@ function ojs2notebook(ojs) {
|
|
|
6712
6717
|
return {
|
|
6713
6718
|
id: idx,
|
|
6714
6719
|
mode: "js",
|
|
6715
|
-
value: cell
|
|
6720
|
+
value: cell.text,
|
|
6721
|
+
start: cell.start,
|
|
6722
|
+
end: cell.end
|
|
6716
6723
|
};
|
|
6717
6724
|
})
|
|
6718
6725
|
};
|
|
6719
6726
|
}
|
|
6720
6727
|
function omd2notebook(omd) {
|
|
6721
|
-
const cells =
|
|
6728
|
+
const cells = splitOmd(omd);
|
|
6722
6729
|
return {
|
|
6723
6730
|
files: [],
|
|
6724
6731
|
nodes: cells.map((cell, idx) => {
|
|
6725
6732
|
return {
|
|
6726
6733
|
id: idx,
|
|
6727
6734
|
mode: cell.inlineMD ? "md" : "js",
|
|
6728
|
-
value: cell.ojs
|
|
6735
|
+
value: cell.ojs,
|
|
6736
|
+
start: cell.offset,
|
|
6737
|
+
end: cell.offset + cell.ojs.length
|
|
6729
6738
|
};
|
|
6730
6739
|
})
|
|
6731
6740
|
};
|
|
@@ -6955,6 +6964,7 @@ export default function define(runtime, observer) {
|
|
|
6955
6964
|
}
|
|
6956
6965
|
}
|
|
6957
6966
|
|
|
6967
|
+
// Module ---
|
|
6958
6968
|
const isRelativePath = (path) => path[0] === ".";
|
|
6959
6969
|
const fullUrl = (path, basePath) => isRelativePath(path) ? join(basePath, path) : path;
|
|
6960
6970
|
async function importFile(relativePath, baseUrl) {
|
|
@@ -6970,14 +6980,14 @@ async function importFile(relativePath, baseUrl) {
|
|
|
6970
6980
|
else if (endsWith(relativePath, ".omd")) {
|
|
6971
6981
|
notebook = omd2notebook(content);
|
|
6972
6982
|
}
|
|
6973
|
-
const retVal = compile(notebook, baseUrl);
|
|
6974
|
-
retVal.
|
|
6983
|
+
const retVal = compile(notebook, { baseUrl });
|
|
6984
|
+
retVal.delete = () => { };
|
|
6975
6985
|
retVal.write = (w) => {
|
|
6976
6986
|
w.import(path);
|
|
6977
6987
|
};
|
|
6978
6988
|
return retVal;
|
|
6979
6989
|
}
|
|
6980
|
-
//
|
|
6990
|
+
// Import precompiled notebook from observable ---
|
|
6981
6991
|
async function importCompiledNotebook(partial) {
|
|
6982
6992
|
const url = `https://api.observablehq.com/${partial[0] === "@" ? partial : `d/${partial}`}.js?v=3`;
|
|
6983
6993
|
let impMod = {
|
|
@@ -6991,17 +7001,72 @@ async function importCompiledNotebook(partial) {
|
|
|
6991
7001
|
catch (e) {
|
|
6992
7002
|
}
|
|
6993
7003
|
const retVal = impMod.default;
|
|
6994
|
-
retVal.
|
|
7004
|
+
retVal.delete = () => { };
|
|
7005
|
+
retVal.write = (w) => {
|
|
7006
|
+
w.import(url);
|
|
7007
|
+
};
|
|
7008
|
+
return retVal;
|
|
7009
|
+
}
|
|
7010
|
+
// Recursive notebook parsing and compiling
|
|
7011
|
+
async function importNotebook(partial) {
|
|
7012
|
+
const url = `https://api.observablehq.com/document/${partial}`;
|
|
7013
|
+
const notebook = fetchEx(url)
|
|
7014
|
+
.then(r => {
|
|
7015
|
+
return r.json();
|
|
7016
|
+
}).catch(e => {
|
|
7017
|
+
console.error(url);
|
|
7018
|
+
console.error(e);
|
|
7019
|
+
});
|
|
7020
|
+
const retVal = compile(await notebook);
|
|
7021
|
+
retVal.delete = () => { };
|
|
6995
7022
|
retVal.write = (w) => {
|
|
6996
7023
|
w.import(url);
|
|
6997
7024
|
};
|
|
6998
7025
|
return retVal;
|
|
6999
7026
|
}
|
|
7000
|
-
function
|
|
7027
|
+
async function createModule(node, parsed, text, { baseUrl, importMode }) {
|
|
7028
|
+
const otherModule = isRelativePath(parsed.src) ?
|
|
7029
|
+
await importFile(parsed.src, baseUrl) :
|
|
7030
|
+
importMode === "recursive" ?
|
|
7031
|
+
await importNotebook(parsed.src) :
|
|
7032
|
+
await importCompiledNotebook(parsed.src);
|
|
7033
|
+
const importVariables = [];
|
|
7034
|
+
const variables = [];
|
|
7035
|
+
parsed.specifiers.forEach(spec => {
|
|
7036
|
+
const viewof = spec.view ? "viewof " : "";
|
|
7037
|
+
importVariables.push(createImportVariable(viewof + spec.name, viewof + spec.alias));
|
|
7038
|
+
if (spec.view) {
|
|
7039
|
+
importVariables.push(createImportVariable(spec.name, spec.alias));
|
|
7040
|
+
}
|
|
7041
|
+
});
|
|
7042
|
+
const retVal = (runtime, main, inspector) => {
|
|
7043
|
+
let mod = runtime.module(otherModule);
|
|
7044
|
+
if (parsed.injections.length) {
|
|
7045
|
+
mod = mod.derive(parsed.injections, main);
|
|
7046
|
+
}
|
|
7047
|
+
variables.forEach(v => v(main, inspector));
|
|
7048
|
+
importVariables.forEach(v => v(main, mod));
|
|
7049
|
+
return mod;
|
|
7050
|
+
};
|
|
7051
|
+
retVal.importVariables = importVariables;
|
|
7052
|
+
retVal.variables = variables;
|
|
7053
|
+
retVal.delete = () => {
|
|
7054
|
+
importVariables.forEach(v => v.delete());
|
|
7055
|
+
variables.forEach(v => v.delete());
|
|
7056
|
+
otherModule.delete();
|
|
7057
|
+
};
|
|
7058
|
+
retVal.write = (w) => {
|
|
7059
|
+
otherModule.write(w);
|
|
7060
|
+
w.importDefine(parsed);
|
|
7061
|
+
};
|
|
7062
|
+
return retVal;
|
|
7063
|
+
}
|
|
7064
|
+
// Variable ---
|
|
7065
|
+
function createVariable(node, inspect, name, inputs, definition, inline = false) {
|
|
7001
7066
|
let i;
|
|
7002
7067
|
let v;
|
|
7003
7068
|
const retVal = (module, inspector) => {
|
|
7004
|
-
i = inspect ? inspector(name) : undefined;
|
|
7069
|
+
i = inspect ? inspector(name, node.id) : undefined;
|
|
7005
7070
|
v = module.variable(i);
|
|
7006
7071
|
if (arguments.length > 1) {
|
|
7007
7072
|
try {
|
|
@@ -7011,9 +7076,22 @@ function createVariable(inspect, name, inputs, definition, inline = false) {
|
|
|
7011
7076
|
console.error(e === null || e === void 0 ? void 0 : e.message);
|
|
7012
7077
|
}
|
|
7013
7078
|
}
|
|
7079
|
+
if (node.pinned) {
|
|
7080
|
+
v = module.variable(inspector(name, node.id));
|
|
7081
|
+
try {
|
|
7082
|
+
v.define(undefined, ["md"], md => {
|
|
7083
|
+
return md `\`\`\`js
|
|
7084
|
+
${node.value}
|
|
7085
|
+
\`\`\``;
|
|
7086
|
+
});
|
|
7087
|
+
}
|
|
7088
|
+
catch (e) {
|
|
7089
|
+
console.error(e === null || e === void 0 ? void 0 : e.message);
|
|
7090
|
+
}
|
|
7091
|
+
}
|
|
7014
7092
|
return v;
|
|
7015
7093
|
};
|
|
7016
|
-
retVal.
|
|
7094
|
+
retVal.delete = () => {
|
|
7017
7095
|
var _a;
|
|
7018
7096
|
try {
|
|
7019
7097
|
(_a = i === null || i === void 0 ? void 0 : i._node) === null || _a === void 0 ? void 0 : _a.remove();
|
|
@@ -7045,91 +7123,52 @@ function createImportVariable(name, alias) {
|
|
|
7045
7123
|
v = main.variable();
|
|
7046
7124
|
v.import(name, alias, otherModule);
|
|
7047
7125
|
};
|
|
7048
|
-
retVal.
|
|
7126
|
+
retVal.delete = () => {
|
|
7049
7127
|
v === null || v === void 0 ? void 0 : v.delete();
|
|
7050
7128
|
};
|
|
7051
7129
|
return retVal;
|
|
7052
7130
|
}
|
|
7053
|
-
|
|
7054
|
-
|
|
7055
|
-
await importFile(parsed.src, baseUrl) :
|
|
7056
|
-
await importCompiledNotebook(parsed.src);
|
|
7057
|
-
const importVariables = [];
|
|
7058
|
-
const variables = [];
|
|
7059
|
-
parsed.specifiers.forEach(spec => {
|
|
7060
|
-
const viewof = spec.view ? "viewof " : "";
|
|
7061
|
-
importVariables.push(createImportVariable(viewof + spec.name, viewof + spec.alias));
|
|
7062
|
-
if (spec.view) {
|
|
7063
|
-
importVariables.push(createImportVariable(spec.name, spec.alias));
|
|
7064
|
-
}
|
|
7065
|
-
});
|
|
7066
|
-
variables.push(createVariable(true, undefined, ["md"], md => {
|
|
7067
|
-
return md `\`\`\`JavaScript
|
|
7068
|
-
${text}
|
|
7069
|
-
\`\`\``;
|
|
7070
|
-
}));
|
|
7071
|
-
const retVal = (runtime, main, inspector) => {
|
|
7072
|
-
let mod = runtime.module(otherModule);
|
|
7073
|
-
if (parsed.injections.length) {
|
|
7074
|
-
mod = mod.derive(parsed.injections, main);
|
|
7075
|
-
}
|
|
7076
|
-
variables.forEach(v => v(main, inspector));
|
|
7077
|
-
importVariables.forEach(v => v(main, mod));
|
|
7078
|
-
return mod;
|
|
7079
|
-
};
|
|
7080
|
-
retVal.importVariables = importVariables;
|
|
7081
|
-
retVal.variables = variables;
|
|
7082
|
-
retVal.dispose = () => {
|
|
7083
|
-
importVariables.forEach(v => v.dispose());
|
|
7084
|
-
variables.forEach(v => v.dispose());
|
|
7085
|
-
otherModule.dispose();
|
|
7086
|
-
};
|
|
7087
|
-
retVal.write = (w) => {
|
|
7088
|
-
otherModule.write(w);
|
|
7089
|
-
w.importDefine(parsed);
|
|
7090
|
-
};
|
|
7091
|
-
return retVal;
|
|
7092
|
-
}
|
|
7093
|
-
async function createCell(node, baseUrl) {
|
|
7131
|
+
// Cell ---
|
|
7132
|
+
async function createCell(node, options) {
|
|
7094
7133
|
const modules = [];
|
|
7095
7134
|
const variables = [];
|
|
7096
7135
|
try {
|
|
7097
7136
|
const text = node.mode && node.mode !== "js" ? `${node.mode}\`${encodeBacktick(node.value)}\`` : node.value;
|
|
7098
7137
|
const parsedModule = splitModule(text);
|
|
7099
|
-
for (const
|
|
7100
|
-
const parsed = parseCell(text);
|
|
7138
|
+
for (const cell of parsedModule) {
|
|
7139
|
+
const parsed = parseCell(cell.text);
|
|
7101
7140
|
switch (parsed.type) {
|
|
7102
7141
|
case "import":
|
|
7103
|
-
modules.push(await createModule(parsed, text,
|
|
7142
|
+
modules.push(await createModule(node, parsed, cell.text, options));
|
|
7104
7143
|
break;
|
|
7105
7144
|
case "viewof":
|
|
7106
|
-
variables.push(createVariable(true, parsed.variable.id, parsed.variable.inputs, parsed.variable.func));
|
|
7107
|
-
variables.push(createVariable(false, parsed.variableValue.id, parsed.variableValue.inputs, parsed.variableValue.func, true));
|
|
7145
|
+
variables.push(createVariable(node, true, parsed.variable.id, parsed.variable.inputs, parsed.variable.func));
|
|
7146
|
+
variables.push(createVariable(node, false, parsed.variableValue.id, parsed.variableValue.inputs, parsed.variableValue.func, true));
|
|
7108
7147
|
break;
|
|
7109
7148
|
case "mutable":
|
|
7110
|
-
variables.push(createVariable(false, parsed.initial.id, parsed.initial.inputs, parsed.initial.func));
|
|
7111
|
-
variables.push(createVariable(false, parsed.variable.id, parsed.variable.inputs, parsed.variable.func));
|
|
7112
|
-
variables.push(createVariable(true, parsed.variableValue.id, parsed.variableValue.inputs, parsed.variableValue.func, true));
|
|
7149
|
+
variables.push(createVariable(node, false, parsed.initial.id, parsed.initial.inputs, parsed.initial.func));
|
|
7150
|
+
variables.push(createVariable(node, false, parsed.variable.id, parsed.variable.inputs, parsed.variable.func));
|
|
7151
|
+
variables.push(createVariable(node, true, parsed.variableValue.id, parsed.variableValue.inputs, parsed.variableValue.func, true));
|
|
7113
7152
|
break;
|
|
7114
7153
|
case "variable":
|
|
7115
|
-
variables.push(createVariable(true, parsed.id, parsed.inputs, parsed.func));
|
|
7154
|
+
variables.push(createVariable(node, true, parsed.id, parsed.inputs, parsed.func));
|
|
7116
7155
|
break;
|
|
7117
7156
|
}
|
|
7118
7157
|
}
|
|
7119
7158
|
}
|
|
7120
7159
|
catch (e) {
|
|
7121
|
-
variables.push(createVariable(true, undefined, [], e.message));
|
|
7160
|
+
variables.push(createVariable(node, true, undefined, [], e.message));
|
|
7122
7161
|
}
|
|
7123
7162
|
const retVal = (runtime, main, inspector) => {
|
|
7124
7163
|
modules.forEach(imp => imp(runtime, main, inspector));
|
|
7125
7164
|
variables.forEach(v => v(main, inspector));
|
|
7126
7165
|
};
|
|
7127
|
-
retVal.id =
|
|
7166
|
+
retVal.id = node.id;
|
|
7128
7167
|
retVal.modules = modules;
|
|
7129
7168
|
retVal.variables = variables;
|
|
7130
|
-
retVal.
|
|
7131
|
-
variables.forEach(v => v.
|
|
7132
|
-
modules.forEach(mod => mod.
|
|
7169
|
+
retVal.delete = () => {
|
|
7170
|
+
variables.forEach(v => v.delete());
|
|
7171
|
+
modules.forEach(mod => mod.delete());
|
|
7133
7172
|
};
|
|
7134
7173
|
retVal.write = (w) => {
|
|
7135
7174
|
modules.forEach(imp => imp.write(w));
|
|
@@ -7137,14 +7176,14 @@ async function createCell(node, baseUrl) {
|
|
|
7137
7176
|
};
|
|
7138
7177
|
return retVal;
|
|
7139
7178
|
}
|
|
7140
|
-
|
|
7179
|
+
// File ---
|
|
7180
|
+
function createFile(file, options) {
|
|
7141
7181
|
function toString() { return globalThis.url; }
|
|
7142
|
-
return [file.name, { url: new URL(fullUrl(file.url, baseUrl)), mimeType: file.mime_type, toString }];
|
|
7182
|
+
return [file.name, { url: new URL(fullUrl(file.url, options.baseUrl)), mimeType: file.mime_type, toString }];
|
|
7143
7183
|
}
|
|
7144
|
-
|
|
7145
|
-
const files =
|
|
7184
|
+
function notebook(_files = [], _cells = [], { baseUrl = ".", importMode = "precompiled" } = {}) {
|
|
7185
|
+
const files = _files.map(f => createFile(f, { baseUrl, importMode }));
|
|
7146
7186
|
const fileAttachments = new Map(files);
|
|
7147
|
-
const _cells = await Promise.all(notebook.nodes.map(n => createCell(n, baseUrl)));
|
|
7148
7187
|
const cells = new Map(_cells.map(c => [c.id, c]));
|
|
7149
7188
|
const retVal = (runtime, inspector) => {
|
|
7150
7189
|
const main = runtime.module();
|
|
@@ -7160,25 +7199,29 @@ async function compile(notebook, baseUrl = ".") {
|
|
|
7160
7199
|
};
|
|
7161
7200
|
retVal.fileAttachments = fileAttachments;
|
|
7162
7201
|
retVal.cells = cells;
|
|
7163
|
-
retVal.
|
|
7164
|
-
const cell = await createCell(n, baseUrl);
|
|
7165
|
-
retVal.
|
|
7202
|
+
retVal.set = async (n) => {
|
|
7203
|
+
const cell = await createCell(n, { baseUrl, importMode });
|
|
7204
|
+
retVal.delete(cell.id);
|
|
7166
7205
|
cells.set(cell.id, cell);
|
|
7167
7206
|
return cell;
|
|
7168
7207
|
};
|
|
7169
|
-
retVal.
|
|
7208
|
+
retVal.get = (id) => {
|
|
7209
|
+
return cells.get(id);
|
|
7210
|
+
};
|
|
7211
|
+
retVal.delete = (id) => {
|
|
7170
7212
|
const cell = cells.get(id);
|
|
7171
7213
|
if (cell) {
|
|
7172
|
-
|
|
7173
|
-
|
|
7214
|
+
cell.delete();
|
|
7215
|
+
return cells.delete(id);
|
|
7174
7216
|
}
|
|
7217
|
+
return false;
|
|
7175
7218
|
};
|
|
7176
|
-
retVal.
|
|
7177
|
-
cells.forEach(cell => cell.
|
|
7219
|
+
retVal.clear = () => {
|
|
7220
|
+
cells.forEach(cell => cell.delete());
|
|
7178
7221
|
cells.clear();
|
|
7179
7222
|
};
|
|
7180
7223
|
retVal.write = (w) => {
|
|
7181
|
-
w.files(
|
|
7224
|
+
w.files(_files);
|
|
7182
7225
|
cells.forEach(cell => cell.write(w));
|
|
7183
7226
|
};
|
|
7184
7227
|
retVal.toString = (w = new Writer()) => {
|
|
@@ -7187,6 +7230,11 @@ async function compile(notebook, baseUrl = ".") {
|
|
|
7187
7230
|
};
|
|
7188
7231
|
return retVal;
|
|
7189
7232
|
}
|
|
7233
|
+
async function compile(notebookOrOjs, { baseUrl = ".", importMode = "precompiled" } = {}) {
|
|
7234
|
+
const ojsNotebook = typeof notebookOrOjs === "string" ? ojs2notebook(notebookOrOjs) : notebookOrOjs;
|
|
7235
|
+
const _cells = await Promise.all(ojsNotebook.nodes.map(n => createCell(n, { baseUrl, importMode })));
|
|
7236
|
+
return notebook(ojsNotebook.files, _cells, { baseUrl, importMode });
|
|
7237
|
+
}
|
|
7190
7238
|
|
|
7191
|
-
export { compile, download, ojs2notebook, omd2notebook
|
|
7239
|
+
export { compile, download, notebook, ojs2notebook, omd2notebook };
|
|
7192
7240
|
//# sourceMappingURL=index.esm.js.map
|