UNPKG

@sucrase/jest-plugin

Version:

Jest plugin for Sucrase

54 lines (46 loc) 1.96 kB
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); var _path = require('path'); var _sucrase = require('sucrase'); function getTransforms(filename, supportsStaticESM) { const extension = _path.extname.call(void 0, filename); const maybeImports = supportsStaticESM ? [] : ["imports"]; if ([".js", ".jsx", ".mjs", ".cjs"].includes(extension)) { return [...maybeImports, "flow", "jsx", "jest"]; } else if (extension === ".ts") { return [...maybeImports, "typescript", "jest"]; } else if ([".tsx", ".mts", ".cts"].includes(extension)) { return [...maybeImports, "typescript", "jsx", "jest"]; } return null; } // this is compatible to the one that is required by Jest, using the type from here: // https://github.com/mozilla/source-map/blob/0.6.1/source-map.d.ts#L6-L12 function process( src, filename, options, ) { const transforms = getTransforms(filename, options.supportsStaticESM); if (transforms !== null) { const {code, sourceMap} = _sucrase.transform.call(void 0, src, { transforms, disableESTransforms: true, preserveDynamicImport: options.supportsDynamicImport, ...options.transformerConfig, sourceMapOptions: {compiledFilename: filename}, filePath: filename, }); const mapBase64 = Buffer.from(JSON.stringify(sourceMap)).toString("base64"); // Split the source map comment across two strings so that tools like // source-map-support don't accidentally interpret it as a source map // comment for this file. let suffix = "//# sourceMapping"; suffix += `URL=data:application/json;charset=utf-8;base64,${mapBase64}`; // sourceMappingURL is necessary for breakpoints to work in WebStorm, so // include it in addition to specifying the source map normally. return {code: `${code}\n${suffix}`, map: sourceMap}; } else { return {code: src}; } } exports.process = process; exports. default = {process};