st-bundle
Version:
CLI for watching and bundling SpringType projects.
125 lines (124 loc) • 4.87 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const cache_1 = require("../cache/cache");
const config_1 = require("../config/config");
const devServer_1 = require("../dev-server/devServer");
const env_1 = require("../env");
const FuseBoxLogAdapter_1 = require("../fuse-log/FuseBoxLogAdapter");
const setup_1 = require("../integrity/setup");
const interceptor_1 = require("../interceptor/interceptor");
const ProductionApiWrapper_1 = require("../production/api/ProductionApiWrapper");
const configParser_1 = require("../tsconfig/configParser");
const utils_1 = require("../utils/utils");
const webIndex_1 = require("../web-index/webIndex");
const assemble_context_1 = require("./assemble_context");
const ContextTaskManager_1 = require("./ContextTaskManager");
const WeakModuleReferences_1 = require("./WeakModuleReferences");
const writer_1 = require("./writer");
class Context {
constructor(config) {
this.config = config;
this.config.ctx = this;
this.log = FuseBoxLogAdapter_1.createFuseLogger(this.config.logging);
this.weakReferences = WeakModuleReferences_1.createWeakModuleReferences(this);
this.assembleContext = assemble_context_1.assembleContext(this);
this.ict = interceptor_1.createInterceptor();
this.webWorkers = {};
this.webIndex = webIndex_1.createWebIndex(this);
setup_1.attachEssentials(this);
this.taskManager = ContextTaskManager_1.createContextTaskManager(this);
}
addTsConfigAtPath(path) {
if (!this.tsConfigAtPaths)
this.tsConfigAtPaths = [];
this.tsConfigAtPaths.push(path);
}
getUniqueEntryHash() {
if (this._uniqueEntryHash)
return this._uniqueEntryHash;
if (!this.config.entries)
return '';
this._uniqueEntryHash = utils_1.fastHash(this.config.entries.join('')) + '_';
return this._uniqueEntryHash;
}
setDevelopment() {
this.writer = writer_1.createWriter({
isProduction: false,
root: env_1.env.SCRIPT_PATH,
output: this.config.output,
});
this.tsConfig = configParser_1.initTypescriptConfig(this.config);
this.devServer = devServer_1.createDevServer(this);
if (this.config.cache && this.config.cache.enabled) {
if (this.config.cacheObject)
this.cache = this.config.cacheObject;
else
this.cache = cache_1.createCache({ ctx: this });
}
this.config.setupEnv();
}
setProduction(prodProps) {
this.config.watch.enabled = false;
this.config.hmr.enabled = false;
prodProps = prodProps || {};
if (prodProps.screwIE === undefined)
prodProps.screwIE = true;
if (prodProps.uglify === undefined) {
prodProps.uglify = true;
}
this.config.production = prodProps;
this.writer = writer_1.createWriter({
isProduction: true,
root: env_1.env.SCRIPT_PATH,
output: this.config.output,
});
this.tsConfig = configParser_1.initTypescriptConfig(this.config);
this.devServer = devServer_1.createDevServer(this);
this.config.setupEnv();
this.productionApiWrapper = new ProductionApiWrapper_1.ProductionAPIWrapper(this);
this.config.manifest = { enabled: false };
if (prodProps.manifest) {
if (typeof prodProps.manifest === 'boolean') {
this.config.manifest.enabled = prodProps.manifest;
}
else {
this.config.manifest = prodProps.manifest;
if (this.config.manifest.enabled === undefined) {
this.config.manifest.enabled = true;
}
}
}
if (!this.config.manifest.filePath) {
this.config.manifest.filePath = 'manifest.json';
}
this.config.manifest.filePath = utils_1.ensureUserPath(this.config.manifest.filePath, this.writer.outputDirectory);
}
get useSingleBundle() {
return this.config.useSingleBundle || this.config.target === 'web-worker';
}
fatal(header, messages) {
this.log.fuseFatal(header, messages);
process.exit(1);
}
isInstalled(name) {
try {
return require(name);
}
catch (e) {
return false;
}
}
}
exports.Context = Context;
function createContext(cfg) {
const context = new Context(config_1.createConfig(cfg));
context.setDevelopment();
return context;
}
exports.createContext = createContext;
function createProdContext(cfg, prodProps) {
const context = new Context(config_1.createConfig(cfg));
context.setProduction(prodProps);
return context;
}
exports.createProdContext = createProdContext;