diff --git a/README.md b/README.md index 924278e..e4393ca 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,7 @@ export interface Options { }, target?: string format?: string + jsx?: 'transform' | 'preserve' | 'automatic' } ``` @@ -48,7 +49,7 @@ export interface Options { loaders: { '.spec.ts': 'tsx' } - } + } ] } } diff --git a/jest.config.js b/jest.config.js index ecffdfb..8d3babd 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,12 +1,14 @@ const hq = require('alias-hq') module.exports = { + testEnvironment: 'jsdom', transform: { "\\.[jt]sx?$": [ 'esbuild-jest', { loaders: { '.spec.js': 'jsx', '.js': 'jsx' - } + }, + jsx: 'automatic' } ] }, diff --git a/package.json b/package.json index c479e78..004b062 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,7 @@ "alias-hq": "^5.1.6", "aria-build": "^0.7.3", "aria-fs": "^0.7.3", - "esbuild": "^0.8.49", + "esbuild": "^0.17.7", "jest": "^26.6.3", "jest-config": "^26.6.3", "prettier": "^2.2.1", diff --git a/src/index.ts b/src/index.ts index e2bca0f..547d96d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,7 +2,7 @@ import { extname } from 'path' import { Config } from '@jest/types' import { TransformOptions as JestTransformOptions, Transformer } from '@jest/transform' -import { Format, Loader, TransformOptions, transformSync } from 'esbuild' +import { Loader, TransformOptions, transformSync } from 'esbuild' import { Options } from './options' import { getExt, loaders } from './utils' @@ -42,11 +42,13 @@ const createTransformer = (options?: Options) => ({ const result = transformSync(sources.code, { loader, - format: options?.format as Format || 'cjs', + format: options?.format || 'cjs', target: options?.target || 'es2018', + jsx: options?.jsx, ...(options?.jsxFactory ? { jsxFactory: options.jsxFactory }: {}), ...(options?.jsxFragment ? { jsxFragment: options.jsxFragment }: {}), - ...sourcemaps + ...sourcemaps, + }) let { map, code } = result; @@ -58,7 +60,7 @@ const createTransformer = (options?: Options) => ({ // Append the inline sourcemap manually to ensure the "sourcesContent" // is null. Otherwise, breakpoints won't pause within the actual source. - code = code + '\n//# sourceMappingURL=data:application/json;base64,' + Buffer.from(JSON.stringify(map)).toString('base64') + code = code + `\n//# sourceMappingURL=data:application/json;base64,${Buffer.from(JSON.stringify(map)).toString('base64')}` } else { map = null } diff --git a/src/options.ts b/src/options.ts index 992e457..83ac020 100644 --- a/src/options.ts +++ b/src/options.ts @@ -1,12 +1,13 @@ -import { Loader } from 'esbuild' +import { Loader, CommonOptions } from 'esbuild' export interface Options { + jsx?: CommonOptions['jsx'] jsxFactory?: string jsxFragment?: string sourcemap?: boolean | 'inline' | 'external' loaders?: { [ext: string]: Loader }, - target?: string - format?: string + target?: CommonOptions['target'] + format?: CommonOptions['format'] } \ No newline at end of file diff --git a/tests/index.spec.ts b/tests/index.spec.ts index a3a7726..c78216a 100644 --- a/tests/index.spec.ts +++ b/tests/index.spec.ts @@ -32,10 +32,8 @@ test('ts file', () => { test('should have sourcemap with [jest.mock]', () => { const output = process('./examples/names-ts/index.spec.ts') - expect(output.code).toMatchInlineSnapshot(` - "\\"use strict\\"; - _getJestObj().mock(\\"./index\\", () => { + "_getJestObj().mock(\\"./index\\", () => { return { display() { return [\\"Joe\\"]; @@ -55,14 +53,14 @@ test('should have sourcemap with [jest.mock]', () => { (0, _globals.expect)((0, _index.display)()).toEqual([\\"Joe\\"]); }); - //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImluZGV4LnNwZWMudHMiXSwibWFwcGluZ3MiOiI7QUFJQSxjQUFLLEtBQUssV0FBVztBQUNuQixTQUFPO0lBQ0w7QUFDRSxhQUFPLENBQUU7Ozs7QUFQZixJQUFBLFdBQUEsUUFBQTtBQUVBLElBQUEsU0FBQSxRQUFBOzs7Ozs7OztBQVVBLEtBQUssaUNBQWlDO0FBQ3BDLEVBQUEsSUFBQSxTQUFBLFFBQU8sSUFBQSxPQUFBLFlBQVcsUUFBUSxDQUFFOzsiLCJuYW1lcyI6W10sInNvdXJjZXNDb250ZW50IjpudWxsfQ==" + //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImluZGV4LnNwZWMudHMiXSwibWFwcGluZ3MiOiJBQUlBLFlBQUEsRUFBS0EsS0FBSyxXQUFXLE1BQU07QUFDekIsU0FBTztJQUNMQyxVQUFVO0FBQ1IsYUFBTyxDQUFFLEtBQUY7SUFDUjtFQUhJO0FBS1IsQ0FORDtBQUpBLElBQUEsV0FBQSxRQUFBLGVBQUE7QUFFQSxJQUFBLFNBQUEsUUFBQSxTQUFBOzs7Ozs7OztBQVVBQyxLQUFLLGlDQUFpQyxNQUFNO0FBQzFDLEdBQUEsR0FBQSxTQUFBLFNBQU8sR0FBQSxPQUFBLFNBQUEsQ0FBUCxFQUFrQkMsUUFBUSxDQUFFLEtBQUYsQ0FBMUI7QUFDRCxDQUZHOyIsIm5hbWVzIjpbIm1vY2siLCJkaXNwbGF5IiwidGVzdCIsInRvRXF1YWwiXSwic291cmNlc0NvbnRlbnQiOm51bGx9" `) expect(output.map).toEqual( { version: 3, sources: [ 'index.spec.ts' ], - mappings: ';AAIA,cAAK,KAAK,WAAW;AACnB,SAAO;IACL;AACE,aAAO,CAAE;;;;AAPf,IAAA,WAAA,QAAA;AAEA,IAAA,SAAA,QAAA;;;;;;;;AAUA,KAAK,iCAAiC;AACpC,EAAA,IAAA,SAAA,QAAO,IAAA,OAAA,YAAW,QAAQ,CAAE;;', - names: [], + mappings: 'AAIA,YAAA,EAAKA,KAAK,WAAW,MAAM;AACzB,SAAO;IACLC,UAAU;AACR,aAAO,CAAE,KAAF;IACR;EAHI;AAKR,CAND;AAJA,IAAA,WAAA,QAAA,eAAA;AAEA,IAAA,SAAA,QAAA,SAAA;;;;;;;;AAUAC,KAAK,iCAAiC,MAAM;AAC1C,GAAA,GAAA,SAAA,SAAO,GAAA,OAAA,SAAA,CAAP,EAAkBC,QAAQ,CAAE,KAAF,CAA1B;AACD,CAFG;', + names: ['mock', 'display', 'test', 'toEqual'], sourcesContent: null }) }) @@ -79,13 +77,13 @@ test('should have sourcemap without [jest.mock]', () => { display }; - //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4vZXhhbXBsZXMvbmFtZXMtdHMvaW5kZXgudHMiXSwibWFwcGluZ3MiOiJBQUFBO0FBRU87QUFDTCxTQUFPO0FBQUE7IiwibmFtZXMiOltdLCJzb3VyY2VzQ29udGVudCI6bnVsbH0=" + //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4vZXhhbXBsZXMvbmFtZXMtdHMvaW5kZXgudHMiXSwibWFwcGluZ3MiOiJBQUFBLE9BQU8sV0FBVztBQUVYLFNBQVMsVUFBVTtBQUN4QixTQUFPO0FBQ1Q7IiwibmFtZXMiOltdLCJzb3VyY2VzQ29udGVudCI6bnVsbH0=" `) expect(output.map).toEqual({ version: 3, sources: [ './examples/names-ts/index.ts' ], - mappings: 'AAAA;AAEO;AACL,SAAO;AAAA;', + mappings: 'AAAA,OAAO,WAAW;AAEX,SAAS,UAAU;AACxB,SAAO;AACT;', names: [], sourcesContent: null }) diff --git a/tests/react-jsx.spec.jsx b/tests/react-jsx.spec.jsx index 30f207b..d9a2b9b 100644 --- a/tests/react-jsx.spec.jsx +++ b/tests/react-jsx.spec.jsx @@ -1,5 +1,4 @@ import { afterEach, beforeEach, expect, it } from "@jest/globals" -import * as React from "react" import { render } from "react-dom" import App from '../examples/react-jsx/App' diff --git a/yarn.lock b/yarn.lock index 3ba4dd8..55f973c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -562,6 +562,116 @@ exec-sh "^0.3.2" minimist "^1.2.0" +"@esbuild/android-arm64@0.17.7": + version "0.17.7" + resolved "https://registry.yarnpkg.com/@esbuild/android-arm64/-/android-arm64-0.17.7.tgz#7d22b442815624423de5541545401e12a8d474d8" + integrity sha512-fOUBZvcbtbQJIj2K/LMKcjULGfXLV9R4qjXFsi3UuqFhIRJHz0Fp6kFjsMFI6vLuPrfC5G9Dmh+3RZOrSKY2Lg== + +"@esbuild/android-arm@0.17.7": + version "0.17.7" + resolved "https://registry.yarnpkg.com/@esbuild/android-arm/-/android-arm-0.17.7.tgz#fa30de0cfae8e8416c693dc449c415765542483b" + integrity sha512-Np6Lg8VUiuzHP5XvHU7zfSVPN4ILdiOhxA1GQ1uvCK2T2l3nI8igQV0c9FJx4hTkq8WGqhGEvn5UuRH8jMkExg== + +"@esbuild/android-x64@0.17.7": + version "0.17.7" + resolved "https://registry.yarnpkg.com/@esbuild/android-x64/-/android-x64-0.17.7.tgz#34a1af914510ec821246859f8ae7d8fe843dd37b" + integrity sha512-6YILpPvop1rPAvaO/n2iWQL45RyTVTR/1SK7P6Xi2fyu+hpEeX22fE2U2oJd1sfpovUJOWTRdugjddX6QCup3A== + +"@esbuild/darwin-arm64@0.17.7": + version "0.17.7" + resolved "https://registry.yarnpkg.com/@esbuild/darwin-arm64/-/darwin-arm64-0.17.7.tgz#06712059a30a6130eef701fb634883a4aaea02f7" + integrity sha512-7i0gfFsDt1BBiurZz5oZIpzfxqy5QkJmhXdtrf2Hma/gI9vL2AqxHhRBoI1NeWc9IhN1qOzWZrslhiXZweMSFg== + +"@esbuild/darwin-x64@0.17.7": + version "0.17.7" + resolved "https://registry.yarnpkg.com/@esbuild/darwin-x64/-/darwin-x64-0.17.7.tgz#58cd69d00d5b9847ad2015858a7ec3f10bf309ad" + integrity sha512-hRvIu3vuVIcv4SJXEKOHVsNssM5tLE2xWdb9ZyJqsgYp+onRa5El3VJ4+WjTbkf/A2FD5wuMIbO2FCTV39LE0w== + +"@esbuild/freebsd-arm64@0.17.7": + version "0.17.7" + resolved "https://registry.yarnpkg.com/@esbuild/freebsd-arm64/-/freebsd-arm64-0.17.7.tgz#1dd3de24a9683c8321a4e3c42b11b32a48e791d4" + integrity sha512-2NJjeQ9kiabJkVXLM3sHkySqkL1KY8BeyLams3ITyiLW10IwDL0msU5Lq1cULCn9zNxt1Seh1I6QrqyHUvOtQw== + +"@esbuild/freebsd-x64@0.17.7": + version "0.17.7" + resolved "https://registry.yarnpkg.com/@esbuild/freebsd-x64/-/freebsd-x64-0.17.7.tgz#b0e409e1c7cc05412c8dd149c2c39e0a1dee9567" + integrity sha512-8kSxlbjuLYMoIgvRxPybirHJeW45dflyIgHVs+jzMYJf87QOay1ZUTzKjNL3vqHQjmkSn8p6KDfHVrztn7Rprw== + +"@esbuild/linux-arm64@0.17.7": + version "0.17.7" + resolved "https://registry.yarnpkg.com/@esbuild/linux-arm64/-/linux-arm64-0.17.7.tgz#35cfae28e460b96ccc027eccc28b13c0712d6df3" + integrity sha512-43Bbhq3Ia/mGFTCRA4NlY8VRH3dLQltJ4cqzhSfq+cdvdm9nKJXVh4NUkJvdZgEZIkf/ufeMmJ0/22v9btXTcw== + +"@esbuild/linux-arm@0.17.7": + version "0.17.7" + resolved "https://registry.yarnpkg.com/@esbuild/linux-arm/-/linux-arm-0.17.7.tgz#a378301c253ef64d19a112c9ec922680c2fb5a71" + integrity sha512-07RsAAzznWqdfJC+h3L2UVWwnUHepsFw5GmzySnUspHHb7glJ1+47rvlcH0SeUtoVOs8hF4/THgZbtJRyALaJA== + +"@esbuild/linux-ia32@0.17.7": + version "0.17.7" + resolved "https://registry.yarnpkg.com/@esbuild/linux-ia32/-/linux-ia32-0.17.7.tgz#7d36087db95b1faaee8df203c511775a4d322a2b" + integrity sha512-ViYkfcfnbwOoTS7xE4DvYFv7QOlW8kPBuccc4erJ0jx2mXDPR7e0lYOH9JelotS9qe8uJ0s2i3UjUvjunEp53A== + +"@esbuild/linux-loong64@0.17.7": + version "0.17.7" + resolved "https://registry.yarnpkg.com/@esbuild/linux-loong64/-/linux-loong64-0.17.7.tgz#b989253520308d81ee0e4846de9f63f2f11c7f10" + integrity sha512-H1g+AwwcqYQ/Hl/sMcopRcNLY/fysIb/ksDfCa3/kOaHQNhBrLeDYw+88VAFV5U6oJL9GqnmUj72m9Nv3th3hA== + +"@esbuild/linux-mips64el@0.17.7": + version "0.17.7" + resolved "https://registry.yarnpkg.com/@esbuild/linux-mips64el/-/linux-mips64el-0.17.7.tgz#ae751365cdf967dfa89dd59cdb0dcc8723a66f9a" + integrity sha512-MDLGrVbTGYtmldlbcxfeDPdhxttUmWoX3ovk9u6jc8iM+ueBAFlaXKuUMCoyP/zfOJb+KElB61eSdBPSvNcCEg== + +"@esbuild/linux-ppc64@0.17.7": + version "0.17.7" + resolved "https://registry.yarnpkg.com/@esbuild/linux-ppc64/-/linux-ppc64-0.17.7.tgz#ad1c9299c463f0409e57166e76e91afb6193ea9f" + integrity sha512-UWtLhRPKzI+v2bKk4j9rBpGyXbLAXLCOeqt1tLVAt1mfagHpFjUzzIHCpPiUfY3x1xY5e45/+BWzGpqqvSglNw== + +"@esbuild/linux-riscv64@0.17.7": + version "0.17.7" + resolved "https://registry.yarnpkg.com/@esbuild/linux-riscv64/-/linux-riscv64-0.17.7.tgz#84acb7451bef7458e6067d9c358026ffa1831910" + integrity sha512-3C/RTKqZauUwBYtIQAv7ELTJd+H2dNKPyzwE2ZTbz2RNrNhNHRoeKnG5C++eM6nSZWUCLyyaWfq1v1YRwBS/+A== + +"@esbuild/linux-s390x@0.17.7": + version "0.17.7" + resolved "https://registry.yarnpkg.com/@esbuild/linux-s390x/-/linux-s390x-0.17.7.tgz#0bf23c78c52ea60ae4ea95239b728683a86a7ab8" + integrity sha512-x7cuRSCm998KFZqGEtSo8rI5hXLxWji4znZkBhg2FPF8A8lxLLCsSXe2P5utf0RBQflb3K97dkEH/BJwTqrbDw== + +"@esbuild/linux-x64@0.17.7": + version "0.17.7" + resolved "https://registry.yarnpkg.com/@esbuild/linux-x64/-/linux-x64-0.17.7.tgz#932d8c6e1b0d6a57a4e94a8390dfebeebba21dcc" + integrity sha512-1Z2BtWgM0Wc92WWiZR5kZ5eC+IetI++X+nf9NMbUvVymt74fnQqwgM5btlTW7P5uCHfq03u5MWHjIZa4o+TnXQ== + +"@esbuild/netbsd-x64@0.17.7": + version "0.17.7" + resolved "https://registry.yarnpkg.com/@esbuild/netbsd-x64/-/netbsd-x64-0.17.7.tgz#6aa81873c6e08aa419378e07c8d3eed5aa77bf25" + integrity sha512-//VShPN4hgbmkDjYNCZermIhj8ORqoPNmAnwSPqPtBB0xOpHrXMlJhsqLNsgoBm0zi/5tmy//WyL6g81Uq2c6Q== + +"@esbuild/openbsd-x64@0.17.7": + version "0.17.7" + resolved "https://registry.yarnpkg.com/@esbuild/openbsd-x64/-/openbsd-x64-0.17.7.tgz#0698f260250a7022e2cae7385cbd09a86eb0967c" + integrity sha512-IQ8BliXHiOsbQEOHzc7mVLIw2UYPpbOXJQ9cK1nClNYQjZthvfiA6rWZMz4BZpVzHZJ+/H2H23cZwRJ1NPYOGg== + +"@esbuild/sunos-x64@0.17.7": + version "0.17.7" + resolved "https://registry.yarnpkg.com/@esbuild/sunos-x64/-/sunos-x64-0.17.7.tgz#ef97445672deec50e3b3549af2ee6d42fbc04250" + integrity sha512-phO5HvU3SyURmcW6dfQXX4UEkFREUwaoiTgi1xH+CAFKPGsrcG6oDp1U70yQf5lxRKujoSCEIoBr0uFykJzN2g== + +"@esbuild/win32-arm64@0.17.7": + version "0.17.7" + resolved "https://registry.yarnpkg.com/@esbuild/win32-arm64/-/win32-arm64-0.17.7.tgz#70865d2332d7883e2e49077770adfe51c51343e3" + integrity sha512-G/cRKlYrwp1B0uvzEdnFPJ3A6zSWjnsRrWivsEW0IEHZk+czv0Bmiwa51RncruHLjQ4fGsvlYPmCmwzmutPzHA== + +"@esbuild/win32-ia32@0.17.7": + version "0.17.7" + resolved "https://registry.yarnpkg.com/@esbuild/win32-ia32/-/win32-ia32-0.17.7.tgz#39831b787013c7da7e61c8eb6a9df0fed9bd0fcb" + integrity sha512-/yMNVlMew07NrOflJdRAZcMdUoYTOCPbCHx0eHtg55l87wXeuhvYOPBQy5HLX31Ku+W2XsBD5HnjUjEUsTXJug== + +"@esbuild/win32-x64@0.17.7": + version "0.17.7" + resolved "https://registry.yarnpkg.com/@esbuild/win32-x64/-/win32-x64-0.17.7.tgz#03b231fcfa0702562978979468dfc8b09b55ac59" + integrity sha512-K9/YybM6WZO71x73Iyab6mwieHtHjm9hrPR/a9FBPZmFO3w+fJaM2uu2rt3JYf/rZR24MFwTliI8VSoKKOtYtg== + "@istanbuljs/load-nyc-config@^1.0.0": version "1.1.0" resolved "https://registry.yarnpkg.com/@istanbuljs/load-nyc-config/-/load-nyc-config-1.1.0.tgz#fd3db1d59ecf7cf121e80650bb86712f9b55eced" @@ -1868,10 +1978,33 @@ error-ex@^1.3.1: dependencies: is-arrayish "^0.2.1" -esbuild@^0.8.49: - version "0.8.49" - resolved "https://registry.yarnpkg.com/esbuild/-/esbuild-0.8.49.tgz#3d33f71b3966611f822cf4c838115f3fbd16def2" - integrity sha512-itiFVYv5UZz4NooO7/Y0bRGVDGz/M/cxKbl6zyNI5pnKaz1mZjvZXAFhhDVz6rGCmcdTKj5oag6rh8DaaSSmfQ== +esbuild@^0.17.7: + version "0.17.7" + resolved "https://registry.yarnpkg.com/esbuild/-/esbuild-0.17.7.tgz#a7ace55f2bf82fdb1c9013a924620ce2596984fa" + integrity sha512-+5hHlrK108fT6C6/40juy0w4DYKtyZ5NjfBlTccBdsFutR7WBxpIY633JzZJewdsCy8xWA/u2z0MSniIJwufYg== + optionalDependencies: + "@esbuild/android-arm" "0.17.7" + "@esbuild/android-arm64" "0.17.7" + "@esbuild/android-x64" "0.17.7" + "@esbuild/darwin-arm64" "0.17.7" + "@esbuild/darwin-x64" "0.17.7" + "@esbuild/freebsd-arm64" "0.17.7" + "@esbuild/freebsd-x64" "0.17.7" + "@esbuild/linux-arm" "0.17.7" + "@esbuild/linux-arm64" "0.17.7" + "@esbuild/linux-ia32" "0.17.7" + "@esbuild/linux-loong64" "0.17.7" + "@esbuild/linux-mips64el" "0.17.7" + "@esbuild/linux-ppc64" "0.17.7" + "@esbuild/linux-riscv64" "0.17.7" + "@esbuild/linux-s390x" "0.17.7" + "@esbuild/linux-x64" "0.17.7" + "@esbuild/netbsd-x64" "0.17.7" + "@esbuild/openbsd-x64" "0.17.7" + "@esbuild/sunos-x64" "0.17.7" + "@esbuild/win32-arm64" "0.17.7" + "@esbuild/win32-ia32" "0.17.7" + "@esbuild/win32-x64" "0.17.7" esbuild@^0.8.53: version "0.8.56"