Skip to content

Commit

Permalink
Make pathPrefix available to plugins as eleventyConfig.pathPrefix
Browse files Browse the repository at this point in the history
Fixes #2526
  • Loading branch information
zachleat committed Aug 10, 2022
1 parent 4c74a53 commit 0bbc16d
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 10 deletions.
4 changes: 2 additions & 2 deletions cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,11 @@ try {
console.log(Eleventy.getHelp());
} else {
let elev = new Eleventy(argv.input, argv.output, {
source: "cli",
// --quiet and --quiet=true both resolve to true
quietMode: argv.quiet,
configPath: argv.config,
source: "cli",
pathPrefix: argv.pathprefix,
});

// reuse ErrorHandler instance in Eleventy
Expand All @@ -82,7 +83,6 @@ try {
elev.setIsVerbose(false);
}

elev.setPathPrefix(argv.pathprefix);
elev.setDryRun(argv.dryrun);
elev.setIncrementalBuild(argv.incremental);
elev.setPassthroughAll(argv.passthroughall);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
},
"dependencies": {
"@11ty/dependency-tree": "^2.0.1",
"@11ty/eleventy-dev-server": "^1.0.0-canary.13",
"@11ty/eleventy-dev-server": "^1.0.0-canary.14",
"@11ty/eleventy-utils": "^1.0.1",
"@iarna/toml": "^2.2.5",
"@sindresorhus/slugify": "^1.1.2",
Expand Down
10 changes: 10 additions & 0 deletions src/Eleventy.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ class Eleventy {
this.env = this.getEnvironmentVariableValues();
this.initializeEnvironmentVariables(this.env);

/**
* @member {String} - The top level directory the site pretends to reside in
* @default "/"
*/
this.pathPrefix = options.pathPrefix || "/";

if (this.pathPrefix || this.pathPrefix === "") {
this.eleventyConfig.setPathPrefix(this.pathPrefix);
}

/**
* @member {Object} - Initialize Eleventy’s configuration, including the user config file
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Filters/Url.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module.exports = function (url, pathPrefix) {

if (pathPrefix === undefined || typeof pathPrefix !== "string") {
// When you retrieve this with config.getFilter("url") it
// grabs the pathPrefix argument from your config for you.
// grabs the pathPrefix argument from your config for you (see defaultConfig.js)
throw new Error("pathPrefix (String) is required in the `url` filter.");
}

Expand Down
18 changes: 12 additions & 6 deletions src/TemplateConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,6 @@ class TemplateConfig {
setPathPrefix(pathPrefix) {
debug("Setting pathPrefix to %o", pathPrefix);
this.overrides.pathPrefix = pathPrefix;

if (!this.hasConfigMerged) {
this.forceReloadConfig();
}
this.config.pathPrefix = pathPrefix;
}

/**
Expand All @@ -179,6 +174,10 @@ class TemplateConfig {
* @returns {String} - The path prefix string
*/
getPathPrefix() {
if (this.overrides.pathPrefix || this.overrides.pathPrefix === "") {
return this.overrides.pathPrefix;
}

if (!this.hasConfigMerged) {
this.getConfig();
}
Expand All @@ -203,8 +202,9 @@ class TemplateConfig {
*
* @param {Object} - the return Object from the user’s config file.
*/
processPlugins({ dir }) {
processPlugins({ dir, pathPrefix }) {
this.userConfig.dir = dir;
this.userConfig.pathPrefix = pathPrefix;

if (this.logger) {
this.userConfig.logger = this.logger;
Expand Down Expand Up @@ -314,6 +314,12 @@ class TemplateConfig {

// Temporarily restore templateFormats
mergedConfig.templateFormats = templateFormats;

// Setup pathPrefix set via command line for plugin consumption
if (this.overrides.pathPrefix || this.overrides.pathPrefix === "") {
mergedConfig.pathPrefix = this.overrides.pathPrefix;
}

this.processPlugins(mergedConfig);
delete mergedConfig.templateFormats;

Expand Down
23 changes: 23 additions & 0 deletions test/TemplateConfigTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -545,3 +545,26 @@ test("Nested .addPlugin calls. More complex order", (t) => {

templateCfg.getConfig();
});

test(".addPlugin has access to pathPrefix", (t) => {
t.plan(1);
let templateCfg = new TemplateConfig();

templateCfg.userConfig.addPlugin(function (eleventyConfig) {
t.is(eleventyConfig.pathPrefix, "/");
});

templateCfg.getConfig();
});

test(".addPlugin has access to pathPrefix (override method)", (t) => {
t.plan(1);
let templateCfg = new TemplateConfig();
templateCfg.setPathPrefix("/test/");

templateCfg.userConfig.addPlugin(function (eleventyConfig) {
t.is(eleventyConfig.pathPrefix, "/test/");
});

templateCfg.getConfig();
});

0 comments on commit 0bbc16d

Please sign in to comment.