File size: 4,681 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
const babelPlugins = require( '@babel/compat-data/plugins' );
const browserslist = require( 'browserslist' );
const CssMinimizerPlugin = require( 'css-minimizer-webpack-plugin' );
const semver = require( 'semver' );
const TerserPlugin = require( 'terser-webpack-plugin' );
const supportedBrowsers = browserslist();
// The list of browsers to check, that are supported by babel compat-data.
// Babel compat-data also includes non-browser environments, which we want to exclude.
const browsersToCheck = [ 'chrome', 'opera', 'edge', 'firefox', 'safari', 'ios', 'ie' ];
// Check if a feature is supported by all browsers in the provided browserslist.
function isFeatureSupported( feature, browsers ) {
const featureMinVersions = babelPlugins[ feature ];
for ( const featureBrowser of browsersToCheck ) {
let featureRange;
if ( ! featureMinVersions[ featureBrowser ] ) {
// No browser entry, which means no version of the browser supports the feature.
featureRange = '>=999999';
} else {
featureRange = `>=${ featureMinVersions[ featureBrowser ] }`;
}
const listRanges = browsers.filter( ( b ) => b.startsWith( featureBrowser ) );
for ( let listRange of listRanges ) {
// Remove browser name from range.
listRange = listRange.split( ' ' )[ 1 ];
// Massage range syntax into something `semver` accepts.
listRange = listRange.replace( '-', ' - ' );
if ( ! semver.subset( listRange, featureRange ) ) {
return false;
}
}
}
return true;
}
/**
* Auxiliary method to help in picking an ECMAScript version based on a list
* of supported browser versions.
*
* If Terser ever supports `browserslist`, this method will no longer be needed
* and the world will be a better place.
* @param {Array<string>} browsers The list of supported browsers.
* @returns {number} The maximum supported ECMAScript version.
*/
function chooseTerserEcmaVersion( browsers ) {
// Test for ES2015 features. If missing fall back to ES5.
if (
! isFeatureSupported( 'transform-arrow-functions', browsers ) ||
! isFeatureSupported( 'transform-classes', browsers )
) {
return 5;
}
// Test for ES2016 features. If missing fall back to ES2015.
if ( ! isFeatureSupported( 'transform-exponentiation-operator', browsers ) ) {
return 2015;
}
// Test for ES2017 features. If missing fall back to ES2016.
if ( ! isFeatureSupported( 'transform-async-to-generator', browsers ) ) {
return 2016;
}
// Test for ES2018 features. If missing fall back to ES2017.
if (
! isFeatureSupported( 'proposal-object-rest-spread', browsers ) ||
! isFeatureSupported( 'transform-named-capturing-groups-regex', browsers ) ||
! isFeatureSupported( 'proposal-unicode-property-regex', browsers )
) {
return 2017;
}
// Test for ES2019 features. If missing fall back to ES2018.
if ( ! isFeatureSupported( 'proposal-optional-catch-binding', browsers ) ) {
return 2018;
}
// Test for ES2020 features. If missing fall back to ES2019.
if (
! isFeatureSupported( 'proposal-optional-chaining', browsers ) ||
! isFeatureSupported( 'proposal-nullish-coalescing-operator', browsers )
) {
return 2019;
}
// Looks like everything we tested for is supported, so default to latest
// available ES spec that Terser can handle.
return 2020;
}
/**
* Returns an array containing a Terser plugin object to be used in Webpack minification.
* @see https://github.com/webpack-contrib/terser-webpack-plugin for complete descriptions of options.
* @param {Object} options Options
* @param options.terserOptions Options for Terser plugin
* @param options.cssMinimizerOptions Options for CSS Minimizer plugin
* @param options.extractComments Whether to extract comments into a separate LICENSE file (defaults to true)
* @param options.parallel Whether to run minifiers in parallel (defaults to true)
* @returns {Object[]} Terser plugin object to be used in Webpack minification.
*/
module.exports = ( {
terserOptions = {},
cssMinimizerOptions = {},
parallel = true,
extractComments = true,
} = {} ) => {
terserOptions = {
compress: true,
mangle: {
reserved: [ '__', '_n', '_nx', '_x' ],
},
ecma: chooseTerserEcmaVersion( supportedBrowsers ),
safari10: supportedBrowsers.some(
( browser ) => browser.includes( 'safari 10' ) || browser.includes( 'ios_saf 10' )
),
...terserOptions,
};
cssMinimizerOptions = {
preset: 'default',
...cssMinimizerOptions,
};
return [
new TerserPlugin( {
// SWC handles parallelization internally.
parallel,
extractComments,
terserOptions,
minify: TerserPlugin.swcMinify,
} ),
new CssMinimizerPlugin( { parallel, minimizerOptions: cssMinimizerOptions } ),
];
};
|