jquery/build/tasks/minify.js
Timmy Willison ec8802bafe
Build: migrate most grunt tasks off of grunt (3.x)
Close gh-5330

- lint
- npmcopy
- build, minify, and process for distribution.
- new custom build command using yargs
- compare size of minified/gzip built files
- pretest scripts, including qunit-fixture, babel transpilation, and npmcopy
- node smoke tests
- promises aplus tests
- new watch task using nodemon, which runs `npm run build:all` on `src` changes.

Also:

- upgraded husky and added the new lint command
- updated lint config to use new "flat" config format.
	See https://eslint.org/docs/latest/use/configure/configuration-files-new
- Temporarily disabled one lint rule until flat config is
	supported by eslint-plugin-import.
	See https://github.com/import-js/eslint-plugin-import/issues/2556
- committed package-lock.json
- updated all test scripts to use the new build
- added an express test server that uses middleware-mockserver
	this can be used to run tests without karma
- build-all-variants is now build:all
- run pretest script in jenkins

---------

Co-authored-by: Michał Gołębiowski-Owczarek <m.goleb@gmail.com>
2023-09-20 18:18:42 -04:00

84 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.` );
};