umi-plugin-mako
Version:
## Install
152 lines (150 loc) • 4.81 kB
JavaScript
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/index.ts
var src_exports = {};
__export(src_exports, {
default: () => src_default
});
module.exports = __toCommonJS(src_exports);
var import_path = __toESM(require("path"));
var import_bundler_mako = require("./bundler-mako");
var import_express = __toESM(require("@umijs/deps/compiled/express"));
var import_htmlUtils = require("@umijs/preset-built-in/lib/plugins/commands/htmlUtils");
var import_fs = require("fs");
var import_path2 = require("path");
function src_default(api) {
api.describe({
key: "mako",
config: {
schema(joi) {
return joi.object();
}
}
});
if (!api.userConfig.mako)
return;
if (api.userConfig.ssr) {
console.log("mako bundler no support ssr!");
return;
}
api.modifyConfig(async (memo) => {
return {
...memo,
mfsu: false
};
});
api.modifyBundler(() => {
return import_bundler_mako.Bundler;
});
api.onStart(() => {
process.env.HMR = "none";
try {
const pkg = require(import_path.default.join(
require.resolve("@umijs/mako"),
"../../package.json"
));
api.logger.info(`Using mako@${pkg.version}`);
} catch (e) {
console.error(e);
}
});
api.onBuildComplete(async ({ err, stats }) => {
console.log("mako build complete");
if (!err) {
const compilation = stats.toJson();
const html = (0, import_htmlUtils.getHtmlGenerator)({ api });
const routeMap = await api.applyPlugins({
key: "modifyExportRouteMap",
type: api.ApplyPluginsType.modify,
initialValue: [{ route: { path: "/" }, file: "index.html" }],
args: {
html
}
});
for (const { route, file } of routeMap) {
const defaultContent = await html.getContent({
route,
assets: compilation.assets,
chunks: compilation.chunks
});
const content = await api.applyPlugins({
key: "modifyProdHTMLContent",
type: api.ApplyPluginsType.modify,
initialValue: defaultContent,
args: {
route,
file
}
});
const outputHtml = (0, import_path2.join)(api.paths.absOutputPath, file);
(0, import_fs.writeFileSync)(outputHtml, content, "utf-8");
}
}
});
api.addBeforeMiddlewares(() => {
const outputPath = import_path.default.resolve(
api.paths.cwd,
api.config.outputPath || "dist"
);
return [
require("cors")({
origin: true,
methods: ["GET", "HEAD", "PUT", "POST", "PATCH", "DELETE", "OPTIONS"],
credentials: true
}),
require("compression")(),
require("connect-history-api-fallback")({
index: "/"
}),
import_express.default.static(outputPath)
];
});
api.modifyBundleConfig((bundleConfig, { bundler: { id }, type }) => {
if (id === "mako") {
bundleConfig.onCompileDone = ({ isFirstCompile, stats }) => {
if (isFirstCompile) {
api.service.emit("firstDevCompileDone");
}
api.applyPlugins({
key: "onDevCompileDone",
type: api.ApplyPluginsType.event,
args: {
isFirstCompile,
type,
stats
}
}).catch((e) => {
console.error(e.stack);
});
};
bundleConfig.onCompileFail = () => {
};
}
return bundleConfig;
});
}