Skip to content

Commit e242187

Browse files
authored
Build: Switch form Terser to SWC for JS minification (#5286)
Also, as part of this, fix the `file` & `sources` properties of the source map file. Fixes gh-5285 Closes gh-5286 Ref gh-5258
1 parent 198b41c commit e242187

File tree

Image for: File tree

5 files changed

Image for: 5 files changed
+88
-19
lines changed

5 files changed

Image for: 5 files changed
+88
-19
lines changed

‎Gruntfile.js

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -312,27 +312,41 @@ module.exports = function( grunt ) {
312312
files: [ "<%= eslint.dev.src %>" ],
313313
tasks: [ "dev" ]
314314
},
315-
terser: {
315+
minify: {
316316
all: {
317317
files: {
318318
"dist/<%= grunt.option('filename').replace('.js', '.min.js') %>":
319319
"dist/<%= grunt.option('filename') %>"
320320
},
321321
options: {
322-
ecma: 5,
323322
sourceMap: {
324-
filename: "dist/<%= grunt.option('filename').replace('.js', '.min.map') %>"
325-
},
326-
format: {
327-
ascii_only: true,
328-
comments: false,
329-
preamble: "/*! jQuery v<%= pkg.version %> | " +
330-
"(c) OpenJS Foundation and other contributors | " +
331-
"jquery.org/license */"
323+
filename: "dist/<%= grunt.option('filename').replace('.js', '.min.map') %>",
324+
325+
// The map's `files` & `sources` property are set incorrectly, fix
326+
// them via overrides from the task config.
327+
// See https://github.com/swc-project/swc/issues/7588#issuecomment-1624345254
328+
overrides: {
329+
file: "jquery.min.js",
330+
sources: [
331+
"jquery.js"
332+
]
333+
}
332334
},
333-
compress: {
334-
hoist_funs: false,
335-
loops: false
335+
swc: {
336+
format: {
337+
ecma: 5,
338+
asciiOnly: true,
339+
comments: false,
340+
preamble: "/*! jQuery v4.0.0-pre | " +
341+
"(c) OpenJS Foundation and other contributors | " +
342+
"jquery.org/license */\n"
343+
},
344+
compress: {
345+
ecma: 5,
346+
hoist_funs: false,
347+
loops: false
348+
},
349+
mangle: true
336350
}
337351
}
338352
}
@@ -400,7 +414,7 @@ module.exports = function( grunt ) {
400414
grunt.registerTask( "dev", [
401415
"build:*:*",
402416
runIfNewNode( "newer:eslint:dev" ),
403-
"newer:terser",
417+
"newer:minify",
404418
"remove_map_comment",
405419
"dist:*",
406420
"qunit_fixture",
@@ -410,7 +424,7 @@ module.exports = function( grunt ) {
410424
grunt.registerTask( "default", [
411425
runIfNewNode( "eslint:dev" ),
412426
"build:*:*",
413-
"terser",
427+
"minify",
414428
"remove_map_comment",
415429
"dist:*",
416430
"test:prepare",

‎build/tasks/build.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,6 @@ module.exports = function( grunt ) {
339339
"";
340340

341341
grunt.log.writeln( "Creating custom build...\n" );
342-
grunt.task.run( [ "build:*:*" + ( modules ? ":" + modules : "" ), "terser", "dist" ] );
342+
grunt.task.run( [ "build:*:*" + ( modules ? ":" + modules : "" ), "minify", "dist" ] );
343343
} );
344344
};

‎build/tasks/minify.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* Minify JavaScript using SWC.
3+
*/
4+
5+
"use strict";
6+
7+
module.exports = ( grunt ) => {
8+
const swc = require( "@swc/core" );
9+
10+
grunt.registerMultiTask(
11+
"minify",
12+
"Minify JavaScript using SWC",
13+
async function() {
14+
const done = this.async();
15+
const options = this.options();
16+
const sourceMapFilename = options.sourceMap && options.sourceMap.filename;
17+
const sourceMapOverrides = options.sourceMap && options.sourceMap.overrides || {};
18+
19+
await Promise.all( this.files.map( async( { src, dest } ) => {
20+
if ( src.length !== 1 ) {
21+
grunt.fatal( "The minify task requires a single source per destination" );
22+
}
23+
24+
const { code, map: incompleteMap } = await swc.minify(
25+
grunt.file.read( src[ 0 ] ),
26+
{
27+
...options.swc,
28+
inlineSourcesContent: false,
29+
sourceMap: sourceMapFilename ?
30+
{
31+
filename: sourceMapFilename
32+
} :
33+
false
34+
}
35+
);
36+
37+
grunt.file.write( dest, code );
38+
39+
if ( sourceMapFilename ) {
40+
41+
// Apply map overrides if needed. See the task config description
42+
// for more details.
43+
const mapObject = {
44+
...JSON.parse( incompleteMap ),
45+
...sourceMapOverrides
46+
};
47+
const map = JSON.stringify( mapObject );
48+
49+
grunt.file.write( sourceMapFilename, map );
50+
}
51+
} ) );
52+
53+
done();
54+
}
55+
);
56+
};

‎build/tasks/sourcemap.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
var fs = require( "fs" );
44

55
module.exports = function( grunt ) {
6-
var config = grunt.config( "terser.all.files" );
6+
var config = grunt.config( "minify.all.files" );
77
grunt.registerTask( "remove_map_comment", function() {
88
var minLoc = grunt.config.process( Object.keys( config )[ 0 ] );
99

‎package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"devDependencies": {
2727
"@babel/core": "7.10.5",
2828
"@babel/plugin-transform-for-of": "7.10.4",
29+
"@swc/core": "1.3.66",
2930
"colors": "1.4.0",
3031
"commitplease": "3.2.0",
3132
"core-js-bundle": "3.6.5",
@@ -42,7 +43,6 @@
4243
"grunt-karma": "4.0.2",
4344
"grunt-newer": "1.3.0",
4445
"grunt-npmcopy": "0.2.0",
45-
"grunt-terser": "2.0.0",
4646
"gzip-js": "0.3.2",
4747
"husky": "4.2.5",
4848
"jsdom": "19.0.0",
@@ -67,7 +67,6 @@
6767
"rollup": "2.21.0",
6868
"sinon": "7.3.1",
6969
"strip-json-comments": "3.1.1",
70-
"terser": "5.17.6",
7170
"testswarm": "1.1.2"
7271
},
7372
"scripts": {

0 commit comments

Image for: 0 commit comments
Comments
 (0)