From f407f8846bd23a9581d17948ceae8d33943ce1bd Mon Sep 17 00:00:00 2001 From: Alec Larson <1925840+aleclarson@users.noreply.github.com> Date: Tue, 4 May 2021 14:37:46 -0400 Subject: [PATCH] fix(jest-27): replace `export const` with `export default` This issue was caused by https://github.com/facebook/jest/pull/11193 --- src/index.ts | 84 +++++++++++++++++++++++++++------------------------- 1 file changed, 43 insertions(+), 41 deletions(-) diff --git a/src/index.ts b/src/index.ts index 8fcc45c..e41d3df 100644 --- a/src/index.ts +++ b/src/index.ts @@ -10,47 +10,49 @@ export interface Options { target?: string } -export const createTransformer = (options: Options | null) => { - if (!options) options = {} - const { loaders = {} } = options - delete options.loaders - return { - process(content: string, filename: string) { - const ext = getExt(filename) - const strict = /^(im|ex)port /m.test(content) - const result = transformSync(content, { - loader: loaders[ext] || (extname(filename).slice(1) as Loader), - format: 'cjs', - target: 'es2018', - sourcemap: 'external', - sourcefile: filename, - ...options, - // Esbuild does not enable strict mode when compiling ES modules to - // CommonJS format: https://github.com/evanw/esbuild/issues/422#issuecomment-739740602 - banner: strict ? '"use strict";' : undefined, - }) - if (/\bjest\.mock\b/.test(result.code)) { - const { nebu } = require('nebu') as typeof import('nebu') - result.code = nebu.process(result.code, { - plugins: [hoistJestMock], - state: { strict }, - }).js - } - const map = { - ...JSON.parse(result.map), - sourcesContent: null, - } - // Append the inline sourcemap manually to ensure the "sourcesContent" - // is null. Otherwise, breakpoints won't pause within the actual source. - return { - code: - result.code + - '\n//# sourceMappingURL=data:application/json;base64,' + - Buffer.from(JSON.stringify(map)).toString('base64'), - map, - } - }, - } +export default { + createTransformer(options: Options | null) { + if (!options) options = {} + const { loaders = {} } = options + delete options.loaders + return { + process(content: string, filename: string) { + const ext = getExt(filename) + const strict = /^(im|ex)port /m.test(content) + const result = transformSync(content, { + loader: loaders[ext] || (extname(filename).slice(1) as Loader), + format: 'cjs', + target: 'es2018', + sourcemap: 'external', + sourcefile: filename, + ...options, + // Esbuild does not enable strict mode when compiling ES modules to + // CommonJS format: https://github.com/evanw/esbuild/issues/422#issuecomment-739740602 + banner: strict ? '"use strict";' : undefined, + }) + if (/\bjest\.mock\b/.test(result.code)) { + const { nebu } = require('nebu') as typeof import('nebu') + result.code = nebu.process(result.code, { + plugins: [hoistJestMock], + state: { strict }, + }).js + } + const map = { + ...JSON.parse(result.map), + sourcesContent: null, + } + // Append the inline sourcemap manually to ensure the "sourcesContent" + // is null. Otherwise, breakpoints won't pause within the actual source. + return { + code: + result.code + + '\n//# sourceMappingURL=data:application/json;base64,' + + Buffer.from(JSON.stringify(map)).toString('base64'), + map, + } + }, + } + }, } function getExt(str: string) {