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

Function rewriteImportExpressions

src/javascript/transpile.ts:154–185  ·  view source on GitHub ↗
(
  output: Sourcemap,
  body: Node,
  resolveImport: (specifier: string) => string = String,
  resolveFile: (specifier: string) => string = String
)

Source from the content-addressed store, hash-verified

152}
153
154function rewriteImportExpressions(
155 output: Sourcemap,
156 body: Node,
157 resolveImport: (specifier: string) => string = String,
158 resolveFile: (specifier: string) => string = String
159): void {
160 function rewriteImportSource(source: StringLiteral) {
161 output.replaceLeft(source.start, source.end, JSON.stringify(resolveImport(getStringLiteralValue(source))));
162 }
163 simple(body, {
164 ImportExpression(node) {
165 const source = node.source;
166 if (isStringLiteral(source)) {
167 rewriteImportSource(source);
168 }
169 },
170 CallExpression(node) {
171 const source = node.arguments[0];
172 if (isImportMetaResolve(node) && isStringLiteral(source)) {
173 const value = getStringLiteralValue(source);
174 const resolution = isPathImport(value) && !isJavaScript(value) ? resolveFile(value) : resolveImport(value);
175 output.replaceLeft(
176 node.start,
177 node.end,
178 isPathImport(resolution)
179 ? `new URL(${JSON.stringify(resolution)}, location).href`
180 : JSON.stringify(resolution)
181 );
182 }
183 }
184 });
185}
186
187function rewriteImportDeclarations(
188 output: Sourcemap,

Callers 1

transpileJavaScriptFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected