Skip to content

Commit

Permalink
Rename AutoCopy to HtmlRelativeCopy
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Dec 11, 2024
1 parent fcbadd4 commit 8bd3b97
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AutoCopy } from "../Util/AutoCopy.js";
import { HtmlRelativeCopy } from "../Util/HtmlRelativeCopy.js";

// one AutoCopy instance per entry
// one HtmlRelativeCopy instance per entry
function init(eleventyConfig, options) {
let opts = Object.assign(
{
Expand All @@ -13,41 +13,40 @@ function init(eleventyConfig, options) {
options,
);

let ac = new AutoCopy();
ac.setUserConfig(eleventyConfig);
ac.addMatchingGlob(opts.match);
ac.setFailOnError(opts.failOnError);
ac.setCopyOptions(opts.copyOptions);
let htmlrel = new HtmlRelativeCopy();
htmlrel.setUserConfig(eleventyConfig);
htmlrel.addMatchingGlob(opts.match);
htmlrel.setFailOnError(opts.failOnError);
htmlrel.setCopyOptions(opts.copyOptions);

eleventyConfig.htmlTransformer.addUrlTransform(
opts.extensions,
function (targetFilepathOrUrl) {
// @ts-ignore
ac.copy(targetFilepathOrUrl, this.page.inputPath, this.page.outputPath);
htmlrel.copy(targetFilepathOrUrl, this.page.inputPath, this.page.outputPath);

// TODO front matter option for manual copy

return targetFilepathOrUrl;
},
{
enabled: () => ac.isEnabled(),
enabled: () => htmlrel.isEnabled(),
// - MUST run after other plugins but BEFORE HtmlBase plugin
priority: -1,
},
);

ac.addPaths(opts.paths);
htmlrel.addPaths(opts.paths);
}

function AutoCopyPlugin(eleventyConfig) {
function HtmlRelativeCopyPlugin(eleventyConfig) {
// Important: if this is empty, no URL transforms are added
for (let options of eleventyConfig.autoCopies) {
for (let options of eleventyConfig.passthroughCopiesHtmlRelative) {
init(eleventyConfig, options);
}
}

Object.defineProperty(AutoCopyPlugin, "eleventyPackage", {
value: "@11ty/eleventy/autocopy-transform-plugin",
Object.defineProperty(HtmlRelativeCopyPlugin, "eleventyPackage", {
value: "@11ty/eleventy/html-relative-copy-plugin",
});

export { AutoCopyPlugin };
export { HtmlRelativeCopyPlugin };
11 changes: 6 additions & 5 deletions src/UserConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ class UserConfig {

/** @type {object} */
this.passthroughCopies = {};
this.autoCopies = new Set();
this.passthroughCopiesHtmlRelative = new Set();

/** @type {object} */
this.layoutAliases = {};
Expand Down Expand Up @@ -792,13 +792,14 @@ class UserConfig {
* @returns {any} a reference to the `EleventyConfig` object.
*/
addPassthroughCopy(fileOrDir, copyOptions = {}) {
if (copyOptions.mode === "auto") {
if (copyOptions.mode === "html-relative") {
if (isPlainObject(fileOrDir)) {
throw new Error(
"mode: 'auto' does not yet support input -> output (objects) mapping. Please pass a string glob or an Array of string globs!",
"mode: 'html-relative' does not yet support passthrough copy objects (input -> output mapping). Use a string glob or an Array of string globs.",
);
}
this.autoCopies?.add({

this.passthroughCopiesHtmlRelative?.add({
match: fileOrDir,
...copyOptions,
});
Expand Down Expand Up @@ -1226,7 +1227,7 @@ class UserConfig {
globalData: this.globalData,
layoutAliases: this.layoutAliases,
layoutResolution: this.layoutResolution,
autoCopies: this.autoCopies,
passthroughCopiesHtmlRelative: this.passthroughCopiesHtmlRelative,
passthroughCopies: this.passthroughCopies,

// Liquid
Expand Down
10 changes: 5 additions & 5 deletions src/Util/AutoCopy.js → src/Util/HtmlRelativeCopy.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import path from "node:path";
import { TemplatePath } from "@11ty/eleventy-utils";
import isValidUrl from "../Util/ValidUrl.js";
import { isGlobMatch } from "../Util/GlobMatcher.js";
import isValidUrl from "./ValidUrl.js";
import { isGlobMatch } from "./GlobMatcher.js";

class AutoCopy {
class HtmlRelativeCopy {
#userConfig;
#matchingGlobs = new Set();
#matchingGlobsArray;
Expand Down Expand Up @@ -121,7 +121,7 @@ class AutoCopy {
if (!alias) {
if (this.#failOnError) {
throw new Error(
"Missing input file for auto-passthrough file copy: " +
"Missing input file for `html-relative` Passthrough Copy file: " +
TemplatePath.absolutePath(source),
);
}
Expand All @@ -146,4 +146,4 @@ class AutoCopy {
}
}

export { AutoCopy };
export { HtmlRelativeCopy };
4 changes: 2 additions & 2 deletions src/defaultConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { FilterPlugin as InputPathToUrlFilterPlugin } from "./Plugins/InputPathT
import { HtmlTransformer } from "./Util/HtmlTransformer.js";
import TransformsUtil from "./Util/TransformsUtil.js";
import MemoizeUtil from "./Util/MemoizeFunction.js";
import { AutoCopyPlugin } from "./Plugins/AutoCopyPlugin.js";
import { HtmlRelativeCopyPlugin } from "./Plugins/HtmlRelativeCopyPlugin.js";

/**
* @module 11ty/eleventy/defaultConfig
Expand Down Expand Up @@ -132,7 +132,7 @@ export default function (config) {
});

// Requires user configuration, so must run as second-stage
config.addPlugin(AutoCopyPlugin);
config.addPlugin(HtmlRelativeCopyPlugin);

return {
templateFormats: ["liquid", "md", "njk", "html", "11ty.js"],
Expand Down
32 changes: 16 additions & 16 deletions test/AutoCopyPluginTest.js → test/HtmlRelativeCopyTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ test("Basic usage", async (t) => {
configPath: false,
config: function (eleventyConfig) {
eleventyConfig.addPassthroughCopy("**/*.png", {
mode: "auto"
mode: "html-relative"
})

eleventyConfig.on("eleventy.passthrough", copyMap => {
Expand Down Expand Up @@ -62,7 +62,7 @@ test("More complex image path (parent dir)", async (t) => {
configPath: false,
config: function (eleventyConfig) {
eleventyConfig.addPassthroughCopy("**/*.png", {
mode: "auto"
mode: "html-relative"
})

eleventyConfig.on("eleventy.passthrough", copyMap => {
Expand Down Expand Up @@ -113,7 +113,7 @@ test("No matches", async (t) => {
configPath: false,
config: function (eleventyConfig) {
eleventyConfig.addPassthroughCopy("**/*.jpeg", {
mode: "auto"
mode: "html-relative"
})

eleventyConfig.on("eleventy.passthrough", copyMap => {
Expand Down Expand Up @@ -144,7 +144,7 @@ test("Match but does not exist (throws error)", async (t) => {
configPath: false,
config: function (eleventyConfig) {
eleventyConfig.addPassthroughCopy("**/*.png", {
mode: "auto"
mode: "html-relative"
});

eleventyConfig.on("eleventy.passthrough", copyMap => {
Expand All @@ -171,7 +171,7 @@ test("Match but does not exist (no error, using `failOnError: false`)", async (t
configPath: false,
config: function (eleventyConfig) {
eleventyConfig.addPassthroughCopy("**/*.png", {
mode: "auto",
mode: "html-relative",
failOnError: false,
})

Expand Down Expand Up @@ -204,7 +204,7 @@ test("Copying dotfiles are not allowed", async (t) => {
config: function (eleventyConfig) {
// WARNING: don’t do this
eleventyConfig.addPassthroughCopy("**/*", {
mode: "auto",
mode: "html-relative",
copyOptions: {
// debug: true,
}
Expand Down Expand Up @@ -241,7 +241,7 @@ test("Using with InputPathToUrl plugin", async (t) => {
config: function (eleventyConfig) {
// order of addPlugin shouldn’t matter here
eleventyConfig.addPassthroughCopy("**/*.{html,njk}", {
mode: "auto"
mode: "html-relative"
});

eleventyConfig.addPlugin(InputPathToUrlTransformPlugin);
Expand Down Expand Up @@ -280,7 +280,7 @@ test("Using with InputPathToUrl plugin (reverse addPlugin order)", async (t) =>
eleventyConfig.addPlugin(InputPathToUrlTransformPlugin);

eleventyConfig.addPassthroughCopy("**/*.{html,njk}", {
mode: "auto"
mode: "html-relative"
});

eleventyConfig.on("eleventy.passthrough", copyMap => {
Expand Down Expand Up @@ -315,7 +315,7 @@ test("Use with HtmlBasePlugin usage", async (t) => {
config: function (eleventyConfig) {
eleventyConfig.addPlugin(HtmlBasePlugin);
eleventyConfig.addPassthroughCopy("**/*.png", {
mode: "auto"
mode: "html-relative"
});

eleventyConfig.on("eleventy.passthrough", copyMap => {
Expand Down Expand Up @@ -367,7 +367,7 @@ test("Using with InputPathToUrl plugin and HtmlBasePlugin", async (t) => {
config: function (eleventyConfig) {
// order of addPlugin shouldn’t matter here
eleventyConfig.addPassthroughCopy("**/*.{html,njk}", {
mode: "auto"
mode: "html-relative"
});

eleventyConfig.addPlugin(InputPathToUrlTransformPlugin);
Expand Down Expand Up @@ -404,10 +404,10 @@ test("Multiple addPlugin calls (use both globs)", async (t) => {
configPath: false,
config: function (eleventyConfig) {
eleventyConfig.addPassthroughCopy("**/*.jpg", {
mode: "auto"
mode: "html-relative"
});
eleventyConfig.addPassthroughCopy("**/*.png", {
mode: "auto"
mode: "html-relative"
});

eleventyConfig.on("eleventy.passthrough", copyMap => {
Expand Down Expand Up @@ -465,7 +465,7 @@ test("Array of globs", async (t) => {
configPath: false,
config: function (eleventyConfig) {
eleventyConfig.addPassthroughCopy(["**/*.jpg", "**/*.png"], {
mode: "auto"
mode: "html-relative"
});

eleventyConfig.on("eleventy.passthrough", copyMap => {
Expand Down Expand Up @@ -526,7 +526,7 @@ test("overwrite: false", async (t) => {
configPath: false,
config: function (eleventyConfig) {
eleventyConfig.addPassthroughCopy("**/*.png", {
mode: "auto",
mode: "html-relative",
copyOptions: {
overwrite: false,
}
Expand Down Expand Up @@ -576,7 +576,7 @@ test("Input -> output remapping not yet supported (throws error)", async (t) =>
config: function (eleventyConfig) {
// not yet supported
eleventyConfig.addPassthroughCopy({"**/*.png": "yo"}, {
mode: "auto"
mode: "html-relative"
});

eleventyConfig.on("eleventy.passthrough", copyMap => {
Expand All @@ -592,7 +592,7 @@ test("Input -> output remapping not yet supported (throws error)", async (t) =>
await t.throwsAsync(async () => {
await elev.write();
}, {
message: `mode: 'auto' does not yet support input -> output (objects) mapping. Please pass a string glob or an Array of string globs!`
message: `mode: \'html-relative\' does not yet support passthrough copy objects (input -> output mapping). Use a string glob or an Array of string globs.`
});

t.is(fs.existsSync("test/stubs-autocopy/_site12/test/index.html"), false);
Expand Down

0 comments on commit 8bd3b97

Please sign in to comment.