@jakesidsmith/tsb
Version:
Dead simple TypeScript bundler, watcher, dev server, transpiler, and polyfiller
115 lines • 5.45 kB
JavaScript
;
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.getTsconfig = exports.resolveTsconfig = exports.resolveTsconfigPath = void 0;
const yup = __importStar(require("yup"));
const path = __importStar(require("path"));
const fs = __importStar(require("fs"));
const ts = __importStar(require("typescript"));
const logger = __importStar(require("./logger"));
const constants_1 = require("./constants");
const utils_1 = require("./utils");
const TSCONFIG_VALIDATOR = yup
.object()
.shape({
extends: yup.string().optional(),
include: yup.array().of(yup.string().required()).optional(),
compilerOptions: yup.lazy((value) => {
if (value) {
return yup.object().shape({
jsx: yup.mixed().oneOf(constants_1.VALID_JSX_OPTIONS).optional(),
sourceMap: yup.boolean().optional(),
module: yup.string().optional(),
});
}
return yup.mixed().optional();
}),
})
.required();
const resolveTsconfigPath = (root, tsconfigPath) => {
const resolvedTsconfigPath = path.resolve(root, tsconfigPath);
const fullTsconfigPath = fs.existsSync(resolvedTsconfigPath) &&
fs.lstatSync(resolvedTsconfigPath).isDirectory()
? path.resolve(resolvedTsconfigPath, 'tsconfig.json')
: resolvedTsconfigPath;
if (!fs.existsSync(fullTsconfigPath)) {
logger.error(`Could not resolve tsconfig.json at "${fullTsconfigPath}"`);
return process.exit(1);
}
return fullTsconfigPath;
};
exports.resolveTsconfigPath = resolveTsconfigPath;
const resolveTsconfig = (tsconfigPath) => {
var _a, _b;
const tsconfigDir = path.dirname(tsconfigPath);
const tsconfig = ts.readConfigFile(tsconfigPath, ts.sys.readFile);
if (tsconfig.error) {
logger.error(ts.flattenDiagnosticMessageText(tsconfig.error.messageText, '\n'));
logger.error(`Error reading tsconfig.json at "${tsconfigPath}"`);
return process.exit(1);
}
const validTsconfig = tsconfig.config;
try {
TSCONFIG_VALIDATOR.validateSync(validTsconfig);
}
catch (error) {
logger.error((0, utils_1.getErrorMessages)(error));
logger.error(`Invalid tsconfig.json at "${tsconfigPath}"`);
return process.exit(1);
}
const extendedPath = validTsconfig.extends
? (0, exports.resolveTsconfigPath)(tsconfigDir, validTsconfig.extends)
: null;
if (extendedPath === tsconfigPath) {
logger.error(`Invalid tsconfig.json at "${tsconfigPath}" - cannot extend itself`);
return process.exit(1);
}
const extended = extendedPath ? (0, exports.resolveTsconfig)(extendedPath) : null;
return {
raw: validTsconfig,
resolved: Object.assign(Object.assign(Object.assign({}, extended === null || extended === void 0 ? void 0 : extended.raw), validTsconfig), { include: (_b = (_a = validTsconfig.include) === null || _a === void 0 ? void 0 : _a.map((include) => path.resolve(tsconfigDir, include))) !== null && _b !== void 0 ? _b : extended === null || extended === void 0 ? void 0 : extended.resolved.include, compilerOptions: Object.assign(Object.assign({}, extended === null || extended === void 0 ? void 0 : extended.raw.compilerOptions), validTsconfig.compilerOptions) }),
};
};
exports.resolveTsconfig = resolveTsconfig;
const getTsconfig = (configPath) => {
var _a, _b, _c;
const { resolved } = (0, exports.resolveTsconfig)(configPath);
if (!((_a = resolved.compilerOptions) === null || _a === void 0 ? void 0 : _a.sourceMap)) {
logger.warn('No sourceMap enabled in tsconfig.json - source maps will not be generated');
}
if (!((_b = resolved.include) === null || _b === void 0 ? void 0 : _b.length)) {
logger.error('No files in tsconfig.json include option - specify some files to parse');
return process.exit(1);
}
if (((_c = resolved.compilerOptions) === null || _c === void 0 ? void 0 : _c.module) &&
!resolved.compilerOptions.module.toLowerCase().startsWith('es')) {
logger.warn(`Your tsconfig.json module was set to "${resolved.compilerOptions.module}".
You should target an ES module type e.g. "ESNext" to get the full benefits of this bundler.
We'll handle converting everything to CommonJS for you.`);
}
return resolved;
};
exports.getTsconfig = getTsconfig;
//# sourceMappingURL=tsconfig.js.map