Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added ability to adjust plugin processAssets hook stage (#262) #263

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.DS_Store
.eslintcache
.nyc_output
.pnpm-debug.log
coverage
coverage.lcov
node_modules
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,15 @@ This plugin supports the following hooks via the `getCompilerHooks` export; `aft

Returns: `{ afterEmit: SyncWaterfallHook, beforeEmit: SyncWaterfallHook }`

### `assetHookStage`

Type: `Number`
Default: `Infinity`

If you need to consume the output of this plugin in another plugin, it can be useful to adjust the stage at which the manifest is generated. Pass a new stage to `assetHookStage` to change when the manifest is generated. See the [docs on `processAssets`](https://webpack.js.org/api/compilation-hooks/#list-of-asset-processing-stages) for more detail.

Note: any files added to the compilation after the stage specified will not be included in the manifest.

#### Usage

```js
Expand Down
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export type Manifest = Record<string, any>;

export interface InternalOptions {
[key: string]: any;
assetHookStage: number;
basePath: string;
fileName: string;
filter: (file: FileDescriptor) => Boolean;
Expand All @@ -37,6 +38,7 @@ export interface InternalOptions {
export type ManifestPluginOptions = Partial<InternalOptions>;

const defaults = {
assetHookStage: Infinity,
basePath: '',
fileName: 'manifest.json',
filter: null,
Expand Down Expand Up @@ -84,7 +86,7 @@ class WebpackManifestPlugin implements WebpackPluginInstance {
const normalModuleLoader = normalModuleLoaderHook.bind(this, { moduleAssets });
const hookOptions = {
name: 'WebpackManifestPlugin',
stage: Infinity
stage: this.options.assetHookStage
};

compiler.hooks.compilation.tap(hookOptions, (compilation) => {
Expand Down
54 changes: 54 additions & 0 deletions test/unit/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const CopyPlugin = require('copy-webpack-plugin');
const DependencyExtractionWebpackPlugin = require('@wordpress/dependency-extraction-webpack-plugin');
const del = require('del');

const webpack = require('webpack');

const { compile } = require('../helpers/unit');

const outputPath = join(__dirname, '../output/options');
Expand Down Expand Up @@ -141,3 +143,55 @@ test('useLegacyEmit', async (t) => {

t.snapshot(manifest);
});

test('assetHookStage', async (t) => {
const FIRST_PROCESS_ASSETS_STAGE = 0;
const SECOND_PROCESS_ASSETS_STAGE = 1;
let assets;

class LastStagePlugin {
/* eslint-disable class-methods-use-this */
shellscape marked this conversation as resolved.
Show resolved Hide resolved
apply(compiler) {
const isWebpack4 = webpack.version.startsWith('4');

const callback = (compilation) => {
// We'll check for our manifest being included in the assets of this invocation
assets = Object.keys(isWebpack4 ? compilation.assets : compilation);
};

const hookOptions = {
name: 'LastStagePlugin',
// Make sure our plugin is scheduled to run after the manifest plugin
stage: SECOND_PROCESS_ASSETS_STAGE
};

if (isWebpack4) {
compiler.hooks.emit.tap(hookOptions, callback);
} else {
compiler.hooks.thisCompilation.tap(hookOptions, (compilation) => {
compilation.hooks.processAssets.tap(hookOptions, callback);
});
}
}
/* eslint-enable class-methods-use-this */
}

const config = {
context: __dirname,
entry: {
main: '../fixtures/file.js'
},
output: {
filename: '[name].js',
path: join(outputPath, 'assetHookStage')
},
plugins: [new LastStagePlugin()]
};

// Ensure we register the manifest plugin to run first.
const { manifest } = await compile(config, t, { assetHookStage: FIRST_PROCESS_ASSETS_STAGE });

t.snapshot(manifest);
const laterPluginHasManifest = assets.includes('manifest.json');
t.is(laterPluginHasManifest, true);
});
16 changes: 16 additions & 0 deletions test/unit/snapshots/options.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,19 @@ Generated by [AVA](https://avajs.dev).
'main.js': 'main.js',
'main.json': 'main.asset.json',
}

## assetHookStage

> Snapshot 1

{
'main.js': 'main.js',
}

## assetHookStage

> Snapshot 1

{
'main.js': 'main.js',
}
Binary file modified test/unit/snapshots/options.js.snap
Binary file not shown.