{"size": 1548, "ext": "js", "max_stars_repo_path": "packages/sand-build/src/webpack/config/loader/jsLoader.js", "max_stars_repo_name": "zirupay/sand-build", "max_stars_repo_stars_event_min_datetime": "2020-04-14T17:46:30.000Z", "max_stars_repo_stars_event_max_datetime": "2020-04-23T11:57:16.000Z", "max_issues_repo_path": "packages/sand-build/src/webpack/config/loader/jsLoader.js", "max_issues_repo_name": "zirupay/sand-build", "max_issues_repo_issues_event_min_datetime": "2020-04-22T12:58:31.000Z", "max_issues_repo_issues_event_max_datetime": "2021-09-02T09:47:53.000Z", "max_forks_repo_path": "packages/sand-build/src/webpack/config/loader/jsLoader.js", "max_forks_repo_name": "372623460jh/sand", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 2, "max_issues_count": 9, "max_forks_count": null, "avg_line_length": 18.2117647059, "max_line_length": 56, "alphanum_fraction": 0.5620155039}
const {
  getWebpackBabelConfig,
} = require('../../../common/getWebpackBabelConfig');

/**
 * 获取jsloader
 * @param {*} isProd 是否生产环境
 */
function getJsLoader(opts) {
  // 外部传入的babel配置
  const { webpackOptions = {} } = opts;
  const { babelConfig } = webpackOptions;

  let baseBabelConfig = getWebpackBabelConfig();
  if (babelConfig) {
    baseBabelConfig = babelConfig(baseBabelConfig);
  }

  return [
    {
      // js jsx
      test: /\.(js|jsx)$/,
      loader: 'babel-loader',
      // @jianghe/sand-build 也需要过loader
      exclude: /node_modules(?!\/@jianghe\/sand-build)/,
      // 有babelConfig使用babelConfig,否则使用默认
      options: baseBabelConfig,
    },
  ];
}

/**
 * 获取ts tsx loader
 */
function getTsLoader() {
  return [
    {
      test: /\.ts(x?)$/,
      // 先ts转es6再使用babel
      use: [
        {
          loader: 'ts-loader',
        },
      ],
      exclude: '/node_modules/',
    },
  ];
}

/**
 * 获取ts tsx需要过babel
 */
function getTsLoaderShouldBable(opts) {
  const { webpackOptions = {} } = opts;
  const { babelConfig } = webpackOptions;

  let baseBabelConfig = getWebpackBabelConfig();
  if (babelConfig) {
    baseBabelConfig = babelConfig(baseBabelConfig);
  }

  return [
    {
      test: /\.ts(x?)$/,
      // 先ts转es6再使用babel
      use: [
        {
          loader: 'babel-loader',
          options: baseBabelConfig,
        },
        {
          loader: 'ts-loader',
        },
      ],
      exclude: '/node_modules/',
    },
  ];
}

module.exports = {
  getJsLoader,
  getTsLoader,
  getTsLoaderShouldBable,
};
