@hpcc-js/observablehq-compiler 1.2.0 → 1.2.2

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/dist/index.esm.js CHANGED
@@ -1,5 +1,3 @@
1
- import { join, endsWith } from '@hpcc-js/util';
2
-
3
1
  // This file was generated. Do not modify manually!
4
2
  var astralIdentifierCodes = [509, 0, 227, 0, 150, 4, 294, 9, 1368, 2, 2, 1, 6, 3, 41, 2, 5, 0, 166, 1, 574, 3, 9, 9, 370, 1, 154, 10, 50, 3, 123, 2, 54, 14, 32, 10, 3, 1, 11, 3, 46, 10, 8, 0, 46, 9, 7, 2, 37, 13, 2, 9, 6, 1, 45, 0, 13, 2, 49, 13, 9, 3, 2, 11, 83, 11, 7, 0, 161, 11, 6, 9, 7, 3, 56, 1, 2, 6, 3, 1, 3, 2, 10, 0, 11, 1, 3, 6, 4, 4, 193, 17, 10, 9, 5, 0, 82, 19, 13, 9, 214, 6, 3, 8, 28, 1, 83, 16, 16, 9, 82, 12, 9, 9, 84, 14, 5, 9, 243, 14, 166, 9, 71, 5, 2, 1, 3, 3, 2, 0, 2, 1, 13, 9, 120, 6, 3, 6, 4, 0, 29, 9, 41, 6, 2, 3, 9, 0, 10, 10, 47, 15, 406, 7, 2, 7, 17, 9, 57, 21, 2, 13, 123, 5, 4, 0, 2, 1, 2, 6, 2, 0, 9, 9, 49, 4, 2, 1, 2, 4, 9, 9, 330, 3, 19306, 9, 87, 9, 39, 4, 60, 6, 26, 9, 1014, 0, 2, 54, 8, 3, 82, 0, 12, 1, 19628, 1, 4706, 45, 3, 22, 543, 4, 4, 5, 9, 7, 3, 6, 31, 3, 149, 2, 1418, 49, 513, 54, 5, 49, 9, 0, 15, 0, 23, 4, 2, 14, 1361, 6, 2, 16, 3, 6, 2, 1, 2, 4, 262, 6, 10, 9, 357, 0, 62, 13, 1495, 6, 110, 6, 6, 9, 4759, 9, 787719, 239];
5
3
 
@@ -6657,6 +6655,18 @@ function createFunction(refs, async = false, generator = false, blockStatement =
6657
6655
  body.substring(1, body.length - 1).trim() :
6658
6656
  `return (\n${body}\n);`);
6659
6657
  }
6658
+ function join(baseURL, relativeURL) {
6659
+ return relativeURL
6660
+ ? baseURL.replace(/\/+$/, "") + "/" + relativeURL.replace(/^\/+/, "")
6661
+ : baseURL;
6662
+ }
6663
+ const isRelativePath = (path) => path[0] === ".";
6664
+ const fixRelativeUrl = (path, basePath) => {
6665
+ if (isRelativePath(path)) {
6666
+ return join(basePath, path);
6667
+ }
6668
+ return path;
6669
+ };
6660
6670
  // Hide "import" from bundlers as they have a habit of replacing "import" with "require"
6661
6671
  async function obfuscatedImport(url) {
6662
6672
  return new FuncTypes.asyncFunctionType("url", "return import(url)")(url);
@@ -6799,7 +6809,7 @@ function calcRefs(cellAst, cellStr) {
6799
6809
  }
6800
6810
  return retVal;
6801
6811
  }
6802
- function parseImportExpression(cellAst) {
6812
+ function parseImportDeclaration(cellAst) {
6803
6813
  var _a, _b, _c, _d;
6804
6814
  return {
6805
6815
  type: "import",
@@ -6876,14 +6886,17 @@ function parseVariableExpression(cellStr, cellAst, refs, bodyStr) {
6876
6886
  func: createFunction(refs, cellAst.async, cellAst.generator, cellAst.body.type === "BlockStatement", bodyStr)
6877
6887
  };
6878
6888
  }
6879
- function parseCell(cellStr) {
6889
+ function parseCell(cellStr, baseUrl) {
6880
6890
  var _a, _b;
6881
6891
  const cellAst = parseCell$2(cellStr);
6882
- if (((_a = (cellAst.body)) === null || _a === void 0 ? void 0 : _a.type) == "ImportDeclaration") {
6883
- return parseImportExpression(cellAst);
6892
+ let bodyStr = cellAst.body && cellStr.substring(cellAst.body.start, cellAst.body.end);
6893
+ switch ((_a = (cellAst.body)) === null || _a === void 0 ? void 0 : _a.type) {
6894
+ case "ImportDeclaration":
6895
+ return parseImportDeclaration(cellAst);
6896
+ case "ImportExpression":
6897
+ bodyStr = `import("${fixRelativeUrl(cellAst.body.source.value, baseUrl)}")`;
6884
6898
  }
6885
6899
  const refs = calcRefs(cellAst, cellStr);
6886
- const bodyStr = cellAst.body && cellStr.substring(cellAst.body.start, cellAst.body.end);
6887
6900
  switch ((_b = cellAst.id) === null || _b === void 0 ? void 0 : _b.type) {
6888
6901
  case "ViewExpression":
6889
6902
  return parseViewExpression(cellStr, cellAst, refs, bodyStr);
@@ -6964,20 +6977,17 @@ export default function define(runtime, observer) {
6964
6977
  }
6965
6978
  }
6966
6979
 
6967
- // Module ---
6968
- const isRelativePath = (path) => path[0] === ".";
6969
- const fullUrl = (path, basePath) => isRelativePath(path) ? join(basePath, path) : path;
6970
6980
  async function importFile(relativePath, baseUrl) {
6971
- const path = fullUrl(relativePath, baseUrl);
6981
+ const path = fixRelativeUrl(relativePath, baseUrl);
6972
6982
  const content = await fetchEx(path).then(r => r.text());
6973
6983
  let notebook;
6974
- if (endsWith(relativePath, ".ojsnb")) {
6984
+ if (relativePath.endsWith(".ojsnb")) {
6975
6985
  notebook = JSON.parse(content);
6976
6986
  }
6977
- else if (endsWith(relativePath, ".ojs")) {
6987
+ else if (relativePath.endsWith(".ojs")) {
6978
6988
  notebook = ojs2notebook(content);
6979
6989
  }
6980
- else if (endsWith(relativePath, ".omd")) {
6990
+ else if (relativePath.endsWith(".omd")) {
6981
6991
  notebook = omd2notebook(content);
6982
6992
  }
6983
6993
  const retVal = compile(notebook, { baseUrl });
@@ -7136,7 +7146,7 @@ async function createCell(node, options) {
7136
7146
  const text = node.mode && node.mode !== "js" ? `${node.mode}\`${encodeBacktick(node.value)}\`` : node.value;
7137
7147
  const parsedModule = splitModule(text);
7138
7148
  for (const cell of parsedModule) {
7139
- const parsed = parseCell(cell.text);
7149
+ const parsed = parseCell(cell.text, options.baseUrl);
7140
7150
  switch (parsed.type) {
7141
7151
  case "import":
7142
7152
  modules.push(await createModule(node, parsed, cell.text, options));
@@ -7179,7 +7189,7 @@ async function createCell(node, options) {
7179
7189
  // File ---
7180
7190
  function createFile(file, options) {
7181
7191
  function toString() { return globalThis.url; }
7182
- return [file.name, { url: new URL(fullUrl(file.url, options.baseUrl)), mimeType: file.mime_type, toString }];
7192
+ return [file.name, { url: new URL(fixRelativeUrl(file.url, options.baseUrl)), mimeType: file.mime_type, toString }];
7183
7193
  }
7184
7194
  function notebook(_files = [], _cells = [], { baseUrl = ".", importMode = "precompiled" } = {}) {
7185
7195
  const files = _files.map(f => createFile(f, { baseUrl, importMode }));
@@ -7189,7 +7199,7 @@ function notebook(_files = [], _cells = [], { baseUrl = ".", importMode = "preco
7189
7199
  const main = runtime.module();
7190
7200
  main.builtin("FileAttachment", runtime.fileAttachments(name => {
7191
7201
  var _a;
7192
- return (_a = fileAttachments.get(name)) !== null && _a !== void 0 ? _a : { url: new URL(fullUrl(name, baseUrl)), mimeType: null };
7202
+ return (_a = fileAttachments.get(name)) !== null && _a !== void 0 ? _a : { url: new URL(fixRelativeUrl(name, baseUrl)), mimeType: null };
7193
7203
  }));
7194
7204
  main.builtin("fetchEx", fetchEx);
7195
7205
  cells.forEach(cell => {