deno.land / x / deno2node@v1.6.0 / src / _transformations / vendor.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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);}
deno2node

Version Info

Tagged at
a year ago