Socket
Socket
Sign inDemoInstall

esbuild-jest

Package Overview
Dependencies
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

esbuild-jest - npm Package Compare versions

Comparing version 0.4.0 to 0.5.0-beta.1

12

esbuild-jest.d.ts

@@ -0,1 +1,2 @@

import { Transformer } from '@jest/transform';
import { Loader } from 'esbuild';

@@ -13,9 +14,6 @@

}
declare const createTransformer: (options?: Options) => {
process(content: string, filename: string): {
code: string;
map: string;
};
};
export { Options, createTransformer };
declare const transformer: Pick<Transformer, 'canInstrument' | 'createTransformer'>;
export default transformer;
export { Options };

@@ -0,4 +1,29 @@

import path, { extname } from 'path';
import { transformSync } from 'esbuild';
import path, { extname } from 'path';
import babelJest from 'babel-jest';
const { process } = babelJest.createTransformer({
plugins: [
"@babel/plugin-transform-modules-commonjs"
],
parserOpts: {
plugins: [
"jsx",
"typescript"
]
}
});
function babelTransform(opts) {
const { sourceText , sourcePath , config , options } = opts;
const babelResult = process(sourceText, sourcePath, config, options);
return babelResult.code;
}
const loaders = [
"js",
"jsx",
"ts",
"tsx",
"json"
];
const getExt = (str)=>{

@@ -8,11 +33,15 @@ const basename = path.basename(str);

const lastDot = basename.lastIndexOf('.');
const extname1 = path.extname(basename).replace(/(\.[a-z0-9]+).*/i, '$1');
if (firstDot === lastDot) return extname1;
return basename.slice(firstDot, lastDot) + extname1;
const extname = path.extname(basename).replace(/(\.[a-z0-9]+).*/i, '$1');
if (firstDot === lastDot) return extname;
return basename.slice(firstDot, lastDot) + extname;
};
const createTransformer = (options)=>({
process (content, filename) {
process (content, filename, config, opts) {
const sources = {
code: content
};
const ext = getExt(filename), extName = extname(filename).slice(1);
const enableSourcemaps = (options === null || options === void 0 ? void 0 : options.sourcemap) || false;
const ext = getExt(filename);
const loader = (options === null || options === void 0 ? void 0 : options.loaders) && (options === null || options === void 0 ? void 0 : options.loaders[ext]) ? options.loaders[ext] : extname(filename).slice(1);
const loader = (options === null || options === void 0 ? void 0 : options.loaders) && (options === null || options === void 0 ? void 0 : options.loaders[ext]) ? options.loaders[ext] : loaders.includes(extName) ? extName : 'text';
const sourcemaps = enableSourcemaps ? {

@@ -24,3 +53,16 @@ sourcemap: true,

};
const result = transformSync(content, {
/// this logic or code from
/// https://github.com/threepointone/esjest-transform/blob/main/src/index.js
/// this will supoort the jest.mock
/// https://github.com/aelbore/esbuild-jest/issues/12
if (sources.code.indexOf("ock(") >= 0 || (opts === null || opts === void 0 ? void 0 : opts.instrument)) {
const source = babelTransform({
sourceText: content,
sourcePath: filename,
config,
options: opts
});
sources.code = source;
}
const result = transformSync(sources.code, {
loader,

@@ -58,3 +100,7 @@ format: (options === null || options === void 0 ? void 0 : options.format) || 'cjs',

;
const transformer = {
canInstrument: true,
createTransformer
};
export { createTransformer };
export default transformer;
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var path = require('path');
var esbuild = require('esbuild');
var path = require('path');
var babelJest = require('babel-jest');

@@ -11,3 +10,28 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
var babelJest__default = /*#__PURE__*/_interopDefaultLegacy(babelJest);
const { process } = babelJest__default['default'].createTransformer({
plugins: [
"@babel/plugin-transform-modules-commonjs"
],
parserOpts: {
plugins: [
"jsx",
"typescript"
]
}
});
function babelTransform(opts) {
const { sourceText , sourcePath , config , options } = opts;
const babelResult = process(sourceText, sourcePath, config, options);
return babelResult.code;
}
const loaders = [
"js",
"jsx",
"ts",
"tsx",
"json"
];
const getExt = (str)=>{

@@ -17,11 +41,15 @@ const basename = path__default['default'].basename(str);

const lastDot = basename.lastIndexOf('.');
const extname1 = path__default['default'].extname(basename).replace(/(\.[a-z0-9]+).*/i, '$1');
if (firstDot === lastDot) return extname1;
return basename.slice(firstDot, lastDot) + extname1;
const extname = path__default['default'].extname(basename).replace(/(\.[a-z0-9]+).*/i, '$1');
if (firstDot === lastDot) return extname;
return basename.slice(firstDot, lastDot) + extname;
};
const createTransformer = (options)=>({
process (content, filename) {
process (content, filename, config, opts) {
const sources = {
code: content
};
const ext = getExt(filename), extName = path.extname(filename).slice(1);
const enableSourcemaps = (options === null || options === void 0 ? void 0 : options.sourcemap) || false;
const ext = getExt(filename);
const loader = (options === null || options === void 0 ? void 0 : options.loaders) && (options === null || options === void 0 ? void 0 : options.loaders[ext]) ? options.loaders[ext] : path.extname(filename).slice(1);
const loader = (options === null || options === void 0 ? void 0 : options.loaders) && (options === null || options === void 0 ? void 0 : options.loaders[ext]) ? options.loaders[ext] : loaders.includes(extName) ? extName : 'text';
const sourcemaps = enableSourcemaps ? {

@@ -33,3 +61,16 @@ sourcemap: true,

};
const result = esbuild.transformSync(content, {
/// this logic or code from
/// https://github.com/threepointone/esjest-transform/blob/main/src/index.js
/// this will supoort the jest.mock
/// https://github.com/aelbore/esbuild-jest/issues/12
if (sources.code.indexOf("ock(") >= 0 || (opts === null || opts === void 0 ? void 0 : opts.instrument)) {
const source = babelTransform({
sourceText: content,
sourcePath: filename,
config,
options: opts
});
sources.code = source;
}
const result = esbuild.transformSync(sources.code, {
loader,

@@ -67,3 +108,7 @@ format: (options === null || options === void 0 ? void 0 : options.format) || 'cjs',

;
const transformer = {
canInstrument: true,
createTransformer
};
exports.createTransformer = createTransformer;
module.exports = transformer;
{
"name": "esbuild-jest",
"version": "0.4.0",
"version": "0.5.0-beta.1",
"description": "Jest plugin to use esbuild for transformation",

@@ -24,4 +24,9 @@ "main": "esbuild-jest.js",

"peerDependencies": {
"esbuild": ">=0.8.36"
"esbuild": ">=0.8.49"
},
"dependencies": {
"@babel/core": "^7.12.17",
"@babel/plugin-transform-modules-commonjs": "^7.12.13",
"babel-jest": "^26.6.3"
}
}

@@ -8,2 +8,4 @@ # esbuild-jest

| Note: Jest mocks are not currently supported, for more information see https://github.com/aelbore/esbuild-jest/issues/12 and https://github.com/evanw/esbuild/issues/412
## Install

@@ -10,0 +12,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc