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

Add an example for minifying .js files for copy.md #1707

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/docs/copy.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,53 @@ module.exports = function (eleventyConfig) {
};
```

{% codetitle ".eleventy.js" %}

```js
const path = require('path')
const { minify: jsMinify } = require("terser")
const duplexify = require('duplexify')
const concatStream = require('concat-stream')
const from2String = require('from2-string')
module.exports = function (eleventyConfig) {
async function minify(ext, raw) {
switch (ext) {
case ".js": {
const minified = await jsMinify(raw, {});
return minified.code;
}
default:
return raw;
}
}
let copyOptions = {
// transform files: minify all .js files
transform: function(src, dest, stats) {
const ext = path.extname(src)
switch (ext) {
case '.js': {
break
}
default: return null
}
const stream = duplexify()
const writer = concatStream({ encoding: 'string' }, function (raw) {
minify(ext, raw)
.then(minified => {
stream.setReadable(from2String(minified))
})
.catch(e => {
stream.emit('error', e)
})
})
stream.setWritable(writer)
return stream
}
};
eleventyConfig.addPassthroughCopy({ "./static/": "/" }, copyOptions);
};
```

Review the [full list of options on the `recursive-copy` GitHub repository](https://github.com/timkendrick/recursive-copy#usage).

<div class="youtube-related">
Expand Down