import-jsx
Advanced tools
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" |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
6136
6
61
4
- Removedrequire-from-string@^1.2.1
- Removedrequire-from-string@1.2.1(transitive)