Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

import-jsx

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

import-jsx - npm Package Compare versions

Comparing version 1.3.2 to 2.0.0

68

index.js
'use strict';
const path = require('path');
const fs = require('fs');
const requireFromString = require('require-from-string');
const destructuringTransform = require('babel-plugin-transform-es2015-destructuring');

@@ -13,6 +11,2 @@ const restSpreadTransform = require('babel-plugin-transform-object-rest-spread');

const supportsDestructuring = Number(process.versions.node.split('.')[0]) >= 6;
const cache = new Map();
const importJsx = (moduleId, options) => {

@@ -25,3 +19,5 @@ if (typeof moduleId !== 'string') {

pragma: 'h',
cache: true
cache: true,
// Put on options object for easier testing.
supportsDestructuring: Number(process.versions.node.split('.')[0]) >= 6
}, options);

@@ -31,29 +27,47 @@

if (options.cache && cache.has(modulePath)) {
return cache.get(modulePath);
if (!options.cache) {
delete require.cache[modulePath];
}
const source = fs.readFileSync(modulePath, 'utf8');
// If they used .jsx, and there's already a .jsx, then hook there
// Otherwise, hook node's default .js
const ext = path.extname(modulePath);
const hookExt = require.extensions[ext] ? ext : '.js';
if (source.includes('React')) {
options.pragma = 'React.createElement';
}
const oldExtension = require.extensions[hookExt];
const plugins = [
[restSpreadTransform, {useBuiltIns: true}],
supportsDestructuring ? null : destructuringTransform,
[jsxTransform, {pragma: options.pragma, useBuiltIns: true}]
].filter(Boolean);
require.extensions[hookExt] = module => {
const oldCompile = module._compile;
const result = babel.transform(source, {
plugins,
filename: modulePath,
sourceMaps: 'inline',
babelrc: false
});
module._compile = source => {
if (source.includes('React')) {
options.pragma = 'React.createElement';
}
const m = requireFromString(result.code, modulePath);
const plugins = [
[restSpreadTransform, {useBuiltIns: true}],
options.supportsDestructuring ? null : destructuringTransform,
[jsxTransform, {pragma: options.pragma, useBuiltIns: true}]
].filter(Boolean);
if (options.cache) {
cache.set(modulePath, m);
const result = babel.transform(source, {
plugins,
filename: modulePath,
sourceMaps: 'inline',
babelrc: false
});
module._compile = oldCompile;
module._compile(result.code, modulePath);
};
require.extensions[hookExt] = oldExtension;
oldExtension(module, modulePath);
};
const m = require(modulePath);
require.extensions[hookExt] = oldExtension;
if (!options.cache) {
delete require.cache[modulePath];
}

@@ -60,0 +74,0 @@

{
"name": "import-jsx",
"version": "1.3.2",
"version": "2.0.0",
"description": "Require and transpile JSX on the fly",

@@ -16,3 +16,3 @@ "license": "MIT",

"scripts": {
"test": "xo && ava"
"test": "xo && nyc --check-coverage --branches=100 --lines=100 --functions=100 --statements=100 ava"
},

@@ -35,3 +35,2 @@ "files": [

"caller-path": "^2.0.0",
"require-from-string": "^1.2.1",
"resolve-from": "^3.0.0"

@@ -41,2 +40,3 @@ },

"ava": "^0.19.1",
"nyc": "^13.3.0",
"sinon": "^2.3.5",

@@ -43,0 +43,0 @@ "xo": "^0.18.2"

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