casterly
Version:
CLI for Casterly
132 lines (131 loc) • 5.13 kB
JavaScript
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.watchCompilers = void 0;
const path = __importStar(require("path"));
const unistore_1 = __importDefault(require("unistore"));
const __1 = require("..");
const logger_1 = require("./logger");
var WebpackStatusPhase;
(function (WebpackStatusPhase) {
WebpackStatusPhase[WebpackStatusPhase["COMPILING"] = 1] = "COMPILING";
WebpackStatusPhase[WebpackStatusPhase["COMPILED_WITH_ERRORS"] = 2] = "COMPILED_WITH_ERRORS";
WebpackStatusPhase[WebpackStatusPhase["COMPILED_WITH_WARNINGS"] = 4] = "COMPILED_WITH_WARNINGS";
WebpackStatusPhase[WebpackStatusPhase["COMPILED"] = 5] = "COMPILED";
})(WebpackStatusPhase || (WebpackStatusPhase = {}));
function getWebpackStatusPhase(status) {
if (status.loading === true) {
return WebpackStatusPhase.COMPILING;
}
else if (status.errors) {
return WebpackStatusPhase.COMPILED_WITH_ERRORS;
}
else if (status.warnings) {
return WebpackStatusPhase.COMPILED_WITH_WARNINGS;
}
return WebpackStatusPhase.COMPILED;
}
const buildStore = unistore_1.default();
buildStore.subscribe((state) => {
const { client, server } = state;
const [{ status }] = [
{ status: client, phase: getWebpackStatusPhase(client) },
{ status: server, phase: getWebpackStatusPhase(server) },
].sort((a, b) => a.phase.valueOf() - b.phase.valueOf());
const { bootstrap: bootstrapping, port } = logger_1.logStore.getState();
if (bootstrapping && status.loading) {
return;
}
let nextState;
if (status.loading === true) {
nextState = {
bootstrap: false,
loading: true,
port,
fileName: status.fileName,
};
}
else {
const { errors, warnings, typeChecking, buildDuration } = status;
if (errors == null && typeChecking) {
nextState = {
bootstrap: false,
port,
typeChecking: true,
loading: false,
errors,
warnings,
buildDuration,
};
}
else {
nextState = {
bootstrap: false,
port,
loading: false,
typeChecking: false,
errors,
warnings,
buildDuration,
};
}
}
logger_1.logStore.setState(nextState, true);
});
function watchCompilers(client, server, hasTypeChecking) {
buildStore.setState({
client: { loading: true },
server: { loading: true },
});
function tapCompiler(key, compiler, enableTypecheck, onEvent) {
compiler.hooks.invalid.tap(`BuildInvalid-${key}`, (fileName) => {
onEvent({
loading: true,
fileName: fileName
? path.relative(path.join(__1.paths.appPath), fileName)
: undefined,
});
});
compiler.hooks.done.tap(`BuildDone-${key}`, (stats) => {
const { errors, warnings } = stats.toJson({
all: false,
warnings: true,
errors: true,
});
const hasErrors = !!(errors === null || errors === void 0 ? void 0 : errors.length);
const hasWarnings = !!(warnings === null || warnings === void 0 ? void 0 : warnings.length);
const buildDuration = (stats.endTime - stats.startTime) / 1000;
onEvent({
loading: false,
errors: hasErrors ? errors !== null && errors !== void 0 ? errors : null : null,
warnings: hasWarnings ? warnings !== null && warnings !== void 0 ? warnings : null : null,
typeChecking: enableTypecheck,
buildDuration,
});
});
}
tapCompiler('client', client, hasTypeChecking, (status) => buildStore.setState({ client: status }));
tapCompiler('server', server, false, (status) => buildStore.setState({ server: status }));
}
exports.watchCompilers = watchCompilers;
;