Skip to content

Commit

Permalink
Terser for Gulp (#1035)
Browse files Browse the repository at this point in the history
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
brendan8c committed Aug 22, 2021
1 parent 622654c commit c7cd53d
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions RECIPES.md
@@ -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)
```

0 comments on commit c7cd53d

Please sign in to comment.