Skip to content

Commit

Permalink
Updated hooks (#154)
Browse files Browse the repository at this point in the history
* Updated dependencies

* Updated hooks

- Added manifest parameter to afterOptions hook
- Use manifest parameter in internal transform and afterOptions taps
- Check options.enabled after afterOptions hook has been called
  • Loading branch information
webdeveric authored Apr 22, 2021
1 parent b5a53ae commit e4106d2
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 72 deletions.
83 changes: 34 additions & 49 deletions package-lock.json

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

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"devDependencies": {
"@types/lodash.get": "^4.4.6",
"@types/lodash.has": "^4.5.6",
"@types/node": "^14.14.27",
"@types/node": "^14.14.41",
"@types/tapable": "^2.2.2",
"@types/webpack-sources": "^2.1.0",
"@webdeveric/eslint-config": "^0.1.2",
Expand All @@ -74,21 +74,21 @@
"compression-webpack-plugin": "^7.1.2",
"copy-webpack-plugin": "^8.1.1",
"cspell": "^5.3.12",
"css-loader": "^5.2.1",
"css-loader": "^5.2.4",
"eslint": "^7.24.0",
"file-loader": "^6.2.0",
"fs-extra": "^9.1.0",
"husky": "^6.0.0",
"lint-staged": "^10.5.4",
"memory-fs": "^0.5.0",
"mini-css-extract-plugin": "^1.4.1",
"mini-css-extract-plugin": "^1.5.0",
"mkdirp": "^1.0.4",
"mocha": "^8.3.0",
"nyc": "^15.1.0",
"rimraf": "^3.0.2",
"superagent": "^6.1.0",
"typescript": "^4.2.4",
"webpack": "^5.32.0",
"webpack": "^5.35.0",
"webpack-dev-server": "^3.11.2",
"webpack-subresource-integrity": "^1.5.2"
}
Expand Down
34 changes: 17 additions & 17 deletions src/WebpackAssetsManifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,33 +53,33 @@ class WebpackAssetsManifest
transform: new SyncWaterfallHook([ 'assets', 'manifest' ]),
done: new AsyncSeriesHook([ 'manifest', 'stats' ]),
options: new SyncWaterfallHook([ 'options' ]),
afterOptions: new SyncHook([ 'options' ]),
afterOptions: new SyncHook([ 'options', 'manifest' ]),
});

this.hooks.transform.tap(PLUGIN_NAME, assets => {
const { sortManifest } = this.options;
this.hooks.transform.tap(PLUGIN_NAME, (assets, manifest) => {
const { sortManifest } = manifest.options;

return sortManifest ? getSortedObject(
assets,
typeof sortManifest === 'function' ? sortManifest.bind(this) : undefined,
typeof sortManifest === 'function' ? sortManifest.bind(manifest) : undefined,
) : assets;
});

this.hooks.afterOptions.tap(PLUGIN_NAME, options => {
this.options = Object.assign( this.defaultOptions, options );
this.options.integrityHashes = filterHashes( this.options.integrityHashes );
this.hooks.afterOptions.tap(PLUGIN_NAME, (options, manifest) => {
manifest.options = Object.assign( manifest.defaultOptions, options );
manifest.options.integrityHashes = filterHashes( manifest.options.integrityHashes );

validate(optionsSchema, this.options, { name: PLUGIN_NAME });
validate(optionsSchema, manifest.options, { name: PLUGIN_NAME });

this.options.output = path.normalize( this.options.output );
manifest.options.output = path.normalize( manifest.options.output );

// Copy over any entries that may have been added to the manifest before apply() was called.
// If the same key exists in assets and options.assets, options.assets should be used.
this.assets = Object.assign(this.options.assets, this.assets, this.options.assets);
manifest.assets = Object.assign(manifest.options.assets, manifest.assets, manifest.options.assets);

[ 'apply', 'customize', 'transform', 'done' ].forEach( hookName => {
if ( typeof this.options[ hookName ] === 'function' ) {
this.hooks[ hookName ].tap(`${PLUGIN_NAME}.option.${hookName}`, this.options[ hookName ] );
if ( typeof manifest.options[ hookName ] === 'function' ) {
manifest.hooks[ hookName ].tap(`${PLUGIN_NAME}.option.${hookName}`, manifest.options[ hookName ] );
}
});
});
Expand Down Expand Up @@ -109,17 +109,17 @@ class WebpackAssetsManifest
*/
apply(compiler)
{
if ( ! this.options.enabled ) {
return;
}

this.compiler = compiler;

// Allow hooks to modify options
this.options = this.hooks.options.call(this.options);

// Ensure options contain defaults and are valid
this.hooks.afterOptions.call(this.options);
this.hooks.afterOptions.call(this.options, this);

if ( ! this.options.enabled ) {
return;
}

compiler.hooks.watchRun.tap(PLUGIN_NAME, this.handleWatchRun.bind(this));

Expand Down
4 changes: 2 additions & 2 deletions test/WebpackAssetsManifest-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ describe('WebpackAssetsManifest', function() {
it('should return a JSON string', function() {
const manifest = new WebpackAssetsManifest();

manifest.hooks.afterOptions.call(manifest.options);
manifest.hooks.afterOptions.call(manifest.options, manifest);

assert.equal('{}', manifest.toString());
assert.equal('{}', manifest + '');
Expand All @@ -132,7 +132,7 @@ describe('WebpackAssetsManifest', function() {
space: '\t',
});

manifest.hooks.afterOptions.call(manifest.options);
manifest.hooks.afterOptions.call(manifest.options, manifest);

manifest.set('test', 'test');

Expand Down

0 comments on commit e4106d2

Please sign in to comment.