UNPKG

@twstyled/babel-preset

Version:

Babel plugin for twstyled -- the full-featured Tailwind CSS + CSS in JS Compiler

125 lines (124 loc) 4.96 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (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; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getConfigPaths = exports.FatalTypeScriptError = void 0; const fs_1 = require("fs"); const path = __importStar(require("path")); const os = __importStar(require("os")); const chalk_1 = __importDefault(require("chalk")); const json5_1 = require("json5"); const code_frame_1 = require("@babel/code-frame"); class FatalTypeScriptError extends Error { } exports.FatalTypeScriptError = FatalTypeScriptError; function getConfigPaths() { var _a, _b; const dir = process.cwd(); let typeScriptPath; try { typeScriptPath = require.resolve('typescript', { paths: [dir] }); } catch (_) { /** noop */ } const tsConfigPath = path.join(dir, 'tsconfig.json'); const useTypeScript = Boolean(typeScriptPath && fs_1.existsSync(tsConfigPath)); let jsConfig; if (useTypeScript) { const ts = require(typeScriptPath); const tsConfig = getTypescriptConfig(ts, tsConfigPath); jsConfig = { compilerOptions: tsConfig.options }; } else { const jsConfigPath = path.join(dir, 'jsconfig.json'); if (fs_1.existsSync(jsConfigPath)) { jsConfig = parseJsonFile(jsConfigPath); } } let baseUrl, paths; if ((_a = jsConfig === null || jsConfig === void 0 ? void 0 : jsConfig.compilerOptions) === null || _a === void 0 ? void 0 : _a.baseUrl) { baseUrl = jsConfig.compilerOptions.baseUrl; } else { baseUrl = dir; } if ((_b = jsConfig === null || jsConfig === void 0 ? void 0 : jsConfig.compilerOptions) === null || _b === void 0 ? void 0 : _b.paths) { paths = jsConfig.compilerOptions.paths; } else { paths = {}; } return { baseUrl: path.isAbsolute(baseUrl) ? path.relative(dir, baseUrl) : baseUrl, paths }; } exports.getConfigPaths = getConfigPaths; function parseJsonFile(filePath) { const contents = fs_1.readFileSync(filePath, 'utf8'); // Special case an empty file if (contents.trim() === '') { return {}; } try { return json5_1.parse(contents); } catch (err) { const codeFrame = code_frame_1.codeFrameColumns(String(contents), { start: { line: err.lineNumber, column: err.columnNumber } }, { message: err.message, highlightCode: true }); throw new Error(`Failed to parse "${filePath}":\n${codeFrame}`); } } function getTypescriptConfig(ts, tsConfigPath) { var _a, _b; try { const formatDiagnosticsHost = { getCanonicalFileName: (fileName) => fileName, getCurrentDirectory: ts.sys.getCurrentDirectory, getNewLine: () => os.EOL }; const { config, error } = ts.readConfigFile(tsConfigPath, ts.sys.readFile); if (error) { throw new FatalTypeScriptError(ts.formatDiagnostic(error, formatDiagnosticsHost)); } const result = ts.parseJsonConfigFileContent(config, ts.sys, path.dirname(tsConfigPath)); if (result.errors) { result.errors = result.errors.filter(({ code }) => // No inputs were found in config file code !== 18003); } if ((_a = result.errors) === null || _a === void 0 ? void 0 : _a.length) { throw new FatalTypeScriptError(ts.formatDiagnostic(result.errors[0], formatDiagnosticsHost)); } return result; } catch (err) { if ((err === null || err === void 0 ? void 0 : err.name) === 'SyntaxError') { const reason = '\n' + ((_b = err === null || err === void 0 ? void 0 : err.message) !== null && _b !== void 0 ? _b : ''); throw new FatalTypeScriptError(chalk_1.default.red.bold('Could not parse', chalk_1.default.cyan('tsconfig.json') + '.' + ' Please make sure it contains syntactically correct JSON.') + reason); } throw err; } }