"use strict";

var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property");

var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");

_Object$defineProperty(exports, "__esModule", {
  value: true
});

exports.default = exports.TITLE = exports.ROOT_PATH = exports.REMOVE_DATA_TEST_ID = exports.PORT = exports.NODE_ENV = exports.ENTRY = exports.CWD = exports.APP_PATH = void 0;

var _concat = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/concat"));

var _map = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/map"));

var _path = require("path");

var _debug = _interopRequireDefault(require("debug"));

var _yargs = require("yargs");

var _webpackBuildTime = _interopRequireDefault(require("../../scripts/webpack-build-time"));

var _webpack = require("./webpack.plugins");

var _context;

const CWD = process.env.CWD = process.cwd();
exports.CWD = CWD;
const TITLE = Boolean(process.env.TITLE) ? process.env.TITLE : 'Cosmic React App';
exports.TITLE = TITLE;
const PORT = Boolean(process.env.PORT) ? process.env.PORT : _yargs.argv.port ? _yargs.argv.port : 9000;
exports.PORT = PORT;
const NODE_ENV = process.env.NODE_ENV = Boolean(process.env.NODE_ENV) ? process.env.NODE_ENV : _yargs.argv.env ? _yargs.argv.env : 'development';
exports.NODE_ENV = NODE_ENV;
const ROOT_PATH = process.env.ROOT_PATH = Boolean(process.env.ROOT_PATH) ? (0, _path.resolve)(CWD, process.env.ROOT_PATH) : _yargs.argv.rootPath ? (0, _path.resolve)(CWD, _yargs.argv.rootPath) : (0, _path.resolve)(CWD);
exports.ROOT_PATH = ROOT_PATH;
const APP_PATH = process.env.APP_PATH = Boolean(process.env.APP_PATH) ? (0, _path.resolve)(ROOT_PATH, process.env.ROOT_PATH) : _yargs.argv.appPath ? (0, _path.resolve)(ROOT_PATH, _yargs.argv.appPath) : (0, _path.resolve)(ROOT_PATH, 'app');
exports.APP_PATH = APP_PATH;
const ENTRY = Boolean(process.env.ENTRY) ? process.env.ENTRY : _yargs.argv.entry ? (0, _path.join)(ROOT_PATH, _yargs.argv.entry) : (0, _path.join)(ROOT_PATH, 'index.js');
exports.ENTRY = ENTRY;
const REMOVE_DATA_TEST_ID = Boolean(process.env.REMOVE_DATA_TEST_ID) ? process.env.REMOVE_DATA_TEST_ID === 'true' : false;
exports.REMOVE_DATA_TEST_ID = REMOVE_DATA_TEST_ID;
const CLI_ROOT = (0, _path.resolve)(__dirname, '..', '..', '..');
const local_node_modules = (0, _path.join)(CLI_ROOT, 'node_modules');

const pkg = require((0, _path.resolve)(ROOT_PATH, './package.json'));

const babelLoaderPlugins = [[require.resolve('@babel/plugin-proposal-decorators'), {
  legacy: true
}], [require.resolve('@babel/plugin-proposal-class-properties'), {
  loose: true
}], [require.resolve('@babel/plugin-proposal-private-methods'), {
  loose: true
}], [require.resolve('@babel/plugin-proposal-private-property-in-object'), {
  loose: true
}]];

if (REMOVE_DATA_TEST_ID) {
  babelLoaderPlugins.push([require.resolve('babel-plugin-react-remove-properties'), {
    properties: ['data-test-id']
  }]);
}

