Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Terser for Gulp #1035

Merged
merged 1 commit into from Aug 22, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
```