"use strict";

var _path = _interopRequireDefault(require("path"));

var _utils = require("../utils");

var _common = require("../common");

var _pluginUtils = require("../pluginUtils");

var _loaderUtils = require("../loaderUtils");

var _configsAssetsLoaders = require("../loaderUtils/configsAssetsLoaders");

var _resolvers = require("./resolvers");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

const options = (0, _utils.getOptions)();
const {
  app: {
    folder,
    context,
    server,
    outputFolder,
    plugins,
    exclude,
    patterns,
    mediaQueryHoverActiveString,
    cssVariableReplacementConfig,
    selectorWeightConfig,
    cssUniqueness,
    customClassNamePrefix,
    seperateCssModules,
    changeRuntimeChunkChar,
    // we are going to use this for fonts file name collide in issue in devmode
    enableChunkHash,
    cssHashSelectors,
    classNamePrefix,
    selectorReplace,
    devConsoleExculde,
    sourcemap,
    crossorigin,
    postCssPluginOrder,
    externals,
    enableMjsLoader
  }
} = options;
const {
  disableContextURL
} = server;
const appPath = process.cwd();
const contextURL = disableContextURL ? '' : context;
const serverUrl = (0, _utils.getServerURL)(server, 'https');
const output = {
  path: _path.default.join(appPath, outputFolder),
  filename: 'js/[name].js',
  chunkFilename: 'js/[name].js',
  publicPath: `${[serverUrl, contextURL].filter(a => a).join('/')}/`,
  jsonpFunction: `${context}Jsonp`
};
const nameTemplate = (0, _configsAssetsLoaders.createNameTemplate)(enableChunkHash);

if (crossorigin) {
  output.crossOriginLoading = 'anonymous';
}

module.exports = {
  entry: (0, _common.getEntries)(options, 'dev'),
  devtool: sourcemap,
  mode: 'development',
  // watchOptions: {
  //   ignored: /node_modules.(?!@zohodesk)/
  // },
  output,
  stats: options.app.disableWatch ? 'errors-only' : {
    children: false
  },
  optimization: {
    splitChunks: _common.splitChunks,
    moduleIds: 'named',
    runtimeChunk: {
      name: entrypoint => changeRuntimeChunkChar ? `./runtime-${entrypoint.name}` : `./runtime~${entrypoint.name}`
    }
  },
  plugins: (0, _pluginUtils.getDevPlugins)(options, output.publicPath),
  module: {
    /* strictExportPresence for break the build when imported module not present in respective file */
    strictExportPresence: true,
    rules: [{
      test: /\.js$/,
      use: (0, _loaderUtils.getDevJsLoaders)(options),
      include: module => {
        const srcPath = _path.default.join(appPath, folder);

        const depsPath = _path.default.join(appPath, 'node_modules', '@zohodesk');

        if (module.includes(srcPath) || devConsoleExculde && module.includes(depsPath)) {
          return true;
        }

        return false;
      }
    }, enableMjsLoader ? {
      test: /\.mjs$/,
      include: /node_modules/,
      type: 'javascript/auto'
    } : null, seperateCssModules ? {
      test: /\.css$/,
      exclude: /\.module\.css$/,
      use: (0, _loaderUtils.getCSSLoaders)({
        plugins,
        exclude,
        patterns,
        mediaQueryHoverActiveString,
        cssVariableReplacementConfig,
        selectorWeightConfig,
        customClassNamePrefix,
        classNameBlob: '[local]',
        cssUniqueness: false,
        selectorReplace: null,
        postCssPluginOrder
      })
    } : null, {
      test: seperateCssModules ? /\.module\.css$/ : /(\.module)?\.css$/,
      use: (0, _loaderUtils.getCSSLoaders)({
        plugins,
        exclude,
        patterns,
        mediaQueryHoverActiveString,
        cssVariableReplacementConfig,
        selectorWeightConfig,
        classNameBlob: null,
        cssUniqueness,
        customClassNamePrefix,
        selectorReplace,
        cssHashSelectors,
        classNamePrefix,
        postCssPluginOrder
      })
    }, (0, _configsAssetsLoaders.configImageLoader)(nameTemplate), (0, _configsAssetsLoaders.configFontLoader)(nameTemplate), (0, _configsAssetsLoaders.configSVGLoader)(nameTemplate), (0, _configsAssetsLoaders.configVideoLoader)(nameTemplate), (0, _configsAssetsLoaders.configAudioLoader)(nameTemplate), {
      test: /\.tmpl$/,
      use: [{
        loader: 'html-loader',
        options: {
          minimize: true
        }
      }]
    }, {
      test: /\.worker.js$/,
      use: {
        loader: require.resolve('../loaders/workerLoader.js'),
        options: {
          esModule: true,
          filename: 'js/[name].js'
        }
      }
    }].filter(Boolean)
  },
  externals: Object.assign({
    ZC: '$ZC'
  }, externals),
  resolve: (0, _resolvers.moduleResolver)(options),
  resolveLoader: (0, _resolvers.loaderResolver)(options)
};