const config = {
  context: __dirname,
  entry: {
    App: [ENTRY, './version.js'] //vendor: ['react', 'react-dom', 'redux', 'lodash'],

  },
  output: {
    path: (0, _path.join)(ROOT_PATH, '_dist'),
    publicPath: '/',
    filename: '[name]/[name].[chunkhash].js',
    chunkFilename: 'Chunks/[name].bundle.js'
  },
  resolve: {
    alias: {
      app: (0, _path.resolve)(APP_PATH),
      custom: (0, _path.resolve)(ROOT_PATH, 'custom'),
      'CORE_CONFIGS_PATH': '@meniga/config/src/configs',
      'CUSTOM_CONFIGS_PATH': (0, _path.resolve)(APP_PATH, 'configs')
    },
    modules: [(0, _path.resolve)(CWD, 'node_modules'), (0, _path.resolve)(CWD, '../../node_modules'), 'node_modules'],
    extensions: ['.ts', '.tsx', '.js', '.jsx']
  },
  resolveLoader: {
    modules: [local_node_modules, (0, _path.resolve)(__dirname, 'loaders'), (0, _path.resolve)(CWD, 'node_modules'), 'node_modules']
  },
  module: {
    rules: [{
      test: /\.(j|t)sx?$/,
      loader: 'babel-loader',
      // TODO: Remove this cosmic- rule when packages get pre-built
      // exclude: /node_modules\/(?!cosmic-|pfm-|clm-|mms-)/,
      exclude: /node_modules[\\/](?!(bfm-|pfm-|clm-|mms-|admin-|portal-|@yaireo))/,
      query: {
        sourceType: 'unambiguous',
        cacheDirectory: true,
        presets: [[require.resolve('@babel/preset-env'), {
          targets: {
            browsers: ['last 2 versions', 'ie >= 11']
          },
          modules: false,
          //forceAllTransforms: true, // TODO: check if babel-minify  is a better option instead of uglify
          useBuiltIns: 'usage',
          corejs: {
            version: 3,
            proposals: true
          }
        }], require.resolve('@babel/preset-typescript'), require.resolve('@babel/preset-react')],
        plugins: (0, _concat.default)(babelLoaderPlugins).call(babelLoaderPlugins, (0, _map.default)(_context = ['babel-plugin-macros', '@babel/plugin-syntax-dynamic-import', '@babel/plugin-transform-async-to-generator', '@babel/plugin-proposal-export-namespace-from', '@babel/plugin-proposal-export-default-from', '@babel/plugin-proposal-object-rest-spread', 'babel-plugin-jsx-control-statements', '@babel/plugin-transform-runtime', 'babel-plugin-lodash']).call(_context, require.resolve)),
        env: {
          development: {
            plugins: [require.resolve('react-hot-loader/babel')]
          }
        }
      }
    }, {
      test: /\.(jpe?g|png|gif|ico)$/i,
      loader: 'file-loader?name=images/[name].[ext]'
    }, {
      test: /\.module.less$/i,
      //loader: 'style-loader!css-loader?modules&localIdentName=[name]__[local]!less-loader',
      loader: _webpack.ExtractTextPlugin.extract({
        fallback: 'style-loader',
        use: [{
          loader: 'css-loader',
          options: {
            modules: true,
            localIdentName: '[name]__[local]'
          }
        }, {
          loader: 'less-loader'
        }],
        publicPath: '../'
      })
    }, {
      test: /^(?!.*\.module).*\.less.*$/i,
      // loader: 'style-loader!css-loader!less-loader',
      loader: _webpack.ExtractTextPlugin.extract({
        fallback: 'style-loader',
        use: [{
          loader: 'css-loader'
        }, {
          loader: 'less-loader'
        }],
        publicPath: '../'
      })
    }, {
      test: /\.css$/,
      //loader: 'style-loader!css-loader',
      loader: _webpack.ExtractTextPlugin.extract({
        fallback: 'style-loader',
        use: 'css-loader',
        publicPath: '../'
      })
    }, {
      test: /\.(woff|woff2|svg|ttf|eot)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
      loader: 'file-loader?name=fonts/[name].[ext]',
      exclude: [/css/, /images/],
      include: /fonts/
    }, {
      test: /\.svg$/,
      loader: 'svg-inline-loader'
    }]
  },
  plugins: [_yargs.argv.buildTime ? _webpackBuildTime.default : () => {}, new _webpack.ExtractTextPlugin({
    filename: '[name].[chunkhash].css',
    allChunks: true
  }), //new webpack.NoEmitOnErrorsPlugin(),
  // new BundleAnalyzerPlugin({
  // 	generateStatsFile: true,
  // }), // This can be used to visually get package sizes
  new _webpack.ProgressBarWebpackPlugin({
    clear: false
  }), //new webpack.NamedModulesPlugin(),
  new _webpack.HtmlWebpackPlugin({
    title: `${TITLE} (${NODE_ENV})`,
    excludeChunks: ['Silent_renew'],
    inject: 'body',
    hash: true,
    meta: {
      version: pkg.version
    }
  }), new _webpack.CopyWebpackPlugin([{
    from: (0, _path.join)(process.cwd(), '/config/config.json'),
    to: (0, _path.join)(process.cwd(), '/_dist/config/config.json')
  }])],
  optimization: {
    sideEffects: true,
    minimizer: [new _webpack.TerserPlugin({
      cache: true,
      sourceMap: true,
      parallel: true
    })],
    runtimeChunk: 'single',
    splitChunks: {
      chunks: 'all',
      minSize: 30000,
      maxSize: 0,
      minChunks: 1,
      maxAsyncRequests: 5,
      maxInitialRequests: 3,
      automaticNameDelimiter: '-',
      name: true,
      cacheGroups: {
        vendors: {
          test: /[\\/]node_modules[\\/]/,
          priority: -10
        },
        default: {
          minChunks: 2,
          priority: -20,
          reuseExistingChunk: true
        }
      }
    }
  }
};

if (_yargs.argv.buildTime) {
  (0, _debug.default)('webpack:config')('Adding build time plugin');
}

if (_yargs.argv.cache) {
  (0, _debug.default)('webpack:config')('Webpack caching plugin turned on');
}

var _default = config;
exports.default = _default;