Skip to content
This repository has been archived by the owner on Jul 24, 2023. It is now read-only.

Commit

Permalink
Create new instance of EleventyLoad each transform
Browse files Browse the repository at this point in the history
  • Loading branch information
gregives committed Dec 21, 2020
1 parent 96b6514 commit 734805a
Showing 1 changed file with 22 additions and 24 deletions.
46 changes: 22 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ const path = require("path");
const utils = require("./utils");

class EleventyLoad {
constructor(config, options) {
constructor(options, cache, content, context) {
this.options = options;
this.cache = cache;

// Create context for loaders
this.context = {
Expand All @@ -14,27 +15,12 @@ class EleventyLoad {
acc[util] = utils[util].bind(this);
return acc;
}, {}),
config: context._config
};

// Create cache
this.cache = {};

// Keep reference to this
const self = this;

// Transform is our entry point
config.addTransform("eleventy-load", function (content) {
self.context.config = this._config;

// Use input path as dependency
const resource = path.relative(this._config.inputDir, this.inputPath);
return self.addDependency(resource, content);
});

// Clear cache on re-runs
config.on('beforeWatch', () => {
this.cache = {}
});
// Use input path as dependency
const resource = path.relative(context._config.inputDir, context.inputPath);
return this.addDependency(resource, content);
}

// Process additional dependencies straight away
Expand All @@ -50,9 +36,9 @@ class EleventyLoad {

const resolvedDirectory = dependentResource.resource
? path.resolve(
this.context.config.inputDir,
path.parse(dependentResource.resourcePath).dir
)
this.context.config.inputDir,
path.parse(dependentResource.resourcePath).dir
)
: this.context.config.inputDir;

// Resolve resource for consistency
Expand Down Expand Up @@ -134,5 +120,17 @@ class EleventyLoad {
}

module.exports = function (config, options) {
return new EleventyLoad(config, options);
const cache = {}

// Transform is our entry point
config.addTransform("eleventy-load", function (content) {
return new EleventyLoad(options, cache, content, this)
});

// Clear cache on re-runs
config.on('beforeWatch', () => {
for (const resource in cache) {
delete cache[resource];
}
});
};

0 comments on commit 734805a

Please sign in to comment.