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

Commit

Permalink
Process additional dependencies immediately
Browse files Browse the repository at this point in the history
  • Loading branch information
gregives committed Aug 29, 2020
1 parent edf77cb commit 49d98d1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 57 deletions.
82 changes: 25 additions & 57 deletions .eleventy.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,83 +4,51 @@ class EleventyLoad {
constructor(config, options) {
this.config = config
this.options = options
this.files = []

// Transform is our entry point
this.config.addTransform('eleventy-load', this.start.bind(this))
this.config.addTransform('eleventy-load', this.processFile.bind(this))
}

// Context of loaders
get loaderContext() {
return {
files: this.files,
addDependency: this.addDependency
}
}

// Add dependency to file queue
addDependency(path) {
this.files.push(path)
this.config.addWatchTarget(path)
}

// Start processing files from entry point
async start(content, entry) {
const loaders = this.getLoaders(entry)

// Return content if no loaders match
if (loaders === null)
return content

// Process entry file with loaders
content = await this.processFile(content, loaders)

// Process files added to queue
await this.processFileQueue()

return content
// Process additional dependencies straight away
async addDependency(path) {
return await this.processFile(null, path)
}

// Get loaders for path
getLoaders(path) {
// Find which rule matches the given path
const applicableRule = this.options.rules.find((rule) => rule.test.test(path))
const rule = this.options.rules.find((rule) => rule.test.test(path))

// Return loaders if they exist or null
return applicableRule === undefined ? null : applicableRule.loaders
// Return loaders if they exist, else null
return rule && rule.loaders ? rule.loaders : null
}

// Load content of file
async getContent(path, loaders) {
// If loader has raw property, load content as buffer instead of string
const encoding = loaders[0].loader.raw ? null : 'utf8'
return await fs.readFile(path, { encoding })
}

// Process file with the given loaders
async processFile(content, loaders) {
async processFile(content, path, loaders = this.getLoaders(path)) {
// Return content or path if no loaders match
if (loaders === null)
return content || path

// If content isn't passed in, load from path
if (content === null) {
content = await this.getContent(path, loaders)
}

// Apply loaders to content in order
for (const loader of loaders) {
const loaderFunction = loader.loader.bind(this.loaderContext)
const loaderFunction = loader.loader.bind(this)
content = await loaderFunction(content, loader.options)
}

return content
}

// Process all fies in queue
async processFileQueue() {
// Process remaining files in queue
while (this.files.length > 0) {
// Get loaders for file given path
const path = this.files.shift()
const loaders = this.getLoaders(path)

// Continue if no loaders match
if (loaders === null)
continue

// If loader has raw property, load content as buffer instead of string
const encoding = loaders[0].loader.raw ? null : 'utf8'
const content = await fs.readFile(path, { encoding })

// Process file with loaders
await this.processFile(content, loaders)
}
}
}

module.exports = function(config, options) {
Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 49d98d1

Please sign in to comment.