UNPKG

@oazmi/build-tools

Version:

general deno build tool scripts which I practically use in all of my typescript repos

72 lines (71 loc) 4.78 kB
// Copyright 2018-2024 the Deno authors. MIT license. import { ts } from "../../../../@ts-morph/bootstrap/0.24.0/mod.js"; // transform `import.meta.url` to a replacement that works in script modules export const transformImportMeta = (context) => { const factory = context.factory; const compilerModule = context.getCompilerOptions().module; const isScriptModule = compilerModule === ts.ModuleKind.CommonJS || compilerModule === ts.ModuleKind.UMD; return (sourceFile) => ts.visitEachChild(sourceFile, visitNode, context); function visitNode(node) { // find `import.meta.resolve` if (ts.isCallExpression(node) && node.arguments.length === 1 && isImportMetaProp(node.expression) && node.expression.name.escapedText === "resolve") { return ts.visitEachChild(getReplacementImportMetaResolve(node.arguments), visitNode, context); } else if (isImportMetaProp(node)) { // find `import.meta.url` or `import.meta.main` if (node.name.escapedText === "url" && isScriptModule) { return getReplacementImportMetaUrl(); } else if (node.name.escapedText === "main") { if (isScriptModule) { return getReplacementImportMetaMainScript(); } else { return getReplacementImportMetaMainEsm(); } } } return ts.visitEachChild(node, visitNode, context); } function isImportMetaProp(node) { return ts.isPropertyAccessExpression(node) && ts.isMetaProperty(node.expression) && node.expression.keywordToken === ts.SyntaxKind.ImportKeyword && ts.isIdentifier(node.name); } function getReplacementImportMetaUrl() { // Copy and pasted from ts-ast-viewer.com // require("url").pathToFileURL(__filename).href return factory.createPropertyAccessExpression(factory.createCallExpression(factory.createPropertyAccessExpression(factory.createCallExpression(factory.createIdentifier("require"), undefined, [factory.createStringLiteral("url")]), factory.createIdentifier("pathToFileURL")), undefined, [factory.createIdentifier("__filename")]), factory.createIdentifier("href")); } function getReplacementImportMetaMainScript() { // Copy and pasted from ts-ast-viewer.com // (require.main === module) return factory.createParenthesizedExpression(factory.createBinaryExpression(factory.createPropertyAccessExpression(factory.createIdentifier("require"), factory.createIdentifier("main")), factory.createToken(ts.SyntaxKind.EqualsEqualsEqualsToken), factory.createIdentifier("module"))); } function getReplacementImportMetaMainEsm() { // Copy and pasted from ts-ast-viewer.com // (import.meta.url === ('file:///'+process.argv[1].replace(/\\/g,'/')).replace(/\/{3,}/,'///')); // 1. `process.argv[1]` is fullpath; // 2. Win's path is `E:\path\to\main.mjs`, replace to `E:/path/to/main.mjs` return factory.createParenthesizedExpression(factory.createBinaryExpression(factory.createPropertyAccessExpression(factory.createMetaProperty(ts.SyntaxKind.ImportKeyword, factory.createIdentifier("meta")), factory.createIdentifier("url")), factory.createToken(ts.SyntaxKind.EqualsEqualsEqualsToken), factory.createCallExpression(factory.createPropertyAccessExpression(factory.createParenthesizedExpression(factory.createBinaryExpression(factory.createStringLiteral("file:///"), factory.createToken(ts.SyntaxKind.PlusToken), factory.createCallExpression(factory.createPropertyAccessExpression(factory.createElementAccessExpression(factory.createPropertyAccessExpression(factory.createIdentifier("process"), factory.createIdentifier("argv")), factory.createNumericLiteral("1")), factory.createIdentifier("replace")), undefined, [ factory.createRegularExpressionLiteral("/\\\\/g"), factory.createStringLiteral("/"), ]))), factory.createIdentifier("replace")), undefined, [ factory.createRegularExpressionLiteral("/\\/{3,}/"), factory.createStringLiteral("///"), ]))); } function getReplacementImportMetaResolve(args) { // Copy and pasted from ts-ast-viewer.com // new URL(specifier, import.meta.url).href return factory.createPropertyAccessExpression(factory.createNewExpression(factory.createIdentifier("URL"), undefined, [ ...args, factory.createPropertyAccessExpression(factory.createMetaProperty(ts.SyntaxKind.ImportKeyword, factory.createIdentifier("meta")), factory.createIdentifier("url")), ]), factory.createIdentifier("href")); } };