ploadin v1.1.4
ploadin
Ploadin - Webpack PLugIN and LOADer in one. Use data from loaders in plugins and vice-versa.
๐ Homepage | ๐ Repository | ๐ฆ NPM | ๐ Documentation | ๐ Issue Tracker
๐ช Table of Content
- ๐งฐ Features
- ๐ถ Install
- ๐ Usage
- ๐ค API
- ๐ฎ Background
- โณ Changelog
- ๐ Developing
- ๐ Roadmap
- ๐ค Contributing
- ๐ง Contributors
- โญ Show your support
- ๐ Community
- ๐ Related Projects
- ๐จโ๐ง Maintainers
- ๐ License
๐งฐ Features
Are you developing a complex plugin that needs to be used as both a plugin and a loader? ploadin got you covered:
Easily develop Webpack plugins that can access files passed through loaders.
Use instances of
Ploadinsubclasses both as plugins and loaders.Plugin and loader methods share instance's state, so plugin behaviour can be modified based on data passed to loaders and vice versa.
๐ถ Install
npm install ploadin
# or
yarn add ploadin๐ Usage
Subclassing
Subclass Ploadin to create a class that can access both plugin and loader contexts.
Following example shows how to communicate between loader and plugin. Following happens in the example:
Ploadinis subclassed toPloadinSubclassso the loader and plugin methods can communicate.Before compilation starts (and so before loaders are run),pitch data and loader behaviour are decided based on some conditions in
apply.When
pitchis called, the method passes data from plugin down to otherpitchmethods (Webpack docs on pitch).When
loaderis called, the loader will skip its job ifignoreLoaderwas set to truthy.After all is done, the state is reset in
applyon compiler'safterCompilehook.
Notice that in
loaderandpitchmethods,thisrefers to thePloadininstance and notloaderContextas it is in Webpack loaders. TheloaderContextis, instead, passed as the first argument, so all other arguments are shifted by one.
// subclass.js
const { Ploadin, registerSubclass } = require('ploadin');
export class PloadinSubclass extends Ploadin {
constructor() {
this.pitchData = null;
this.ignoreLoader = false;
super();
}
// `apply` is plugin methods
apply(compiler) {
// Set data that pitch should pass on
compiler.hooks.beforeCompile.tapAsync(
'PloadinSubclass',
(stats, callback) => {
this.pitchData = { someData: compiler.xxx };
this.ignoreLoader = compiler.yyy;
callback();
},
);
// Clean up
compiler.hooks.afterCompile.tapAsync(
'SubclassPloadin',
(compilation, callback) => {
this.pitchData = null
this.ignoreLoader = false;
callback();
},
);
}
// `loader` and `pitch` are loader methods
loader(loaderContext, source, ...args) {
// Skip loader action based on some conditions
if (this.ignoreLoader) {
return source
}
// Process source here otherwise...
...
}
pitch(
loaderContext,
remainingRequest,
precedingRequest,
data
) {
// Pass data from plugin to pitch
Object.assign(data, this.pitchData);
}
}// subclass.ts
import { Ploadin, registerSubclass } from 'ploadin';
export class PloadinSubclass extends Ploadin {
constructor() {
this.pitchData = null;
this.ignoreLoader = false;
super();
}
// `apply` is plugin methods
apply(compiler: Compiler) {
// Set data that pitch should pass on
compiler.hooks.beforeCompile.tapAsync(
'PloadinSubclass',
(stats, callback) => {
this.pitchData = { someData: compiler.xxx };
this.ignoreLoader = compiler.yyy;
callback();
},
);
// Clean up
compiler.hooks.afterCompile.tapAsync(
'SubclassPloadin',
(compilation, callback) => {
this.pitchData = null
this.ignoreLoader = false;
callback();
},
);
}
// `loader` and `pitch` are loader methods
loader(loaderContext: any, source?: string, ...args: any[]) {
// Skip loader action based on some conditions
if (this.ignoreLoader) {
return source
}
// Process source here otherwise...
...
}
pitch(
loaderContext,
remainingRequest: string,
precedingRequest: string,
data: any,
) {
// Pass data from plugin to pitch
Object.assign(data, this.pitchData);
}
}Using in Webpack
To use as plugin, pass the instance itself as a plugin.
To use as loader, pass the asLoader property.
// webpack.config.js
const { PloadinSubclass } = require('./subclass');
const myPloadinSubclass = new PloadinSubclass();
module.exports = {
plugins: [
myPloadinSubclass,
],
module: {
rules: [
{
test: /\.js$/i, // some test
use: [
myPloadinSubclass.asLoader,
...
],
},
],
},
};Using multiple subclasses and instances
You can use multiple Ploadin subclasses, and even multiple instances of the same class, within the same config, they will not interfere.
// webpack.config.js
const { PloadinSubclass } = require('./subclass1');
const { AnotherPloadinSubclass } = require('./subclass2');
const myPloadinSubclass1 = new PloadinSubclass();
const myPloadinSubclass2 = new PloadinSubclass();
const anotherPloadin1 = new AnotherPloadinSubclass();
const anotherPloadin2 = new AnotherPloadinSubclass();
module.exports = {
plugins: [
myPloadinSubclass1,
myPloadinSubclass2,
anotherPloadin1,
anotherPloadin2
],
module: {
rules: [
{
test: /\.js$/i, // some test
use: [
myPloadinSubclass1.asLoader,
myPloadinSubclass2.asLoader,
anotherPloadin1.asLoader,
anotherPloadin2.asLoader,
...
],
},
],
},
};๐ค API
TypeDoc documentation can be found here.
Ploadin
Ploadin class has following properties:
Ploadin.asLoader: object
Loader object to be used in webpack config.
Following methods will be called if defined:
Ploadin.classOptions: any
Data associated with the Ploadin class. The data returned by classOptions is
the same data (copy actually) of what is passed to registerSubclass.
Ploadin.apply(compiler: Compiler): void
Webpack plugin's apply method. See
Writing a Webpack plugin
for details.
Ploadin.loader(loaderContext: LoaderContext, content?: string, map?: string, data: any): void
Webpack loader's loader method. See Webpack loaders
for details.
Note that argument signature is shifted as
loaderContextis passed as first argument.this, instead, refers toPloadininstance.
Ploadin.pitch(loaderContext: LoaderContext, remainingRequest: string, precedingRequest: string, data: any): void
Webpack loader's pitch method. See Webpack loader's pitch
for details.
Note that argument signature is shifted as
loaderContextis passed as first argument.this, instead, refers toPloadininstance.
Helpers
registerSubclass(subclass: Subclass, options: any): boolean
- subclass - class extending
Ploadin - options - any data associated with this subclass
Returns true if successfully registered, false if the class has been
registered before.
Normally, any class subclassing Ploadin is automatically registered with
instance-manager when a first instance is created. This is necessary so the
class and its instances can be looked up by indices.
You can register the class yourself. This enables you to optionally pass along options associated with the given class.
One use of this is to store options passed to class factory so we can associate the options with the dynamically-created class.
๐ฎ Background
This package was prompted by the challenge of how to use and manage dynamically created Webpack plugins that need to access both loader and plugin contexts (similarly to how mini-css-extract-plugin needs access to both).
Webpack passes only JSON-serializable data to loaders, so loaders don't have direct access to plugins. And if you're dealing with dynamically-created classes, correctly matching loaders with their respective plugins gets more complicated.
โณ Changelog
This projects follows semantic versioning. The changelog can be found here.
๐ Developing
If you want to contribute to the project or forked it, this guide will get you up and going.
๐ Roadmap
This package is considered feature-complete. However, if you have ideas how it could be improved, please be sure to share it with us by opening an issue.
๐ค Contributing
Contributions, issues and feature requests are welcome! Thank you โค๏ธ
Feel free to dive in! See current issues, open an issue, or submit PRs.
How to report bugs, feature requests, and how to contribute and what conventions we use is all described in the contributing guide.
When contributing we follow the Contributor Covenant. See our Code of Conduct.
๐ง Contributors
Contributions of any kind welcome. Thanks goes to these wonderful people โค๏ธ
Recent and Top Contributors
Generated using Hall of Fame.
All Contributors
Contribution type emoji legend
No additional contributors. Be the first one!
This project follows the all-contributors specification.
โญ Show your support
Give a โญ๏ธif this project helped you!
๐ Community
๐ Related Projects
- mini-extract-plugin - Generalized extensible variation of mini-css-extract-plugin that can be used to create plugins to extract text from custom file types
๐จโ๐ง Maintainers
๐ค Juro Oravec
- Twitter: @JuroOravec
- GitHub: @JuroOravec
- LinkedIn: @jurooravec
- Sourcerer: @JuroOravec
๐ License
Copyright ยฉ 2020 Juro Oravec.
This project is MIT licensed.