jquery/build/tasks/minify.js
Michał Gołębiowski-Owczarek e84886aa37
Build: Update ESLint-related packages, fix linting errors (3.x version)
The main change is the new rule in `eslint-config-jquery`:
`template-curly-spacing`.

Closes gh-5348
Ref jquery/eslint-config-jquery#21
Ref gh-5347
2023-11-02 00:48:05 +01:00

86 lines
2.1 KiB
JavaScript

"use strict";
const UglifyJS = require( "uglify-js" );
const fs = require( "fs" );
const path = require( "path" );
const processForDist = require( "./dist" );
const getTimestamp = require( "./lib/getTimestamp" );
const rjs = /\.js$/;
module.exports = async function minify( { dir, filename } ) {
const filepath = path.join( dir, filename );
const contents = await fs.promises.readFile( filepath, "utf8" );
const version = /jQuery JavaScript Library ([^\n]+)/.exec( contents )[ 1 ];
const banner = `/*! jQuery ${ version }` +
" | (c) OpenJS Foundation and other contributors" +
" | jquery.org/license */";
const minFilename = filename.replace( rjs, ".min.js" );
const mapFilename = filename.replace( rjs, ".min.map" );
const { code, error, map: incompleteMap, warning } = UglifyJS.minify(
contents,
{
compress: {
hoist_funs: false,
loops: false,
// Support: IE <11
// typeofs transformation is unsafe for IE9-10
// See https://github.com/mishoo/UglifyJS2/issues/2198
typeofs: false
},
output: {
ascii_only: true,
// Support: Android 4.0 only
// UglifyJS 3 breaks Android 4.0 if this option is not enabled.
// This is in lieu of setting ie for all of mangle, compress, and output
ie8: true,
preamble: banner
},
sourceMap: {
filename: minFilename
}
}
);
if ( error ) {
throw new Error( error );
}
if ( warning ) {
console.warn( warning );
}
// The map's `sources` property is set to an array index.
// Fix it by setting it to the correct filename.
const map = JSON.stringify( {
...JSON.parse( incompleteMap ),
file: minFilename,
sources: [ filename ]
} );
await Promise.all( [
fs.promises.writeFile(
path.join( dir, minFilename ),
code
),
fs.promises.writeFile(
path.join( dir, mapFilename ),
map
)
] );
// Always process files for dist
// Doing it here avoids extra file reads
processForDist( contents, filename );
processForDist( code, minFilename );
processForDist( map, mapFilename );
console.log( `[${ getTimestamp() }] ${ minFilename } ${ version } with ${
mapFilename
} created.` );
};