"use strict";var _WeakMap = require("@babel/runtime-corejs2/core-js/weak-map");var _Object$defineProperty = require("@babel/runtime-corejs2/core-js/object/define-property");var _Object$getOwnPropertyDescriptor = require("@babel/runtime-corejs2/core-js/object/get-own-property-descriptor");var _interopRequireDefault = require("@babel/runtime-corejs2/helpers/interopRequireDefault");_Object$defineProperty(exports, "__esModule", { value: true });exports.filter = filter;exports.toConfigs = toConfigs;var _create = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/object/create"));



var _index = require("../../cli-shared-utils/index.js");
var _path = _interopRequireDefault(require("path"));
var constanst = _interopRequireWildcard(require("./constanst.js"));
var paths = _interopRequireWildcard(require("./paths.js"));function _getRequireWildcardCache(e) {if ("function" != typeof _WeakMap) return null;var r = new _WeakMap(),t = new _WeakMap();return (_getRequireWildcardCache = function (e) {return e ? t : r;})(e);}function _interopRequireWildcard(e, r) {if (!r && e && e.__esModule) return e;if (null === e || "object" != typeof e && "function" != typeof e) return { default: e };var t = _getRequireWildcardCache(r);if (t && t.has(e)) return t.get(e);var n = { __proto__: null },a = _Object$defineProperty && _Object$getOwnPropertyDescriptor;for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) {var i = a ? _Object$getOwnPropertyDescriptor(e, u) : null;i && (i.get || i.set) ? _Object$defineProperty(n, u, i) : n[u] = e[u];}return n.default = e, t && t.set(e, n), n;} /**
 * 过滤manifest.json中分包配置。只取合法的配置，不正确的配置被过滤掉
 */




/**
 * 将配置中的分包转化为webpack的entry
 * 将配置中的分包转化为webpack的externals
 * 将配置中的分包转化为resConfig
 * @param {Array} subpackages
 */
function toConfigs(subpackages) {
  const entry = (0, _create.default)(null);
  const externals = (0, _create.default)(null);
  const resConfig = [];
  let isMain = false;

  subpackages.forEach(({ name, root }) => {
    const pkgPath = _path.default.join(paths.SRC, root);
    let entryKey, entryFile, mFile;
    if (name === 'main') {
      isMain = true;
    }
    if (_index.fs.statSync(pkgPath).isDirectory()) {
      // 如果是文件夹，取当前文件夹中的game.js作为入口
      entryFile = _path.default.join(pkgPath, constanst.ENTRY);
      // 入口名为分包名+game
      entryKey = _path.default.join(root, constanst.ENTRY_NAME);
      // 如果是文件夹，取当前文件夹中的game.js作为模块
      mFile = _path.default.posix.join(root, constanst.ENTRY);

      // 如果是文件夹，打包时要拷贝资源，生成资源配置文件
      resConfig.push({ name, root });
    } else {
      entryFile = pkgPath;
      // 去掉.js的后缀，作为entryKey
      const ps = _path.default.parse(root);
      entryKey = _path.default.posix.join(ps.dir, ps.name);

      mFile = root;
    }
    entry[entryKey] = entryFile;
    externals[name] = `commonjs ${mFile}`;
  });

  return { isMain, externals, entry, resConfig };
}

function filter(subpackages = []) {
  return subpackages.filter(({ name, root }, ind, arr) => {
    if (!name) {
      (0, _index.info)('分包配置中name为空的分包，已经被过滤！');
      return false;
    }
    if (!root) {
      (0, _index.info)(`分包配置中name=${name}的分包，root不存在，已经被过滤！`);
      return false;
    }

    // 检查 root 值对应的目录是否存在
    const pkgPath = _path.default.join(paths.SRC, root);
    if (!_index.fs.existsSync(pkgPath)) {
      (0, _index.info)(`分包配置name=${name}的分包，在${paths.SRC}目录下不存在${pkgPath}，已经被过滤！`);
      return false;
    }

    if (_index.fs.statSync(pkgPath).isDirectory() && !_index.fs.existsSync(_path.default.join(pkgPath, constanst.ENTRY))) {
      (0, _index.info)(`分包配置name=${name}的分包，在${pkgPath}目录下不存在${constanst.ENTRY}，已经被过滤！`);
      return false;
    }

    return true;
  });
}