UNPKG

umi-plugin-3-mako

Version:

## 安装

177 lines (175 loc) 6.13 kB
var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; 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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); 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_fs = __toESM(require("fs")); var import_http_proxy_middleware = require("http-proxy-middleware"); var import_path = __toESM(require("path")); require("./requireHook").init(); 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 generatorAlias = Object.keys(config.resolve.alias).map((key) => { return [key, config.resolve.alias[key]]; }); const generatorEntry = {}; Object.keys(config.entry).forEach((key) => { generatorEntry[key] = config.entry[key][0]; }); build({ root: this.cwd, config: { mode: config.mode, devtool: false, autoCSSModules: true, less: {}, resolve: { alias: generatorAlias }, entry: generatorEntry, // always enable stats to provide json for onBuildComplete hook stats: { modules: false } }, watch: false }).then(() => { const outputPath = import_path.default.resolve( this.cwd, this.config.outputPath || "dist" ); const statsJsonPath = import_path.default.join(outputPath, "stats.json"); const statsJson = JSON.parse(import_fs.default.readFileSync(statsJsonPath, "utf-8")); import_fs.default.rmSync(statsJsonPath); const stats = { compilation: statsJson, hasErrors: () => false }; const statsUtil = { toJson: () => stats.compilation }; onBuildComplete == null ? void 0 : onBuildComplete(null, statsUtil); resolve({ stats: statsUtil, compiler: {} }); }).catch((err) => { reject(err); }); }); } setupDevServerOpts({ bundleConfigs }) { const config = bundleConfigs[0]; const generatorAlias = Object.keys(config.resolve.alias).map((key) => { return [key, config.resolve.alias[key]]; }); const generatorEntry = {}; Object.keys(config.entry).forEach((key) => { generatorEntry[key] = config.entry[key][0]; }); return { onListening: async ({ server, 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" }); server.app.use("/__/hmr-ws", wsProxy); server.socketServer.on("upgrade", wsProxy.upgrade); const { build } = require("@umijs/mako"); const makoConfig = { mode: config.mode, devtool: false, autoCSSModules: true, less: {}, resolve: { alias: generatorAlias }, entry: generatorEntry, // always enable stats to provide json for onBuildComplete hook stats: { modules: false }, plugins: [] }; makoConfig.hmr = {}; makoConfig.devServer = { port: hmrPort, host: hostname }; makoConfig.plugins.push({ name: "mako-dev", generateEnd: (args) => { var _a2; (_a2 = config.onCompileDone) == null ? void 0 : _a2.call(config, { ...args, stats: { ...args == null ? void 0 : args.stats, compilation: { chunks: [{ name: "umi", files: ["umi.js", "umi.css"] }] } } }); } }); console.log(`http://${hostname}:${port}`); const cwd = this.cwd; try { await build({ root: 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 });