Skip to main content
Module

x/deno2node/src/_transformations/vendor.ts

Compile your Deno project to run on Node.js.
Go to Latest
File
import { Node, SourceFile } from "../deps.deno.ts";import type { Context } from "../context.ts";
interface LocalFile { readonly sourceFile: SourceFile; readonly url: URL;}
function getVendoredPath(url: URL) { // FIXME respect ${vendorDir}/import_map.json return url.hostname + "/" + url.pathname;}
const createVendorSpecifiersFn = (ctx: Context) => ({ sourceFile, url }: LocalFile) => { if (!ctx.config.vendorDir) return; for (const statement of sourceFile.getStatements()) { if ( Node.isImportDeclaration(statement) || Node.isExportDeclaration(statement) ) { const oldSpecifierValue = statement.getModuleSpecifierValue(); if (oldSpecifierValue === undefined) continue; const newUrl = new URL(oldSpecifierValue, url); if (newUrl.protocol === "file:") continue; const newSpecifierValue = sourceFile.getRelativePathAsModuleSpecifierTo( ctx.resolve(ctx.config.vendorDir, getVendoredPath(newUrl)), ) + ".js"; statement.setModuleSpecifier(newSpecifierValue); } } };
function addUrl(sourceFile: SourceFile): LocalFile { return { sourceFile, url: new URL("./" + sourceFile.getFilePath(), "file:"), };}
export function vendorEverything(ctx: Context) { const vendorSpecifiers = createVendorSpecifiersFn(ctx); ctx.project.getSourceFiles().map(addUrl).forEach(vendorSpecifiers);}