Instantly share code, notes, and snippets.

Embed
What would you like to do?
Transform compiled Clojurescript code for React Native/Metro
/**
* Copyright (c) 2015-present, Facebook, Inc.
* Copyright (c) 2018 Arctype Corp.
*
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of the facebook/react source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* Note: This is a fork of the fb-specific transform.js
*/
'use strict';
var metroWorker = require("./node_modules/metro/src/JSTransformer/worker");
var fs = require("fs");
function transformCode(buf, deps) {
var out = "__d(function (global, _$$_REQUIRE, _$$_IMPORT_DEFAULT, _$$_IMPORT_ALL, module, exports, _dependencyMap) {\n" +
"var __dependencyIndex = {}\n";
for (var i = 0; i < deps.length; i++) {
out += "__dependencyIndex[\"" + deps[i].name + "\"] = _dependencyMap[" + i.toString() + "];\n";
}
out += "var __lookupRequire = function(name) { if(__dependencyIndex[name]) { return __dependencyIndex[name]; } else { throw new Error('Module is not defined: ' + name); }};\n" +
"var require = function(name) { return _$$_REQUIRE(__lookupRequire(name), name); };\n";
out += buf.toString("utf8") +
"\n});";
return out;
}
module.exports = {
transform: async function(filename, localPath, codeBuf, options) {
if (!options.transformOptions.hot && filename.indexOf("index.ios.js") > 0) {
console.log("Invoking custom TRANSFORM:" + filename);
var mockBuffer = fs.readFileSync("cljs-deps.js");
var mock = await metroWorker.transform(filename, localPath, mockBuffer, options);
return {
dependencies: mock.dependencies,
output: [{data: {code: transformCode(codeBuf, mock.dependencies), map: []},
type: 'js/module'}],
getSource: function() { return codeBuf }
}
}
else {
return await metroWorker.transform(filename, localPath, codeBuf, options);
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment