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

Webpack Helpers v1 Beta: Webpack 5, addFilter() #205

Open
wants to merge 83 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
83 commits
Select commit Hold shift + click to select a range
dcf2eb6
Upgrade dependencies for Webpack 5 / Node 14-16
kadamwhite Apr 28, 2022
4cb27d0
Pin Chalk to v4 for node compatibility (see note)
kadamwhite Apr 28, 2022
f6cc586
Update choose-port to latest react-dev-utils version
kadamwhite Apr 28, 2022
c697a40
Use Asset Modules over url-loader and file-loader
kadamwhite Apr 28, 2022
b3c8d52
Switch to Webpack 5-compatible CSS optimizer plugin
kadamwhite Apr 28, 2022
a7fbc9b
Add source-map-loader, borrowed from latest CRA
kadamwhite Apr 28, 2022
287723c
JSX pragma is now baked into WordPress babel preset
kadamwhite Apr 28, 2022
069375f
Update to latest TerserPlugin config from CRA
kadamwhite Apr 28, 2022
d866674
Update webpack devserver config for devServer v4
kadamwhite Apr 28, 2022
6898a3b
Update unit tests for asset module changes
kadamwhite Apr 28, 2022
880aaef
Remove duplicative dependencies from package.json
kadamwhite Apr 28, 2022
d75ca23
Bump version to 1.0.0-beta.0
kadamwhite Apr 28, 2022
d3256e6
Replace filterLoaders system with a more generic addFilter hook system
kadamwhite Apr 28, 2022
4a7a5a2
Remove support for Node 10
kadamwhite Apr 29, 2022
f1a4646
Set publicPath to "" to avoid issues with "auto"
kadamwhite Apr 29, 2022
7755c07
Tag v1.0.0-beta.1
kadamwhite Apr 29, 2022
e702595
Update src/loaders.js
kadamwhite Apr 29, 2022
5e4d73b
Hash filenames by default
kadamwhite Apr 29, 2022
a08e8a0
Limit CSS filename hash to 8 characters
kadamwhite Apr 29, 2022
f70f124
Fix test to expect more flexible html regex
kadamwhite Apr 29, 2022
b876ecb
Consolidate filters used for presets, props @tfrommen
kadamwhite Apr 29, 2022
1bd56aa
Tag v1.0.0-beta.2
kadamwhite Apr 29, 2022
6432ae9
Use "development-asset-manifest.json" in dev mode for clarity
kadamwhite Apr 29, 2022
96cbc41
Tag v1.0.0-beta.3
kadamwhite Apr 29, 2022
cc71e96
Document manifest name change
kadamwhite Apr 29, 2022
02d7b13
Fix tests after beta.3 change and improve manifest documentation
kadamwhite Apr 29, 2022
1173405
Ignore DS_Store files
kadamwhite May 17, 2022
c30ef09
Allow loader to be skipped when rendering preset by filtering it to null
kadamwhite May 17, 2022
f5d7c10
Tag v1.0.0-beta.4
kadamwhite May 17, 2022
1645bbc
Detect HTTPS when using the devServer.server flag (devServer.https is…
kadamwhite May 17, 2022
9dbc218
Simplify function body of removeNullLoaders helper
kadamwhite May 17, 2022
0e48396
Update tests to use devServer.server:"https" instead of devServer.htt…
kadamwhite May 17, 2022
1d59d74
Tag v1.0.0-beta.5
kadamwhite May 17, 2022
ef6449f
Expose removeFilter in addition to addFilter
kadamwhite May 18, 2022
df1eef8
Tag v1.0.0-beta.6
kadamwhite May 18, 2022
05a8fc8
Remove eslint-loader and add eslint-webpack-plugin
kadamwhite May 21, 2022
a5d0051
Add test to test-build to validate linting behavior
kadamwhite May 21, 2022
e8e3054
Swap out fix-style-only-entries plugin for "webpack-remove-empty-scri…
kadamwhite May 21, 2022
bf9e4a4
Do not include lint validation in prod build test because it causes C…
kadamwhite May 21, 2022
8410f9c
Remove leftover loaders.eslint() reference
kadamwhite May 22, 2022
62f38f8
Preserve spacing in JSON
kadamwhite May 22, 2022
1420643
Switch back to 16-character hashes (Asset_Loader expects this)
kadamwhite May 22, 2022
7d10a73
Adjust build stats defaults and apply to dev builds as well
kadamwhite May 22, 2022
ad40f3a
Filter out null plugin instances
kadamwhite May 22, 2022
a88e280
Add SimpleBuildReportPlugin and expose via plugins.formatConsoleOutput
kadamwhite May 22, 2022
ca2005e
Include a bundleAnalyzer plugin by default when --analyze is passed t…
kadamwhite May 23, 2022
04f2615
Include simpleBuildReport plugin in production presets
kadamwhite May 23, 2022
82fb64c
Tag v1.0.0-beta.7
kadamwhite May 23, 2022
8eaa3c9
Correct language in changelog around loader filtering
kadamwhite May 23, 2022
cdd57b2
Do not deep-merge terserOptions, so that terser can be predictably co…
kadamwhite May 23, 2022
bcd10ad
Permit filtering Terser defaults
kadamwhite May 23, 2022
1055650
Change filter strings from "loader/...", "plugin/..." etc to "loaders…
kadamwhite May 23, 2022
bc37992
Document the addFilter and removeFilter modules
kadamwhite May 23, 2022
0743793
Finish converting loader names from "loader/" to "loaders/"
kadamwhite May 23, 2022
cde3aee
Tag v1.0.0-beta.8
kadamwhite May 23, 2022
1591e84
Fix issue where simple build report plugin was listed as dev dependency
kadamwhite May 23, 2022
ee6d541
Tag v1.0.0-beta.9
kadamwhite May 23, 2022
e632c89
Add note about returning null from loader
kadamwhite May 24, 2022
bfbf059
Fix bug where isObj in deep-merge would return true for "null"
kadamwhite May 24, 2022
015ad69
Include modern image formats in image asset test
kadamwhite May 24, 2022
4ad3c3e
Upgrade bundled SimpleBuildReportPlugin
kadamwhite May 24, 2022
c319d17
Tag v1.0.0-beta.10
kadamwhite May 24, 2022
e95a890
Provide preset config as filter argument when appropriate (#207)
kadamwhite Jun 15, 2022
dc67783
Author initial upgrade guide
kadamwhite Jun 15, 2022
0a892f5
Tag v1.0.0-beta.11
kadamwhite Jun 15, 2022
b12898a
Do not pass loader defaults to filters by reference to prevent mutation
kadamwhite Jun 16, 2022
fa464e3
Tag v1.0.0-beta.12
kadamwhite Jun 16, 2022
a20b4d8
Expand multi-config DevServer documentation
kadamwhite Jun 17, 2022
046b247
Standardize JSDoc on @returns instead of @return
kadamwhite Jun 22, 2022
b42bf86
Inject development-mode asset manifest even without publicPath
kadamwhite Jul 1, 2022
3201d61
Switch from [contenthash] back to [fullhash] (prev [hash]) in Dev config
kadamwhite Jul 1, 2022
870f4f0
Set runtimeChunk: "single" to work around HMR issue with multiple ent…
kadamwhite Jul 1, 2022
0e69dcd
Tag v1.0.0-beta.13
kadamwhite Jul 1, 2022
a2637c5
Automatically set devServer "host" property when we can
kadamwhite Jul 1, 2022
583b5ab
Tag v1.0.0-beta.14
kadamwhite Jul 1, 2022
20fff25
Remove check for whether devServer is running, devServer is not relev…
kadamwhite Jul 1, 2022
bfa8c6a
Revert switch to runtimeChunk: single, which requires unintuitive oth…
kadamwhite Jul 1, 2022
122790e
Tag v1.0.0-beta.15
kadamwhite Jul 1, 2022
c7e9e6a
Only show DevServer overlay for errors, not warnings
kadamwhite Jul 1, 2022
301a0f1
Store and re-use inferred publicPaths in subsequent builds to the sam…
kadamwhite Jul 1, 2022
77d09e9
Tag v1.0.0-beta.16
kadamwhite Jul 1, 2022
637e695
Pin block-editor-hmr@0.7.0
kadamwhite May 17, 2023
4195d37
Tag v1.0.0-beta.17
kadamwhite May 17, 2023
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
Prev Previous commit
Next Next commit
Document the addFilter and removeFilter modules
  • Loading branch information
kadamwhite committed May 23, 2022
commit bc37992668fb9779eb3ed2852080680340e23d22
64 changes: 64 additions & 0 deletions docs/modules/helpers.md
Original file line number Diff line number Diff line change
@@ -98,3 +98,67 @@ module.exports = helpers.choosePort( 9090 ).then( port => [
} ),
] );
```

## Filters

Webpack Helpers v1.0 introduced a new system for altering the behavior of bundled loaders, plugins, and presets, also covered under [Customizing Presets](./presets.html#customizing-presets). This system uses a filtering approach similar to [the way PHP filters work within WordPress](https://developer.wordpress.org/reference/functions/add_filter/), with `addFilter` and `removeFilter` helpers you can use to modify various types of internal data at runtime.

```js
// webpack.config.js
const { helpers } = require( '@humanmade/webpack-helpers' );

