UNPKG

declapract

Version:

A tool to declaratively define best practices, maintainable evolve them, and scalably enforce them.

65 lines 3.34 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.importExportsFromFile = void 0; const ts_node_1 = require("ts-node"); const UnexpectedCodePathError_1 = require("../../logic/UnexpectedCodePathError"); const nodeModulesToTSNodeTranspileOnImport = []; const isExplicitImportToNodeModule = (filePath) => new RegExp(/node_modules\//).test(filePath); const extractNodeModuleNameFromFilePath = (filePath) => (new RegExp(/node_modules\/([\w-]+)\//).exec(filePath) ?? [])[1] ?? null; const addNodeModuleToListOfModulesToAllowTranspilation = ({ nodeModuleName, }) => { // check that this is not a redundant op if (nodeModulesToTSNodeTranspileOnImport.includes(nodeModuleName)) return; // do nothing if its already supported // add this module to the list, for future reference nodeModulesToTSNodeTranspileOnImport.push(nodeModuleName); // now update ts-node registration to include this as a supported module (0, ts_node_1.register)({ typeCheck: false, transpileOnly: true, files: true, ignore: nodeModulesToTSNodeTranspileOnImport.map((moduleName) => `/node_modules/(?!${moduleName})/`), skipProject: true, compilerOptions: { esModuleInterop: true, }, }); }; /** * runs `import(filePath)` on the file path, while also considering whether or not we need to update ts-node to support transpiling it (e.g., if its in `/node_modules/` dir) * * note: this fn keeps track of which node modules we've been asked to explicitly import from - and tells tsnode to parse them by adding them to the list of not ignored modules */ const importExportsFromFile = async ({ filePath, }) => { if (isExplicitImportToNodeModule(filePath)) { const nodeModuleName = extractNodeModuleNameFromFilePath(filePath); if (!nodeModuleName) throw new UnexpectedCodePathError_1.UnexpectedCodePathError('got a file path attempting to import a node module but could not extract node module name from it'); addNodeModuleToListOfModulesToAllowTranspilation({ nodeModuleName }); } return Promise.resolve(`${filePath}`).then(s => __importStar(require(s))); }; exports.importExportsFromFile = importExportsFromFile; //# sourceMappingURL=importExportsFromFile.js.map