UNPKG

umi-plugin-mako

Version:
127 lines (125 loc) 4.3 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/bundler-mako.ts var bundler_mako_exports = {}; __export(bundler_mako_exports, { Bundler: () => Bundler }); module.exports = __toCommonJS(bundler_mako_exports); var import_bundler_webpack = require("@umijs/bundler-webpack"); var import_http_proxy_middleware = require("http-proxy-middleware"); var import_utils = require("./utils"); var Bundler = class extends import_bundler_webpack.Bundler { constructor(opts) { super(opts); } async build({ bundleConfigs, watch, onBuildComplete }) { return new Promise((resolve, reject) => { const { build } = require("@umijs/mako"); if (watch) { } const config = bundleConfigs[0]; const makoConfig = (0, import_utils.getMakoConfig)(this.config, config); makoConfig.plugins ?? (makoConfig.plugins = []); let statsJson; makoConfig.plugins.push({ name: "mako-stats", generateEnd: (args) => { statsJson = args.stats; } }); build({ root: this.cwd, config: makoConfig, watch: false }).then(() => { const statsUtil = (0, import_utils.getStats)(statsJson); onBuildComplete == null ? void 0 : onBuildComplete(null, statsUtil); resolve({ stats: statsUtil, compiler: {} }); }).catch((err) => { reject(err); }); }); } setupDevServerOpts({ bundleConfigs }) { const config = bundleConfigs[0]; const makoConfig = (0, import_utils.getMakoConfig)(this.config, config); return { onListening: async ({ port, hostname, listeningApp }) => { var _a; const hmrPort = port + 1; const wsProxy = (0, import_http_proxy_middleware.createProxyMiddleware)({ // mako server in the same host so hard code is ok target: `http://${hostname}:${hmrPort}`, ws: true, logLevel: "silent" }); listeningApp.on("upgrade", wsProxy.upgrade); const { build } = require("@umijs/mako"); makoConfig.hmr = {}; makoConfig.plugins ?? (makoConfig.plugins = []); makoConfig.devServer = { port: port + 1, host: hostname }; makoConfig.plugins.push({ name: "mako-dev", generateEnd: (args) => { var _a2; const statsUtil = (0, import_utils.getStats)(args.stats); const compilation = statsUtil.toJson(); (_a2 = config.onCompileDone) == null ? void 0 : _a2.call(config, { ...args, stats: { ...args == null ? void 0 : args.stats, compilation } }); if (args.isFirstCompile) { const protocol = this.config.https ? "https:" : "http:"; const banner = (0, import_utils.getDevBanner)(protocol, hostname, port); console.log(banner); } } }); try { await build({ root: this.cwd, config: makoConfig, watch: true }); } catch (e) { (_a = config.onCompileFail) == null ? void 0 : _a.call(config, e); console.error(e.message); const err = new Error("Build with mako failed."); err.stack = null; throw err; } }, onConnection: ({ connection, server }) => { } }; } }; Bundler.id = "mako"; Bundler.version = 1; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { Bundler });