<commit_before>'use strict';

const path = require('path');
const extend = require('extend');

const isObject = require('../utils/typeof').isObject;

let resourceQuery = require('../utils/resource-query');
const appendModuleRule = require('../utils/appendModuleRule');

const regFile = /\.(jp?eg|png|gif)$/i;

const IS_PRODUCTION_MODE = process.env.NODE_ENV === 'production';

const DEFAULT_CONFIG = {
    mozjpeg: {
        quality: 75
    },
    pngquant: {
        quality: '65-90',
        speed: 4
    },
    svgo:{
        plugins: [{
            removeViewBox: false
        }, {
            removeEmptyAttrs: false
        }]
    }
};

module.exports = function (options, config) {
    const LOADER_SLICE_MAX = IS_PRODUCTION_MODE ? 2 : 1;

    const OUTPUT_IMAGE_NAME = path.join(config.output.imageDir, '[name]' + (IS_PRODUCTION_MODE ? '-[hash]' : '') + '.[ext]');

    let imageWebpackLoaderOptions = isObject(config.imagemin) ?
        extend(true, {}, DEFAULT_CONFIG, config.imagemin) :
        extend(true, {}, DEFAULT_CONFIG);

    config = appendModuleRule(config, [{
        // 图片 内联模式
        test: regFile,
        resourceQuery: resourceQuery.isInline,
        use: [{
            loader: 'url-loader',
            options: {
                limit: 0, name: OUTPUT_IMAGE_NAME
            }
        }, {
            loader: 'image-webpack-loader',
            options: imageWebpackLoaderOptions
        }].slice(0, LOADER_SLICE_MAX)
    }, {
        // 图片 链接文件模式
        test: regFile,
        resourceQuery: resourceQuery.isUrl,
        use: [{
            loader: 'file-loader',
            options: {
                name: OUTPUT_IMAGE_NAME
            }
        }, {
            loader: 'image-webpack-loader',
            options: imageWebpackLoaderOptions
        }].slice(0, LOADER_SLICE_MAX)
    }, {
        // 图片 默认处理是内联模式 >8kb文件走链接
        test: regFile,
        use: [{
            loader: 'url-loader',
            options: {
                limit: 8192, name: OUTPUT_IMAGE_NAME
            }<commit_msg>feat(图片): 图片处理流程<commit_after>'use strict';

const path = require('path');
const extend = require('extend');

const isObject = require('../utils/typeof').isObject;
const env = require('../utils/env');
const resourceQuery = require('../utils/resource-query');
const appendModuleRule = require('../utils/appendModuleRule');

const regFile = /\.(jp?eg|png|gif)$/i;


const DEFAULT_CONFIG = {
    mozjpeg: {
        quality: 75
    },
    pngquant: {
        quality: '65-90',
        speed: 4
    },
    svgo:{
        plugins: [{
            removeViewBox: false
        }, {
            removeEmptyAttrs: false
        }]
    }
};

module.exports = function (options, config) {
    const isProd = env.isProd();
    const LOADER_SLICE_MAX = isProd ? 2 : 1;

    const OUTPUT_IMAGE_NAME = path.join(config.output.imageDir, '[name]' + (isProd ? '-[hash]' : '') + '.[ext]');

    let imageWebpackLoaderOptions = isObject(config.imagemin) ?
        extend(true, {}, DEFAULT_CONFIG, config.imagemin) :
        extend(true, {}, DEFAULT_CONFIG);

    config = appendModuleRule(config, [{
        // 图片 内联模式
        test: regFile,
        resourceQuery: resourceQuery.isInline,
        use: [{
            loader: require.resolve('url-loader'),
            options: {
                limit: 0, name: OUTPUT_IMAGE_NAME
            }
        }, {
            loader: require.resolve('image-webpack-loader'),
            options: imageWebpackLoaderOptions
        }].slice(0, LOADER_SLICE_MAX)
    }, {
        // 图片 链接文件模式
        test: regFile,
        resourceQuery: resourceQuery.isUrl,
        use: [{
            loader: require.resolve('file-loader'),
            options: {
                name: OUTPUT_IMAGE_NAME
            }
        }, {
            loader: require.resolve('image-webpack-loader'),
            options: imageWebpackLoaderOptions
        }].slice(0, LOADER_SLICE_MAX)
    }, {
        // 图片 默认处理是内联模式 >8kb文件走链接
        test: regFile,
        use: [{
            loader: 'url-loader',
            options: {
                limit: 8192, name: OUTPUT_IMAGE_NAME
            }
{'fraction_non_alphanumeric': 0.1341581756428918, 'fraction_numerical': 0.008248423095584668, 'mean_word_length': 1.9576757532281206, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5134474', 'n_tokens_mistral': 1312, 'n_tokens_neox': 1285, 'n_words': 237}