const { addFilter, removeFilter } = helpers;

// Hook onto a specific filter by name, and provide a callback function to
// alter the value returned from that filter.
addFilter( 'filter/name', function( value ) {
return 'modified value';
} );

// To filter only some values, you can remove a filter by passing the same
// function reference to removeFilter.
const filterFunction = ( value ) => 'modified value';

addFilter( 'filter/name', filterFunction );

// Do something where the filter gets applied.

removeFilter( 'filter/name', filterFunction );

// Now if you do that same thing, the filter will no longer apply.
```

### Filter List

Each [loader](./loaders.html) exposes at minimum two filters, `loaders/{name}` and `loaders/{name}/defaults`. For example, the defaults for `loaders.ts()` can be filtered using

```js
addFilter( 'loaders/ts/defaults', ( loaderDefaultsObject ) => {
// return a filtered value
} );
```
or the computed final loader object can be modified after the fact with

```js
addFilter( 'loaders/ts', ( loaderObject ) => {
// return a filtered value
} );
```

Additional filters provided by Webpack Helpers:

**`presets/stylesheet-loaders`**

Filter the [stylesheet loader chain](https://webpack.js.org/concepts/loaders/#configuration) used in the `presets.production()` and `presets.development()` helpers. Functions added to this filter receive the stylesheet chain as their first argument, and the name of the compilation mode (`production` or `development`) as the second argument.

**`plugins/terser/defaults`**

Filter the [TerserPlugin default options object](https://webpack.js.org/plugins/terser-webpack-plugin/#options) if you wish to alter the minification settings used by the production preset.

**`loaders/postcss/plugins`**

Filter the list of [PostCSS plugins](https://github.com/postcss/postcss/blob/main/docs/plugins.md) used by the `loaders.postcss()` loader.

**`loaders/postcss/preset-env`**

Filter the [`postcss-preset-env` options](https://github.com/csstools/postcss-preset-env#options) used by the `loaders.postcss()` loader.