var __create = Object.create;
var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
  for (var prop in b || (b = {}))
    if (__hasOwnProp.call(b, prop))
      __defNormalProp(a, prop, b[prop]);
  if (__getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(b)) {
      if (__propIsEnum.call(b, prop))
        __defNormalProp(a, prop, b[prop]);
    }
  return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
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(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/dev.ts
var dev_exports = {};
__export(dev_exports, {
  dev: () => dev,
  stripUndefined: () => stripUndefined
});
module.exports = __toCommonJS(dev_exports);
var import_mfsu = require("@umijs/mfsu");
var import_utils = require("@umijs/utils");
var import_fs = require("fs");
var import_path = require("path");
var import_webpack = __toESM(require("../compiled/webpack"));
var import_config = require("./config/config");
var import_constants = require("./constants");
var import_server = require("./server/server");
var import_types = require("./types");
function stripUndefined(obj) {
  Object.keys(obj).forEach((key) => {
    if (obj[key] === void 0) {
      delete obj[key];
    }
  });
  return obj;
}
async function dev(opts) {
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
  const cacheDirectoryPath = (0, import_path.resolve)(opts.rootDir || opts.cwd, opts.config.cacheDirectoryPath || "node_modules/.cache");
  const enableMFSU = opts.config.mfsu !== false;
  let mfsu = null;
  if (enableMFSU) {
    if (opts.config.srcTranspiler === import_types.Transpiler.swc) {
      import_utils.logger.warn(`Swc currently not supported for use with mfsu, recommended you use srcTranspiler: 'esbuild' in dev.`);
    }
    mfsu = new import_mfsu.MFSU({
      strategy: opts.mfsuStrategy,
      include: opts.mfsuInclude || [],
      srcCodeCache: opts.srcCodeCache,
      implementor: import_webpack.default,
      buildDepWithESBuild: (_a = opts.config.mfsu) == null ? void 0 : _a.esbuild,
      depBuildConfig: {
        extraPostCSSPlugins: ((_b = opts.config) == null ? void 0 : _b.extraPostCSSPlugins) || []
      },
      mfName: (_c = opts.config.mfsu) == null ? void 0 : _c.mfName,
      runtimePublicPath: opts.config.runtimePublicPath,
      tmpBase: ((_d = opts.config.mfsu) == null ? void 0 : _d.cacheDirectory) || (0, import_path.join)(cacheDirectoryPath, "mfsu"),
      onMFSUProgress: opts.onMFSUProgress,
      unMatchLibs: (_e = opts.config.mfsu) == null ? void 0 : _e.exclude,
      shared: (_f = opts.config.mfsu) == null ? void 0 : _f.shared,
      remoteAliases: (_g = opts.config.mfsu) == null ? void 0 : _g.remoteAliases,
      remoteName: (_h = opts.config.mfsu) == null ? void 0 : _h.remoteName,
      getCacheDependency() {
        return stripUndefined({
          version: require("../package.json").version,
          mfsu: opts.config.mfsu,
          alias: opts.config.alias,
          externals: opts.config.externals,
          theme: opts.config.theme,
          runtimePublicPath: opts.config.runtimePublicPath,
          publicPath: opts.config.publicPath
        });
      },
      serverBase: opts.mfsuServerBase,
      startBuildWorker: opts.startBuildWorker
    });
  }
  const webpackConfig = await (0, import_config.getConfig)({
    cwd: opts.cwd,
    rootDir: opts.rootDir,
    env: import_types.Env.development,
    entry: opts.entry,
    userConfig: opts.config,
    babelPreset: opts.babelPreset,
    extraBabelPlugins: [
      ...opts.beforeBabelPlugins || [],
      ...(mfsu == null ? void 0 : mfsu.getBabelPlugins()) || [],
      ...opts.extraBabelPlugins || []
    ],
    extraBabelPresets: [
      ...opts.beforeBabelPresets || [],
      ...opts.extraBabelPresets || []
    ],
    extraBabelIncludes: opts.config.extraBabelIncludes,
    extraEsbuildLoaderHandler: (mfsu == null ? void 0 : mfsu.getEsbuildLoaderHandler()) || [],
    chainWebpack: opts.chainWebpack,
    modifyWebpackConfig: opts.modifyWebpackConfig,
    hmr: process.env.HMR !== "none",
    analyze: process.env.ANALYZE,
    cache: opts.cache ? __spreadProps(__spreadValues({}, opts.cache), {
      cacheDirectory: (0, import_path.join)(cacheDirectoryPath, "bundler-webpack")
    }) : void 0,
    pkg: opts.pkg
  });
  const depConfig = await (0, import_config.getConfig)({
    cwd: opts.cwd,
    rootDir: opts.rootDir,
    env: import_types.Env.development,
    entry: opts.entry,
    userConfig: opts.config,
    hash: true,
    staticPathPrefix: import_mfsu.MF_DEP_PREFIX,
    name: import_constants.MFSU_NAME,
    chainWebpack: (_i = opts.config.mfsu) == null ? void 0 : _i.chainWebpack,
    extraBabelIncludes: opts.config.extraBabelIncludes,
    cache: {
      buildDependencies: (_j = opts.cache) == null ? void 0 : _j.buildDependencies,
      cacheDirectory: (0, import_path.join)(cacheDirectoryPath, "mfsu-deps")
    },
    pkg: opts.pkg
  });
  (_k = webpackConfig.resolve).alias || (_k.alias = {});
  ["@umijs/utils/compiled/strip-ansi", "react-error-overlay"].forEach((dep) => {
    webpackConfig.resolve.alias[dep] = require.resolve(dep);
  });
  await (mfsu == null ? void 0 : mfsu.setWebpackConfig({
    config: webpackConfig,
    depConfig
  }));
  if (mfsu && webpackConfig.cache && typeof webpackConfig.cache === "object" && webpackConfig.cache.type === "filesystem") {
    const webpackCachePath = (0, import_path.join)(webpackConfig.cache.cacheDirectory, `default-development`, "index.pack");
    const mfsuCacheExists = (0, import_fs.existsSync)(mfsu.getCacheFilePath());
    const webpackCacheExists = (0, import_fs.existsSync)(webpackCachePath);
    if (webpackCacheExists && !mfsuCacheExists) {
      import_utils.logger.warn(`Invalidate webpack cache since mfsu cache is missing`);
      import_utils.rimraf.sync(webpackConfig.cache.cacheDirectory);
    }
  }
  await (0, import_server.createServer)({
    webpackConfig,
    userConfig: opts.config,
    cwd: opts.cwd,
    beforeMiddlewares: [
      ...(mfsu == null ? void 0 : mfsu.getMiddlewares()) || [],
      ...opts.beforeMiddlewares || []
    ],
    port: opts.port,
    host: opts.host,
    ip: opts.ip,
    afterMiddlewares: [...opts.afterMiddlewares || []],
    onDevCompileDone: opts.onDevCompileDone,
    onProgress: opts.onProgress
  });
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  dev,
  stripUndefined
});
