Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
I wrote some code that allows using the 'terser' plugin in the 'gulp' build. There is no longer any need to use additional plugins for this.
- Loading branch information
Showing
1 changed file
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
## 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 getJs() { | ||
const result = await minify(file.contents.toString(), options); | ||
return await minify(result) | ||
} | ||
(async function() { | ||
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')) | ||
} | ||
exports.js = series(js) | ||
``` |