@oazmi/build-tools
Version:
general deno build tool scripts which I practically use in all of my typescript repos
39 lines (38 loc) • 1.93 kB
JavaScript
// Copyright 2018-2024 the Deno authors. MIT license.
import { ts } from "../../../../@ts-morph/bootstrap/0.25.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`
if (ts.isMetaProperty(node)) {
if (isScriptModule) {
return getReplacementImportMetaScript();
}
else {
return getReplacementImportMetaEsm();
}
}
return ts.visitEachChild(node, visitNode, context);
}
function getReplacementImportMeta(symbolFor, argumentsArray) {
// Copy and pasted from ts-ast-viewer.com
// globalThis[Symbol.for('import-meta-ponyfill')](...args)
return factory.createCallExpression(factory.createElementAccessExpression(factory.createIdentifier("globalThis"), factory.createCallExpression(factory.createPropertyAccessExpression(factory.createIdentifier("Symbol"), factory.createIdentifier("for")), undefined, [factory.createStringLiteral(symbolFor)])), undefined, argumentsArray);
}
function getReplacementImportMetaScript() {
return getReplacementImportMeta("import-meta-ponyfill-commonjs", [
factory.createIdentifier("require"),
factory.createIdentifier("module"),
]);
}
function getReplacementImportMetaEsm() {
return getReplacementImportMeta("import-meta-ponyfill-esmodule", [
factory.createMetaProperty(ts.SyntaxKind.ImportKeyword, factory.createIdentifier("meta")),
]);
}
};