From d9b801894a704c4088528e8330ae4c7290bd9acd Mon Sep 17 00:00:00 2001 From: Artem Guskov <61526280+brendan8c@users.noreply.github.com> Date: Wed, 7 Jul 2021 21:14:15 +0300 Subject: [PATCH 1/3] Terser for Gulp I wrote some code that allows to use the 'terser' plugin in the 'gulp' build. There is no longer any need to use additional plugins for this. --- README.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/README.md b/README.md index ba5788a06..a8968f051 100644 --- a/README.md +++ b/README.md @@ -524,6 +524,34 @@ try { // Do something with error } ``` +## Terser for Gulp +An example of how you can use the Terser plugin in your Gulp build. +```javascript +const { src, dest, series } = require('gulp'); +const { minify } = require('terser'); + +function js() { + const options = { + parse: { + bare_returns: true, // (default false) -- support top level return statements. + html5_comments: true, // (default true) + shebang: true // (default true) -- support #!command as the first line. + } + }; + return src('app/**/*.js') + .on('data', function(file) { + async function getAllBooks() { + const result = await minify(file.contents.toString(), options); + return await minify(result) + } + (async function() { + file.contents = Buffer.from(JSON.parse(Buffer.from(JSON.stringify(await getAllBooks()))).code) + })(); + }) + .pipe(dest('build')) +} +exports.js = series(js) +``` ## Minify options From 92cb26ba0ed86ded577d0099cc28def026f535a4 Mon Sep 17 00:00:00 2001 From: Artem Guskov <61526280+brendan8c@users.noreply.github.com> Date: Wed, 7 Jul 2021 21:23:43 +0300 Subject: [PATCH 2/3] Terser for Gulp (Added changes. Changed the name of the functions.) I wrote some code that allows to use the 'terser' plugin in the 'gulp' build. There is no longer any need to use additional plugins for this. --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a8968f051..485af0ad3 100644 --- a/README.md +++ b/README.md @@ -540,12 +540,12 @@ function js() { }; return src('app/**/*.js') .on('data', function(file) { - async function getAllBooks() { + async function getJs() { const result = await minify(file.contents.toString(), options); return await minify(result) } (async function() { - file.contents = Buffer.from(JSON.parse(Buffer.from(JSON.stringify(await getAllBooks()))).code) + file.contents = Buffer.from(JSON.parse(Buffer.from(JSON.stringify(await getJs()))).code) })(); }) .pipe(dest('build')) From 8ff0aa01f039c050d21e7200f13c3d2359e35415 Mon Sep 17 00:00:00 2001 From: Artem Guskov <61526280+brendan8c@users.noreply.github.com> Date: Sat, 17 Jul 2021 15:44:01 +0300 Subject: [PATCH 3/3] Terser for Gulp Added error handling --- README.md | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 485af0ad3..fa9a464d6 100644 --- a/README.md +++ b/README.md @@ -545,7 +545,16 @@ function js() { return await minify(result) } (async function() { - file.contents = Buffer.from(JSON.parse(Buffer.from(JSON.stringify(await getJs()))).code) + try { + file.contents = Buffer.from(JSON.parse(Buffer.from(JSON.stringify(await getJs()))).code) + } catch (error) { + const { message, line, col, pos } = error + console.log('message: ' + message) + console.log('filename: ' + file.basename) + console.log('line: ' + line) + console.log('col: ' + col) + console.log('pos: ' + pos) + } })(); }) .pipe(dest('build'))