Skip to content

Commit af550c1

Browse files
docs(examples): show 'output.copy' used as a plugin
Copies a prebuilt vendor bundle through 'new CopyPlugin({...})', which is where 'concurrency' and 'stage' live. Says what 'stage' does not do: the minimizer taps with 'additionalAssets', so it re-runs for an asset added at any later stage and only 'info.minimized' leaves a built file alone.
1 parent c23849f commit af550c1

4 files changed

Lines changed: 49 additions & 6 deletions

File tree

‎examples/output-copy/README.md‎

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@ A pattern is a `from` plus optional `to`, `filename`, `context`, `globOptions`,
1616
filename template, so one pattern can rename, flatten and hash.
1717

1818
`output.copy` takes a string or a list of patterns. The plugin behind it takes
19-
`concurrency` and the `processAssets` `stage` as well — reach for
20-
`new CopyPlugin({ patterns, stage })` when you need those.
19+
`concurrency` and the `processAssets` `stage` as well, so reach for
20+
`new webpack.CopyPlugin({ patterns, concurrency, stage })` when you need those —
21+
the config below copies a prebuilt vendor bundle that way. `stage` decides which
22+
asset-processing taps see what was copied; it is not how a file is kept out of
23+
the minimizer, which re-runs for assets added at any later stage. `info:
24+
{ minimized: true }` is what leaves an already-built file alone.
2125

2226
The config also registers a small plugin that merges several copied assets into
2327
one — `copy-webpack-plugin`'s `transformAll`. `output.copy` has no equivalent
@@ -29,7 +33,7 @@ key a cache item on their contents, emit the merged asset and delete the parts.
2933
```javascript
3034
"use strict";
3135

32-
const { Compilation, sources } = require("webpack");
36+
const { Compilation, CopyPlugin, sources } = require("webpack");
3337

3438
/** @import { Compiler } from "webpack" */
3539

@@ -143,6 +147,22 @@ const config = {
143147
]
144148
},
145149
plugins: [
150+
// the plugin behind `output.copy` is where `concurrency` and `stage` live;
151+
// `stage` decides which asset-processing taps see what it copied
152+
new CopyPlugin({
153+
patterns: [
154+
{
155+
from: "*.min.js",
156+
context: "vendor",
157+
to: "vendor",
158+
// the minimizer re-runs for assets added at any later stage, so
159+
// this, not a late `stage`, is what leaves a built file alone
160+
info: { minimized: true }
161+
}
162+
],
163+
concurrency: 50,
164+
stage: Compilation.PROCESS_ASSETS_STAGE_ADDITIONS
165+
}),
146166
new MergeCopiedAssetsPlugin({
147167
include: /^licenses\//,
148168
filename: "THIRD_PARTY_LICENSES.txt",
@@ -193,6 +213,7 @@ assets by path *.txt 144 bytes
193213
asset THIRD_PARTY_LICENSES.txt 121 bytes [emitted] [copied]
194214
asset robots.txt 23 bytes [emitted] [from: static/robots.txt] [copied]
195215
asset output.js 222 bytes [emitted] (name: main)
216+
asset vendor/analytics.min.js 72 bytes [emitted] [from: vendor/analytics.min.js] [copied]
196217
asset index.html 69 bytes [emitted] [from: static/index.html] [copied]
197218
asset pages/index.html 69 bytes [emitted] [from: static/index.html] [copied]
198219
asset runtime/config.json 33 bytes [emitted] [from: config.json] [copied]
@@ -216,6 +237,7 @@ assets by info 44 bytes [immutable]
216237
assets by path *.txt 144 bytes
217238
asset THIRD_PARTY_LICENSES.txt 121 bytes [emitted] [copied]
218239
asset robots.txt 23 bytes [emitted] [from: static/robots.txt] [copied]
240+
asset vendor/analytics.min.js 72 bytes [emitted] [from: vendor/analytics.min.js] [copied] [minimized]
219241
asset index.html 64 bytes [emitted] [from: static/index.html] [copied] [minimized]
220242
asset pages/index.html 64 bytes [emitted] [from: static/index.html] [copied] [minimized]
221243
asset runtime/config.json 33 bytes [emitted] [from: config.json] [copied]

‎examples/output-copy/template.md‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@ A pattern is a `from` plus optional `to`, `filename`, `context`, `globOptions`,
1616
filename template, so one pattern can rename, flatten and hash.
1717

1818
`output.copy` takes a string or a list of patterns. The plugin behind it takes
19-
`concurrency` and the `processAssets` `stage` as well — reach for
20-
`new CopyPlugin({ patterns, stage })` when you need those.
19+
`concurrency` and the `processAssets` `stage` as well, so reach for
20+
`new webpack.CopyPlugin({ patterns, concurrency, stage })` when you need those —
21+
the config below copies a prebuilt vendor bundle that way. `stage` decides which
22+
asset-processing taps see what was copied; it is not how a file is kept out of
23+
the minimizer, which re-runs for assets added at any later stage. `info:
24+
{ minimized: true }` is what leaves an already-built file alone.
2125

2226
The config also registers a small plugin that merges several copied assets into
2327
one — `copy-webpack-plugin`'s `transformAll`. `output.copy` has no equivalent
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
!function(e){e.track=function(n){e.__q=e.__q||[],e.__q.push(n)}}(this);

‎examples/output-copy/webpack.config.js‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use strict";
22

3-
const { Compilation, sources } = require("webpack");
3+
const { Compilation, CopyPlugin, sources } = require("webpack");
44

55
/** @import { Compiler } from "webpack" */
66

@@ -114,6 +114,22 @@ const config = {
114114
]
115115
},
116116
plugins: [
117+
// the plugin behind `output.copy` is where `concurrency` and `stage` live;
118+
// `stage` decides which asset-processing taps see what it copied
119+
new CopyPlugin({
120+
patterns: [
121+
{
122+
from: "*.min.js",
123+
context: "vendor",
124+
to: "vendor",
125+
// the minimizer re-runs for assets added at any later stage, so
126+
// this, not a late `stage`, is what leaves a built file alone
127+
info: { minimized: true }
128+
}
129+
],
130+
concurrency: 50,
131+
stage: Compilation.PROCESS_ASSETS_STAGE_ADDITIONS
132+
}),
117133
new MergeCopiedAssetsPlugin({
118134
include: /^licenses\//,
119135
filename: "THIRD_PARTY_LICENSES.txt",

0 commit comments

Comments
 (0)