Security News
Highlights from the 2024 Rails Community Survey
A record 2,709 developers participated in the 2024 Ruby on Rails Community Survey, revealing key tools, practices, and trends shaping the Rails ecosystem.
loader-runner
Advanced tools
The loader-runner npm package is designed to run webpack loaders in a node.js environment without requiring the webpack itself. It allows developers to test and run individual loaders, simulating the webpack compilation process. This can be particularly useful for loader development, testing, and debugging.
Running a single loader
This feature allows running a single loader on a specified file. In the code sample, `runLoaders` is used to apply a CSS loader to a CSS file, demonstrating how to process a resource with specific loader configurations.
const runLoaders = require('loader-runner').runLoaders;
runLoaders({
resource: 'path/to/file.css',
loaders: [{ loader: 'path/to/css-loader' }],
context: { minimize: true },
readResource: fs.readFile.bind(fs)
}, (err, result) => {
if(err) {
return console.error(err);
}
console.log(result);
});
Running multiple loaders
This feature demonstrates how to chain multiple loaders, in this case, an ESLint loader followed by a Babel loader, to process a JavaScript file. It showcases the ability to use loader-runner for complex processing involving multiple steps.
const runLoaders = require('loader-runner').runLoaders;
runLoaders({
resource: 'path/to/file.js',
loaders: [
{ loader: 'path/to/babel-loader', options: { presets: ['@babel/preset-env'] } },
{ loader: 'path/to/eslint-loader' }
],
context: {},
readResource: fs.readFile.bind(fs)
}, (err, result) => {
if(err) {
return console.error(err);
}
console.log(result);
});
While webpack is a comprehensive module bundler, it internally uses a mechanism similar to loader-runner to process files with loaders. Compared to loader-runner, webpack offers a broader set of features for bundling, optimization, and asset management but is more complex and requires more configuration.
Cosmiconfig is designed for loading and parsing configuration files from various formats. It's similar to loader-runner in the sense that both deal with processing files, but cosmiconfig focuses on configuration files and does not directly relate to webpack loaders or the concept of transforming file content.
import { runLoaders } from "loader-runner";
runLoaders({
resource: "/abs/path/to/file.txt?query",
// String: Absolute path to the resource (optionally including query string)
loaders: ["/abs/path/to/loader.js?query"],
// String[]: Absolute paths to the loaders (optionally including query string)
context: { minimize: true },
// Additional loader context which is used as base context
readResource: fs.readFile.bind(fs)
// A function to read the resource
// Must have signature function(path, function(err, buffer))
}, function(err, result) {
// err: Error?
// result.result: Buffer | String
// The result
// result.resourceBuffer: Buffer
// The raw resource as Buffer (useful for SourceMaps)
// 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
// result.contextDependencies: String[]
// An array of paths (directories) on which the result depends on
})
More documentation following...
FAQs
Runs (webpack) loaders
The npm package loader-runner receives a total of 20,906,401 weekly downloads. As such, loader-runner popularity was classified as popular.
We found that loader-runner demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A record 2,709 developers participated in the 2024 Ruby on Rails Community Survey, revealing key tools, practices, and trends shaping the Rails ecosystem.
Security News
In 2023, data breaches surged 78% from zero-day and supply chain attacks, but developers are still buried under alerts that are unable to prevent these threats.
Security News
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.