addons/isl/jest-transformer-import-meta.cjsblame
View source
b69ab311/**
b69ab312 * Copyright (c) Meta Platforms, Inc. and affiliates.
b69ab313 *
b69ab314 * This source code is licensed under the MIT license found in the
b69ab315 * LICENSE file in the root directory of this source tree.
b69ab316 */
b69ab317
b69ab318const {default: tsJest} = require('ts-jest');
b69ab319
b69ab3110const transformer = tsJest.createTransformer({diagnostics: false});
b69ab3111
b69ab3112/**
b69ab3113 * Replace 'import.meta.hot' with 'undefined' to make Jest happy.
b69ab3114 * Replace 'import.meta.url' with a require filename to enable WebWorkers.
b69ab3115 * Then delegates to the ts-jest transformer.
b69ab3116 *
b69ab3117 * For simplicity, we just do a naive string replace without complex parsing.
b69ab3118 */
b69ab3119function process(sourceText, path, options) {
b69ab3120 const newSourceText = sourceText
b69ab3121 .replace(/import\.meta\.hot/g, 'undefined')
b69ab3122 .replace(/import\.meta\.url/g, `require('url').pathToFileURL(__filename).toString()`);
b69ab3123 return transformer.process(newSourceText, path, options);
b69ab3124}
b69ab3125
b69ab3126module.exports = {process};