requirejs-react-jsx
Advanced tools
Comparing version 0.16.2 to 1.0.0
184
jsx.js
define(function () { | ||
'use strict'; | ||
var isNode = typeof process !== "undefined" && | ||
process.versions && | ||
!!process.versions.node; | ||
var buildMap = {}; | ||
var transform = { | ||
/** | ||
* Reads the .jsx file synchronously and requires react-tools | ||
* to perform the transform when compiling using r.js | ||
*/ | ||
ReactTools: function (name, parentRequire, onLoadNative, config) { | ||
var fs = require.nodeRequire('fs'), | ||
ReactTools = require.nodeRequire('react-tools'), | ||
compiled = void 0; | ||
function getSourceMapConfig(config) { | ||
var fallback = config.isBuild ? false : 'inline'; | ||
var path = parentRequire.toUrl(ensureJSXFileExtension(name, config)); | ||
var oldOptions = config.jsx && config.jsx.transformOptions || {}; // @deprecated | ||
var options = config.config && config.config.jsx && config.config.jsx.transformOptions || oldOptions || { | ||
harmony: true // enable harmony by default | ||
}; | ||
if (config && config.babel && 'sourceMaps' in config.babel) { | ||
return config.babel.sourceMaps; | ||
} else { | ||
return fallback; | ||
} | ||
} | ||
try { | ||
var content = fs.readFileSync(path, {encoding: 'utf8'}); | ||
function babelSync(name, parentRequire, onLoadNative, config) { | ||
var babel = require.nodeRequire('babel'); | ||
var path = parentRequire.toUrl(ensureJSXFileExtension(name, config)); | ||
try { | ||
compiled = ReactTools.transform(ensureJSXPragma(content, config), options); | ||
try { | ||
var result = babel.transformFileSync(path, { | ||
sourceFileName: config.baseUrl + name, | ||
sourceMaps: getSourceMapConfig(config), | ||
filename: config.baseUrl + name | ||
}); | ||
if (process.env.running_under_istanbul) { | ||
var istanbul = require.nodeRequire('istanbul'); | ||
var coverageVariable = Object.keys(global).filter(function (key) { return key.indexOf('$$cov_') === 0 })[0]; | ||
var instrumenter = new istanbul.Instrumenter({ | ||
coverageVariable: coverageVariable | ||
}); | ||
var compiled = result.code; | ||
compiled = instrumenter.instrumentSync(compiled, path); | ||
} | ||
} catch (err) { | ||
throw new Error('jsx.js - Error while running JSXTransformer on ' + path + '\n' + err.message); | ||
} | ||
if (process.env.running_under_istanbul) { | ||
var istanbul = require.nodeRequire('istanbul'); | ||
var coverageVariable = Object.keys(global).filter(function (key) { return key.indexOf('$$cov_') === 0 })[0]; | ||
var instrumenter = new istanbul.Instrumenter({ | ||
coverageVariable: coverageVariable | ||
}); | ||
} catch (err) { | ||
onLoadNative.error(err); | ||
compiled = instrumenter.instrumentSync(compiled, path); | ||
} | ||
if (config.isBuild) { | ||
buildMap[name] = compiled; | ||
} | ||
buildMap[name] = compiled; | ||
onLoadNative.fromText(compiled); | ||
}, | ||
/** | ||
* Dynamically requires Babel and the text plugin async | ||
* and transforms the JSX in the browser. | ||
* The configproperty 'babelOptions' will be passed to Babel. | ||
*/ | ||
babel: function (name, parentRequire, onLoadNative, config) { | ||
name = ensureJSXFileExtension(name, config); | ||
} catch (err) { | ||
onLoadNative.error(err); | ||
} | ||
} | ||
var options = config.jsx && config.jsx.babelOptions || {}; | ||
function babelAsync(name, parentRequire, onLoadNative, config) { | ||
name = ensureJSXFileExtension(name, config); | ||
var onLoad = function(content, babel) { | ||
parentRequire(['babel', 'text'], function (babel, text) { | ||
text.load(name, parentRequire, function (content) { | ||
try { | ||
var transform = babel.transform(ensureJSXPragma(content, config), options); | ||
var result = babel.transform(content, { | ||
sourceFileName: config.baseUrl + name, | ||
sourceMaps: getSourceMapConfig(config), | ||
filename: config.baseUrl + name | ||
}); | ||
content = transform.code; | ||
if (transform.map) { | ||
content += "\n//# sourceURL=" + config.baseUrl + name; | ||
} | ||
onLoadNative.fromText(result.code); | ||
} catch (err) { | ||
onLoadNative.error(err); | ||
} | ||
onLoadNative.fromText(content); | ||
}; | ||
parentRequire(['babel', 'text'], function (babel, text) { | ||
text.load(name, parentRequire, function (content) { | ||
onLoad(content, babel); | ||
}, config); | ||
}); | ||
}, | ||
}); | ||
} | ||
/** | ||
* Dynamically requires JSXTransformer and the text plugin async | ||
* and transforms the JSX in the browser | ||
*/ | ||
JSXTransformer: function (name, parentRequire, onLoadNative, config) { | ||
name = ensureJSXFileExtension(name, config); | ||
var oldOptions = config.jsx && config.jsx.transformOptions || {}; // @deprecated | ||
var options = config.config && config.config.jsx && config.config.jsx.transformOptions || oldOptions || { | ||
harmony: true // enable harmony by default | ||
}; | ||
if (options.inlineSourceMap) { | ||
options.sourceMap = true; | ||
} | ||
var onLoad = function(content, JSXTransformer) { | ||
try { | ||
var transform = JSXTransformer.transform(ensureJSXPragma(content, config), options); | ||
content = transform.code; | ||
if (options.inlineSourceMap && transform.sourceMap) { | ||
var sourceMap = transform.sourceMap; | ||
if (typeof transform.sourceMap.toJSON === 'function') { | ||
sourceMap = transform.sourceMap.toJSON(); | ||
} | ||
sourceMap.file = name; | ||
sourceMap.sources[0] = config.baseUrl + name; | ||
content += "\n//# sourceMappingURL=data:application/json;base64," + btoa(JSON.stringify(sourceMap)); | ||
} else { | ||
content += "\n//# sourceURL=" + config.baseUrl + name; | ||
} | ||
} catch (err) { | ||
onLoadNative.error(err); | ||
} | ||
onLoadNative.fromText(content); | ||
}; | ||
parentRequire(['JSXTransformer', 'text'], function (JSXTransformer, text) { | ||
text.load(name, parentRequire, function (content) { | ||
onLoad(content, JSXTransformer); | ||
}, config); | ||
}); | ||
} | ||
}; | ||
function ensureJSXFileExtension(name, config) { | ||
var fileExtension = config.jsx && config.jsx.fileExtension || '.jsx'; | ||
var fileExtension = config && config.jsx && config.jsx.fileExtension || '.jsx'; | ||
@@ -149,26 +82,9 @@ if (name.indexOf(fileExtension) === -1) { | ||
function ensureJSXPragma(content, config){ | ||
if (config.usePragma && content.indexOf('@jsx React.DOM') === -1) { | ||
content = "/** @jsx React.DOM */\n" + content; | ||
} | ||
return content; | ||
} | ||
var isNode = typeof process !== "undefined" && | ||
process.versions && | ||
!!process.versions.node; | ||
var jsx = { | ||
version: '0.1.1', | ||
load: isNode ? babelSync : babelAsync, | ||
load: function (name, parentRequire, onLoadNative, config) { | ||
var method = isNode ? 'ReactTools' : (config.jsx.transformer || 'JSXTransformer'); | ||
transform[method].call(this, name, parentRequire, onLoadNative, config); | ||
}, | ||
write: function (pluginName, name, write) { | ||
if (buildMap.hasOwnProperty(name)) { | ||
if (typeof buildMap[name] === 'text') { | ||
var text = buildMap[name]; | ||
write.asModule(pluginName + "!" + name, text); | ||
@@ -175,0 +91,0 @@ } |
{ | ||
"name": "requirejs-react-jsx", | ||
"version": "0.16.2", | ||
"version": "1.0.0", | ||
"description": "A RequireJS plugin for loading jsx in require.js and r.js", | ||
@@ -5,0 +5,0 @@ "main": "jsx.js", |
@@ -30,3 +30,3 @@ # requirejs-react-jsx | ||
`app.js` | ||
`app.jsx` | ||
@@ -65,3 +65,3 @@ ```js | ||
"react": "bower_components/react/react-with-addons", | ||
"JSXTransformer": "bower_components/react/JSXTransformer", | ||
"babel": "bower_components/requirejs-react-jsx/babel-5.8.34.min", | ||
"jsx": "bower_components/requirejs-react-jsx/jsx", | ||
@@ -74,15 +74,9 @@ "text": "bower_components/requirejs-text/text" | ||
"exports": "React" | ||
}, | ||
"JSXTransformer": "JSXTransformer" | ||
} | ||
}, | ||
config: { | ||
jsx: { | ||
fileExtension: ".jsx", | ||
transformOptions: { | ||
harmony: true, | ||
stripTypes: false, | ||
inlineSourceMap: true | ||
}, | ||
usePragma: false | ||
babel: { | ||
sourceMaps: "inline", // One of [false, 'inline', 'both']. See https://babeljs.io/docs/usage/options/ | ||
fileExtension: ".jsx" // Can be set to anything, like .es6 or .js. Defaults to .jsx | ||
} | ||
@@ -100,14 +94,2 @@ } | ||
Can also be configured with Babel: | ||
```javascript | ||
config: { | ||
jsx: { | ||
transformer: "babel", | ||
babelOptions: { | ||
sourceMaps: 'inline' | ||
} | ||
} | ||
} | ||
``` | ||
### Building | ||
@@ -166,4 +148,8 @@ | ||
# Changelog | ||
**1.0** - Eliminated all other transformer options than Babel. Switched config variable from `jsx` to `babel`. Added browser compatible babel 5.x build to repository to use for in-browser compilations | ||
# License | ||
[MIT](LICENSE) |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
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
High entropy strings
Supply chain riskContains high entropy strings. This could be a sign of encrypted data, leaked secrets or obfuscated code.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
905628
7
3560
151
8
6