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

Function transform

src/rollup.ts:156–185  ·  view source on GitHub ↗
(code)

Source from the content-addressed store, hash-verified

154 return {
155 name: "resolve-import-meta-resolve",
156 async transform(code) {
157 const program = this.parse(code);
158 const resolves: StringLiteral[] = [];
159
160 simple(program, {
161 CallExpression(node) {
162 if (
163 node.callee.type === "MemberExpression" &&
164 node.callee.object.type === "MetaProperty" &&
165 node.callee.property.type === "Identifier" &&
166 node.callee.property.name === "resolve" &&
167 node.arguments.length === 1 &&
168 isStringLiteral(node.arguments[0])
169 ) {
170 resolves.push(node.arguments[0]);
171 }
172 }
173 });
174
175 if (!resolves.length) return null;
176
177 const output = new Sourcemap(code);
178 for (const source of resolves) {
179 const specifier = getStringLiteralValue(source);
180 const resolution = await resolveImport(specifier);
181 if (resolution) output.replaceLeft(source.start, source.end, annotatePath(relativePath(path, resolution)));
182 }
183
184 return {code: String(output)};
185 }
186 };
187}

Callers 1

transformJavaScriptFunction · 0.85

Calls 4

replaceLeftMethod · 0.95
getStringLiteralValueFunction · 0.85
relativePathFunction · 0.85
resolveImportFunction · 0.70

Tested by

no test coverage detected