@observablehq/notebook-kit 2.3.0 → 2.4.1
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/package.json +1 -1
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.js +1 -0
- package/dist/src/javascript/imports/observable.js +1 -1
- package/dist/src/javascript/imports.d.ts +6 -3
- package/dist/src/javascript/imports.js +10 -9
- package/dist/src/javascript/transpile.d.ts +3 -4
- package/package.json +1 -1
package/dist/package.json
CHANGED
package/dist/src/index.d.ts
CHANGED
package/dist/src/index.js
CHANGED
|
@@ -20,7 +20,7 @@ function getImportAttributeKey(node) {
|
|
|
20
20
|
export function renderObservableImport(source, node, inputs) {
|
|
21
21
|
if (!inputs.includes("@variable"))
|
|
22
22
|
inputs.push("@variable");
|
|
23
|
-
return `(import(${
|
|
23
|
+
return `(import(${source}).then((_) => {
|
|
24
24
|
const module = __variable._module._runtime.module(_.default)${"injections" in node ? `.derive([${renderObservableInjections(node)}], __variable._module)` : ""};
|
|
25
25
|
const outputs = new Map(Array.from(__variable._outputs, (v) => [v._name, v]));${flatMapImportSpecifiers(node, (specifier) => {
|
|
26
26
|
const i = dedollar(getImportedName(specifier));
|
|
@@ -14,13 +14,16 @@ export declare function isImportMetaResolve(node: CallExpression): boolean;
|
|
|
14
14
|
/** Returns true if the given node is an import.meta.url expression. */
|
|
15
15
|
export declare function isImportMetaUrl(node: MemberExpression): boolean;
|
|
16
16
|
export declare function isTypeImport(node: AnyImportSpecifier | ImportDeclaration): boolean;
|
|
17
|
-
export
|
|
17
|
+
export declare function resolveImportDefault(specifier: string): string;
|
|
18
|
+
export type ImportOptions = {
|
|
19
|
+
/** Returns the URL for the given import specifier; defaults to resolveImportDefault. */
|
|
20
|
+
resolveImport?: (specifier: string) => string;
|
|
18
21
|
/** If true, resolve local imports relative to document.baseURI. */
|
|
19
22
|
resolveLocalImports?: boolean;
|
|
20
23
|
};
|
|
21
|
-
export declare function rewriteImportExpressions(output: Sourcemap, body: Node, { resolveLocalImports }?:
|
|
24
|
+
export declare function rewriteImportExpressions(output: Sourcemap, body: Node, { resolveImport, resolveLocalImports }?: ImportOptions): void;
|
|
22
25
|
/** Note: mutates inputs! */
|
|
23
|
-
export declare function rewriteImportDeclarations(output: Sourcemap, body: Node, inputs: string[], { resolveLocalImports }?:
|
|
26
|
+
export declare function rewriteImportDeclarations(output: Sourcemap, body: Node, inputs: string[], { resolveImport, resolveLocalImports }?: ImportOptions): void;
|
|
24
27
|
export declare function getLocalName(node: NamedImportSpecifier): string;
|
|
25
28
|
export declare function getImportedName(node: NamedImportSpecifier): string;
|
|
26
29
|
export declare function flatMapImportSpecifiers<T>(node: ImportDeclaration, f: (node: NamedImportSpecifier) => T): T[];
|
|
@@ -47,10 +47,10 @@ export function isImportMetaUrl(node) {
|
|
|
47
47
|
export function isTypeImport(node) {
|
|
48
48
|
return node.importKind === "type";
|
|
49
49
|
}
|
|
50
|
-
function
|
|
50
|
+
export function resolveImportDefault(specifier) {
|
|
51
51
|
return resolveObservableImport(resolveJsrImport(resolveNpmImport(specifier)));
|
|
52
52
|
}
|
|
53
|
-
export function rewriteImportExpressions(output, body, { resolveLocalImports } = {}) {
|
|
53
|
+
export function rewriteImportExpressions(output, body, { resolveImport = resolveImportDefault, resolveLocalImports } = {}) {
|
|
54
54
|
function rewriteImportSource(source, node = source) {
|
|
55
55
|
const value = getStringLiteralValue(source);
|
|
56
56
|
const resolution = resolveImport(value);
|
|
@@ -79,7 +79,7 @@ export function rewriteImportExpressions(output, body, { resolveLocalImports } =
|
|
|
79
79
|
});
|
|
80
80
|
}
|
|
81
81
|
/** Note: mutates inputs! */
|
|
82
|
-
export function rewriteImportDeclarations(output, body, inputs, { resolveLocalImports } = {}) {
|
|
82
|
+
export function rewriteImportDeclarations(output, body, inputs, { resolveImport = resolveImportDefault, resolveLocalImports } = {}) {
|
|
83
83
|
const declarations = [];
|
|
84
84
|
simple(body, {
|
|
85
85
|
ImportDeclaration(node) {
|
|
@@ -93,16 +93,17 @@ export function rewriteImportDeclarations(output, body, inputs, { resolveLocalIm
|
|
|
93
93
|
});
|
|
94
94
|
const specifiers = [];
|
|
95
95
|
const imports = [];
|
|
96
|
-
for (const [node,
|
|
96
|
+
for (const [node, literal] of declarations) {
|
|
97
97
|
output.delete(node.start, node.end + +(output.input[node.end] === "\n"));
|
|
98
98
|
specifiers.push(rewriteImportSpecifiers(node));
|
|
99
|
-
const value = getStringLiteralValue(
|
|
99
|
+
const value = getStringLiteralValue(literal);
|
|
100
100
|
const resolution = resolveImport(value);
|
|
101
|
+
const source = resolveLocalImports && isLocalImport(resolution)
|
|
102
|
+
? `new URL(${JSON.stringify(resolution)}, document.baseURI)`
|
|
103
|
+
: JSON.stringify(resolution);
|
|
101
104
|
imports.push(isObservableImport(node, value)
|
|
102
|
-
? renderObservableImport(
|
|
103
|
-
: renderImport(
|
|
104
|
-
? `new URL(${JSON.stringify(resolution)}, document.baseURI)`
|
|
105
|
-
: JSON.stringify(resolution), node, output.input));
|
|
105
|
+
? renderObservableImport(source, node, inputs)
|
|
106
|
+
: renderImport(source, node, output.input));
|
|
106
107
|
}
|
|
107
108
|
if (declarations.length > 1) {
|
|
108
109
|
output.insertLeft(0, `const [${specifiers.join(", ")}] = await Promise.all([${imports.join(", ")}]);\n`); // prettier-ignore
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Cell } from "../lib/notebook.js";
|
|
2
|
+
import type { ImportOptions } from "./imports.js";
|
|
2
3
|
export type TranspiledJavaScript = {
|
|
3
4
|
/** the source code of a JavaScript function defining the primary variable */
|
|
4
5
|
body: string;
|
|
@@ -21,12 +22,10 @@ export type TranspiledJavaScript = {
|
|
|
21
22
|
/** the names of any referenced secrets */
|
|
22
23
|
secrets?: Set<string>;
|
|
23
24
|
};
|
|
24
|
-
export
|
|
25
|
-
/** If true, resolve local imports paths relative to document.baseURI. */
|
|
26
|
-
resolveLocalImports?: boolean;
|
|
25
|
+
export interface TranspileOptions extends ImportOptions {
|
|
27
26
|
/** If true, resolve file using import.meta.url (so Vite treats it as an asset). */
|
|
28
27
|
resolveFiles?: boolean;
|
|
29
|
-
}
|
|
28
|
+
}
|
|
30
29
|
/** @deprecated */
|
|
31
30
|
export declare function transpile(input: string, mode: Cell["mode"], options?: TranspileOptions): TranspiledJavaScript;
|
|
32
31
|
export declare function transpile(input: Cell, options?: TranspileOptions): TranspiledJavaScript;
|