Skip to content

Commit

Permalink
add processResource option to control if resource is added as dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Jan 7, 2021
1 parent b8a0544 commit 4149dd9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
17 changes: 14 additions & 3 deletions README.md
Expand Up @@ -14,24 +14,35 @@ runLoaders({
context: { minimize: true },
// Additional loader context which is used as base context

processResource: (loaderContext, resourcePath, callback) => { ... },
// Optional: A function to process the resource
// Must have signature function(context, path, function(err, buffer))
// By default readResource is used and the resource is added a fileDependency

readResource: fs.readFile.bind(fs)
// A function to read the resource
// Optional: A function to read the resource
// Only used when 'processResource' is not provided
// Must have signature function(path, function(err, buffer))

// By default fs.readFile is used
}, function(err, result) {
// err: Error?

// result.result: Buffer | String
// The result
// only available when no error occured

// result.resourceBuffer: Buffer
// The raw resource as Buffer (useful for SourceMaps)
// only available when no error occured

// result.cacheable: Bool
// Is the result cacheable or do it require reexecution?

// result.fileDependencies: String[]
// An array of paths (files) on which the result depends on
// An array of paths (existing files) on which the result depends on

// result.missingDependencies: String[]
// An array of paths (not existing files) on which the result depends on

// result.contextDependencies: String[]
// An array of paths (directories) on which the result depends on
Expand Down
10 changes: 6 additions & 4 deletions lib/LoaderRunner.js
Expand Up @@ -217,8 +217,7 @@ function processResource(options, loaderContext, callback) {

var resourcePath = loaderContext.resourcePath;
if(resourcePath) {
loaderContext.addDependency(resourcePath);
options.readResource(resourcePath, function(err, buffer) {
options.processResource(loaderContext, resourcePath, function(err, buffer) {
if(err) return callback(err);
options.resourceBuffer = buffer;
iterateNormalLoaders(options, loaderContext, [buffer], callback);
Expand Down Expand Up @@ -266,7 +265,10 @@ exports.runLoaders = function runLoaders(options, callback) {
var resource = options.resource || "";
var loaders = options.loaders || [];
var loaderContext = options.context || {};
var readResource = options.readResource || readFile;
var processResource = options.processResource || ((readResource, context, resource, callback) => {
context.addDependency(resource);
readResource(resource, callback);
}).bind(null, options.readResource || readFile);

//
var splittedResource = resource && parsePathQueryFragment(resource);
Expand Down Expand Up @@ -390,7 +392,7 @@ exports.runLoaders = function runLoaders(options, callback) {

var processOptions = {
resourceBuffer: null,
readResource: readResource
processResource: processResource
};
iteratePitchingLoaders(processOptions, loaderContext, function(err, result) {
if(err) {
Expand Down

0 comments on commit 4149dd9

Please sign in to comment.