From 4f0d1fe931b0f0052a87c433ac1ed932163cfa61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hs=C3=BCan?= <8325632+up9cloud@users.noreply.github.com> Date: Sat, 22 Jun 2024 20:26:44 -0700 Subject: [PATCH 1/2] Update copy.md --- src/docs/copy.md | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/src/docs/copy.md b/src/docs/copy.md index 18f2aebd57..589b39070c 100644 --- a/src/docs/copy.md +++ b/src/docs/copy.md @@ -207,6 +207,52 @@ 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 transform(ext, raw) { + switch (ext) { + case ".js": { + const minified = await jsMinify(raw, {}); + return minified.code; + } + default: + return raw; + } + } + let copyOptions = { + 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) { + transform(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).