Skip to content

Instantly share code, notes, and snippets.

@zhangjiayin
Created November 15, 2016 04:18
Show Gist options
  • Save zhangjiayin/9e48c0bf950f5236e3fe91a234596ed7 to your computer and use it in GitHub Desktop.
Save zhangjiayin/9e48c0bf950f5236e3fe91a234596ed7 to your computer and use it in GitHub Desktop.
js requirejs babel in browser
define(function () {
'use strict';
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;
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
};
try {
var content = fs.readFileSync(path, {encoding: 'utf8'});
try {
compiled = ReactTools.transform(ensureJSXPragma(content, config), options);
} catch (err) {
throw new Error('jsx.js - Error while running JSXTransformer on ' + path + '\n' + err.message);
}
} catch (err) {
onLoadNative.error(err);
}
if (config.isBuild) {
buildMap[name] = compiled;
}
onLoadNative.fromText(compiled);
},
/**
* Dynamically requires JSXTransformer and the text plugin async
* and transforms the JSX in the browser
*/
Babel: 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, Babel) {
try {
var transform = Babel.transform(content, {
presets: ['react', 'es2015','stage-3','stage-2'],
plugins: [
"transform-decorators-legacy",
["transform-es2015-template-literals", { "loose": true }],
"transform-es2015-literals",
"transform-es2015-function-name",
"transform-es2015-arrow-functions",
"transform-es2015-block-scoped-functions",
["transform-es2015-classes", { "loose": true }],
"transform-es2015-object-super",
"transform-es2015-shorthand-properties",
["transform-es2015-computed-properties", { "loose": true }],
["transform-es2015-for-of", { "loose": true }],
"transform-es2015-sticky-regex",
"transform-es2015-unicode-regex",
"check-es2015-constants",
["transform-es2015-spread", { "loose": true }],
"transform-es2015-parameters",
["transform-es2015-destructuring", { "loose": true }],
"transform-es2015-block-scoping",
"transform-object-rest-spread",
"transform-es3-member-expression-literals",
"transform-es3-property-literals"
],
//presets: ['es2015', 'react'],
filename: 'embedded',
sourceMaps: 'inline'
})
//console.log(transform);
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(['babel','text'], function (Babel, text) {
text.load(name, parentRequire, function (content) {
onLoad(content, Babel);
}, config);
});
}
};
function ensureJSXFileExtension(name, config) {
var fileExtension = config.jsx && config.jsx.fileExtension || '.jsx';
if (name.indexOf(fileExtension) === -1) {
name = name + fileExtension;
}
return name;
}
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: function (name, parentRequire, onLoadNative, config) {
var method = isNode ? 'ReactTools' : 'Babel';
transform[method].call(this, name, parentRequire, onLoadNative, config);
},
write: function (pluginName, name, write) {
if (buildMap.hasOwnProperty(name)) {
var text = buildMap[name];
write.asModule(pluginName + "!" + name, text);
}
}
};
return jsx;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment