summaryrefslogtreecommitdiff
path: root/node_modules/@11ty/eleventy/src/Plugins/HtmlRelativeCopyPlugin.js
blob: ac1391dce00bd1f569aa17f10a6cf4581b5c5cdd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { HtmlRelativeCopy } from "../Util/HtmlRelativeCopy.js";

// one HtmlRelativeCopy instance per entry
function init(eleventyConfig, options) {
	let opts = Object.assign(
		{
			extensions: "html",
			match: false, // can be one glob string or an array of globs
			paths: [], // directories to also look in for files
			failOnError: true, // fails when a path matches (via `match`) but not found on file system
			copyOptions: undefined,
		},
		options,
	);

	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
			htmlrel.copy(targetFilepathOrUrl, this.page.inputPath, this.page.outputPath);

			// TODO front matter option for manual copy
			return targetFilepathOrUrl;
		},
		{
			enabled: () => htmlrel.isEnabled(),
			// - MUST run after other plugins but BEFORE HtmlBase plugin
			priority: -1,
		},
	);

	htmlrel.addPaths(opts.paths);
}

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

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

export { HtmlRelativeCopyPlugin };