"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.default = exConfig;
var _fs = _interopRequireDefault(require("fs"));
var _path = _interopRequireDefault(require("path"));
var _copyWebpackPlugin = _interopRequireDefault(require("copy-webpack-plugin"));
var paths = _interopRequireWildcard(require("./paths.js"));
var _prettyLoader = _interopRequireDefault(require("../config/pretty-loader"));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;}

/**
 * 获取小游戏工程目录的minigame.config.js的配置
 * @param {Object} webpackConfig webpack的配置文件
 * @param {Object} options 提供一些参数
 * @param {String} options.context   小游戏工程目录
 * @param {String} options.src 小游戏工程src目录
 * @param {String} options.build 小游戏工程的编译目录
 */
function exConfig(webpackConfig, options) {
  // 如果小游戏工程目录存在minigame.config.js，就以此为准，没有再判断webpack.config.js
  if (_fs.default.existsSync(paths.PROJECT_CONFIG)) {
    try {
      const { externals = [], copyFile = [] } = require(paths.PROJECT_CONFIG)(options);
      const exts = webpackConfig.externals || {};
      const copys = [];
      externals.map((e) => {
        // 把 externals添加到webpack配置中去
        exts[e.module_name] = `commonjs ${e.module_path}`;

        const inputFilePath = _path.default.join(options.context, e.module_from);

        (0, _prettyLoader.default)(inputFilePath);

        copys.push({
          from: _path.default.join(options.context, e.module_from),
          to: _path.default.join(options.build, e.module_path)
        });
      });

      if (copys.length) {
        webpackConfig.externals = exts;
        // 把配置的 externals 文件 拷贝到 build目录
        webpackConfig.plugins.push(new _copyWebpackPlugin.default({
          patterns: copys.map((config) => ({
            ...config
          }))
        }));
      }
      // 复制文件到build的逻辑，没有被代码引入，但是打包需要打的文件路径
      const copyFiles = copyFile.map((e) => {
        (0, _prettyLoader.default)(_path.default.join(options.context, e.module_from));
        return {
          from: _path.default.join(options.context, e.module_from),
          to: _path.default.join(options.build, e.module_path)
        };
      });
      if (copyFiles.length) {
        webpackConfig.plugins.push(new _copyWebpackPlugin.default({
          patterns: copyFiles.map((config) => ({
            ...config
          }))
        }));
      }
    } catch (e) {
      console.error(e);
    }
  } else if (_fs.default.existsSync(paths.PROJECT_WEBPACK_CONFIG)) {
    try {
      require(paths.PROJECT_WEBPACK_CONFIG).postHook(webpackConfig, options);
    } catch (e) {
      console.error(e);
    }
  }
}