UNPKG

@eaglesong/task-panorama

Version:
82 lines 3.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const helper_task_1 = require("@eaglesong/helper-task"); const IssueWebpackError_1 = require("fork-ts-checker-webpack-plugin/lib/issue/IssueWebpackError"); const fs_extra_1 = tslib_1.__importDefault(require("fs-extra")); const lodash_1 = tslib_1.__importDefault(require("lodash")); const memory_fs_1 = tslib_1.__importDefault(require("memory-fs")); const webpack_1 = tslib_1.__importDefault(require("webpack")); const config_1 = require("./config"); const manifest_1 = require("./plugins/PanoramaManifestPlugin/manifest"); function extractErrorsFromStats(stats, type) { const errors = []; const processCompilation = (compilation) => { errors.push(...compilation[type]); compilation.children.forEach(processCompilation); }; processCompilation(stats.compilation); return lodash_1.default.unionBy(errors, error => (typeof error === 'string' ? error : error.message)); } class PanoramaTask extends helper_task_1.Task { constructor(options = {}) { super(options); } apply() { this.hooks.build.tapPromise(this.constructor.name, () => this.build()); this.hooks.preBuild.tapPromise(this.constructor.name, async () => { const schemaPath = this.resolvePath('.eaglesong/schemas/panorama-manifest.json'); await fs_extra_1.default.outputJson(schemaPath, manifest_1.manifestSchema, { spaces: 2 }); }); this.hooks.compile.tap(this.constructor.name, addResource => addResource('panorama/**/*.{xml,js,css}')); } async build() { let webpackConfig = config_1.createWebpackConfig(this); if (this.options.config) webpackConfig = this.options.config(webpackConfig); if (!webpackConfig.plugins) webpackConfig.plugins = []; const compiler = webpack_1.default(webpackConfig); if (this.dotaPath == null) { compiler.outputFileSystem = new memory_fs_1.default(); } if (this.isWatching) { compiler.watch({}, (error, stats) => this.compilationHandler(error, stats)); } else { compiler.run((error, stats) => this.compilationHandler(error, stats)); } compiler.hooks.watchRun.tap(this.constructor.name, () => { this.removeErrors(); this.start(); }); return new Promise(resolve => compiler.hooks.done.tap(this.constructor.name, () => setImmediate(() => { this.finish(); resolve(); }))); } compilationHandler(webpackError, stats) { if (webpackError) throw webpackError; if (stats.hasErrors()) { this.displayErrors(extractErrorsFromStats(stats, 'errors'), 'error'); } if (stats.hasWarnings()) { this.displayErrors(extractErrorsFromStats(stats, 'warnings'), 'warning'); } } displayErrors(errors, level) { var _a, _b; for (const error of errors) { if (error instanceof IssueWebpackError_1.IssueWebpackError) { const { line, column } = (_b = (_a = error.issue.location) === null || _a === void 0 ? void 0 : _a.start, (_b !== null && _b !== void 0 ? _b : {})); const message = `(${line},${column}) ${error.issue.code}: ${error.issue.message}`; this.error({ filePath: error.issue.file, level, message }); continue; } this.error({ level, message: typeof error === 'string' ? error : error.toString() }); } } } exports.default = PanoramaTask; //# sourceMappingURL=index.js.map