fable-compiler-netcore
Version:
F# to JavaScript compiler
96 lines (95 loc) • 4.32 kB
JavaScript
;
var path = require("path");
var rollup = require("rollup");
var hypothetical = require("rollup-plugin-hypothetical");
var fableLib = require("./lib");
var constants = require("./constants");
var build_1 = require("./build");
/** Bundles generated JS files and dependencies, requires rollup and plugins */
var bundleCache = null;
var fullRebuildTriggered = false;
function normalizeProjectName(opts) {
var projName = path.basename(opts.projFile[opts.projFile.length - 1]);
return projName.substr(0, projName.indexOf(".")).replace(/[^A-Z_]/ig, "_");
}
function readRollupOptions(opts) {
if (!opts.rollup) {
return opts;
}
var rollupOpts = opts.rollup;
var outDir = fableLib.pathJoin(opts.workingDir, opts.outDir);
rollupOpts = typeof rollupOpts === "boolean" ? {} : rollupOpts;
rollupOpts = typeof rollupOpts === "string" ? { dest: fableLib.pathJoin(outDir, rollupOpts) } : rollupOpts;
rollupOpts.plugins = fableLib.resolvePlugins(rollupOpts.plugins, opts.workingDir, "rollup-plugin-");
var plugins = [], nodeResolve = 'rollup-plugin-node-resolve', commonjs = 'rollup-plugin-commonjs';
// Attention: Plugin order is important
if (!rollupOpts.plugins.some(function (kv) { return kv[0].indexOf(nodeResolve) >= 0; })) {
plugins.push(require(nodeResolve)({ ignoreGlobal: true }));
}
if (!rollupOpts.plugins.some(function (kv) { return kv[0].indexOf(commonjs) >= 0; })) {
plugins.push(require(commonjs)({ jsnext: true, main: true, browser: true }));
}
// Custom plugins
for (var i = 0; i < rollupOpts.plugins.length; i++) {
var kv = rollupOpts.plugins[i];
plugins.push(require(kv[0])(kv[1]));
}
rollupOpts.plugins = plugins;
// Other options
rollupOpts.format = rollupOpts.format == null ? constants.JS_MODULES[opts.module] : rollupOpts.format;
rollupOpts.sourceMap = rollupOpts.sourceMap == null ? opts.sourceMaps : rollupOpts.sourceMap;
rollupOpts.moduleName = rollupOpts.moduleName || normalizeProjectName(opts);
rollupOpts.dest = rollupOpts.dest == null
? fableLib.pathJoin(outDir, "bundle.js")
: fableLib.pathJoin(opts.workingDir, rollupOpts.dest);
if (!opts.verbose) {
rollupOpts.onwarn = function (warning) { };
}
opts.rollup = rollupOpts;
return opts;
}
exports.readRollupOptions = readRollupOptions;
function clearBundleCache() {
bundleCache = null;
}
exports.clearBundleCache = clearBundleCache;
function bundle(jsFiles, opts, fableProc, continuation) {
var rollupOpts = Object.assign({}, opts.rollup);
rollupOpts.cache = bundleCache;
rollupOpts.plugins.splice(0, 0, hypothetical({
files: jsFiles, allowRealFiles: true, allowExternalModules: true
}));
if (rollupOpts.entry == null) {
rollupOpts.entry = Object.getOwnPropertyNames(jsFiles)
.find(function (f) { return jsFiles[f].isEntry; });
}
fableLib.stdoutLog("Bundling...");
rollup.rollup(rollupOpts)
.then(function (bundle) {
var parsed = bundle.generate(rollupOpts);
if (opts.watch) {
bundleCache = bundle;
}
// Write to disk, bundle.write doesn't seem to work
// bundle.write({ dest: rollupOpts.dest, format: rollupOpts.format, sourceMap: rollupOpts.sourceMap });
fableLib.writeFile(rollupOpts.dest, parsed.code, rollupOpts.sourceMap === true ? parsed.map : null);
fableLib.stdoutLog("Bundled " + path.relative(opts.workingDir, rollupOpts.dest) + " at " + (new Date()).toLocaleTimeString());
build_1.postbuild(opts, "SUCCESS", fableProc, continuation);
})
.catch(function (err) {
// The stack here is a bit noisy just report the message
fableLib.stderrLog(err.message);
bundleCache = null;
var buildResult = null;
if (!fullRebuildTriggered) {
fullRebuildTriggered = true;
buildResult = "NEEDS_FULL_REBUILD";
}
else {
fullRebuildTriggered = false; // Prevent infinite loop
buildResult = "FAIL";
}
build_1.postbuild(opts, buildResult, fableProc, continuation);
});
}
exports.bundle = bundle;