MCPcopy Create free account
hub / github.com/observablehq/framework / rewriteImportDeclarations

Function rewriteImportDeclarations

src/javascript/transpile.ts:187–214  ·  view source on GitHub ↗
(
  output: Sourcemap,
  body: Node,
  resolve: (specifier: string) => string = String
)

Source from the content-addressed store, hash-verified

185}
186
187function rewriteImportDeclarations(
188 output: Sourcemap,
189 body: Node,
190 resolve: (specifier: string) => string = String
191): void {
192 const declarations: ImportDeclaration[] = [];
193
194 simple(body, {
195 ImportDeclaration(node) {
196 if (isStringLiteral(node.source)) {
197 declarations.push(node);
198 }
199 }
200 });
201
202 const specifiers: string[] = [];
203 const imports: string[] = [];
204 for (const node of declarations) {
205 output.delete(node.start, node.end + +(output.input[node.end] === "\n"));
206 specifiers.push(rewriteImportSpecifiers(node));
207 imports.push(`import(${annotatePath(resolve(getStringLiteralValue(node.source as StringLiteral)))})`);
208 }
209 if (declarations.length > 1) {
210 output.insertLeft(0, `const [${specifiers.join(", ")}] = await Promise.all([${imports.join(", ")}]);\n`);
211 } else if (declarations.length === 1) {
212 output.insertLeft(0, `const ${specifiers[0]} = await ${imports[0]};\n`);
213 }
214}
215
216function rewriteImportSpecifiers(node: ImportDeclaration): string {
217 return node.specifiers.some(isNotNamespaceSpecifier)

Callers 1

transpileJavaScriptFunction · 0.85

Calls 5

rewriteImportSpecifiersFunction · 0.85
getStringLiteralValueFunction · 0.85
deleteMethod · 0.80
insertLeftMethod · 0.80
resolveFunction · 0.50

Tested by

no test coverage detected