Skip to content
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