rollup-plugin-flow-entry
Version:
Allows Flow to find the original typed source code for the Rollup bundle
221 lines (181 loc) • 6.3 kB
JavaScript
;
var path = require('path');
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
function _typeof(obj) {
"@babel/helpers - typeof";
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
_typeof = function (obj) {
return typeof obj;
};
} else {
_typeof = function (obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
};
}
return _typeof(obj);
}
function _unsupportedIterableToArray(o, minLen) {
if (!o) return;
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
var n = Object.prototype.toString.call(o).slice(8, -1);
if (n === "Object" && o.constructor) n = o.constructor.name;
if (n === "Map" || n === "Set") return Array.from(o);
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
}
function _arrayLikeToArray(arr, len) {
if (len == null || len > arr.length) len = arr.length;
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
return arr2;
}
function _createForOfIteratorHelper(o, allowArrayLike) {
var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];
if (!it) {
if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
if (it) o = it;
var i = 0;
var F = function () {};
return {
s: F,
n: function () {
if (i >= o.length) return {
done: true
};
return {
done: false,
value: o[i++]
};
},
e: function (e) {
throw e;
},
f: F
};
}
throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
var normalCompletion = true,
didErr = false,
err;
return {
s: function () {
it = it.call(o);
},
n: function () {
var step = it.next();
normalCompletion = step.done;
return step;
},
e: function (e) {
didErr = true;
err = e;
},
f: function () {
try {
if (!normalCompletion && it.return != null) it.return();
} finally {
if (didErr) throw err;
}
}
};
}
/**
* Formats an output file, which re-exports items from a list of paths.
* @param {*} config the rollup-plugin-flow-entry config object.
* @param {string} outDir the output directory.
* @param {string} fileName the output file name.
* @param {string[]} paths an array of absolute paths to export types from.
*/
function buildEntry(config, outDir, fileName, paths) {
var mode = config.mode,
types = config.types; // Handle path overrides:
if (typeof types === 'string') {
paths = [types];
} else if (Array.isArray(types)) {
paths = types;
} else if (_typeof(types) === 'object' && types != null) {
var ourTypes = types[fileName];
if (typeof ourTypes === 'string') {
paths = [ourTypes];
} else if (Array.isArray(ourTypes)) {
paths = ourTypes;
} else if (ourTypes === false) {
return;
}
} // Set up the path resolution logic:
var here = path__default['default'].dirname(path__default['default'].resolve(outDir, fileName));
function escapePath(id) {
var out = path__default['default'].relative(here, id).replace(/\\+/g, '/').replace(/[']/g, "\\'");
return /^[/.]/.test(out) ? out : "./".concat(out);
} // Build the source code:
var source = mode != null ? "// @flow ".concat(mode, "\n") : '// @flow\n';
source += '// Generated by rollup-plugin-flow-entry\n\n';
for (var i = 0; i < paths.length; ++i) {
source += "export * from '".concat(escapePath(paths[i]), "'\n");
}
return {
type: 'asset',
fileName: fileName + '.flow',
source: source
};
}
/**
* Extracts the original entry points from the mulit-entry output.
* @param {string} outDir the output directory.
*/
function parseMultiEntry(outDir, code) {
var paths = [];
var lines = code.split('\n');
var _iterator = _createForOfIteratorHelper(lines),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var line = _step.value;
var quoted = line.replace(/^export \* from (".*");?/, '$1');
if (quoted === line) continue;
paths.push(path__default['default'].resolve(outDir, JSON.parse(quoted)));
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
return paths.sort();
}
function isMultiEntry(id) {
return id === '\0rollup-plugin-multi-entry:entry-point' || id === '\0virtual:multi-entry.js';
}
function flowEntry() {
var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var savedMultiEntry;
return {
name: 'rollup-plugin-flow-entry',
transform: function transform(code, id) {
// Capture the multi-entry point if it comes through:
if (isMultiEntry(id)) savedMultiEntry = code;
},
generateBundle: function generateBundle(opts, bundle) {
var outDir = opts.dir != null ? opts.dir : path__default['default'].dirname(opts.file);
for (var n in bundle) {
var file = bundle[n];
if (file.isAsset || !file.isEntry || file.facadeModuleId == null) {
continue;
}
if (!isMultiEntry(file.facadeModuleId)) {
// Normal files:
var entry = buildEntry(config, outDir, file.fileName, [file.facadeModuleId]);
if (entry != null) this.emitFile(entry);
} else {
// rollup-plugin-multi-entry:
if (savedMultiEntry == null || opts.file == null) {
this.warn('Unable to create Flow entry: rollup-plugin-multi-entry not configured correctly');
continue;
}
var _entry = buildEntry(config, outDir, path__default['default'].basename(opts.file), parseMultiEntry(outDir, savedMultiEntry));
if (_entry != null) this.emitFile(_entry);
}
}
}
};
}
module.exports = flowEntry;