Skip to content

Commit db53937

Browse files
feat: added the info option
1 parent 9bc5416 commit db53937

File tree

6 files changed

+245
-21
lines changed

6 files changed

+245
-21
lines changed

README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ module.exports = {
9090
| [`cacheTransform`](#cacheTransform) | `{Boolean\|String\|Object}` | `false` | Enable `transform` caching. You can use `{ cache: { key: 'my-cache-key' } }` to invalidate the cache. |
9191
| [`transformPath`](#transformpath) | `{Function}` | `undefined` | Allows to modify the writing path. |
9292
| [`noErrorOnMissing`](#noerroronmissing) | `{Boolean}` | `false` | Doesn't generate an error on missing file(s). |
93+
| [`info`](#info) | `{Object\|Function}` | `undefined` | Allows to add assets info. |
9394

9495
#### `from`
9596

@@ -773,6 +774,51 @@ module.exports = {
773774
};
774775
```
775776

777+
#### `info`
778+
779+
Type: `Object|Function<Object>`
780+
Default: `undefined`
781+
782+
Allows to add assets info.
783+
784+
**webpack.config.js**
785+
786+
```js
787+
module.exports = {
788+
plugins: [
789+
new CopyPlugin({
790+
patterns: [
791+
"relative/path/to/file.ext",
792+
{
793+
from: "**/*",
794+
// Terser skip this file for minimization
795+
info: { minimized: true },
796+
},
797+
],
798+
}),
799+
],
800+
};
801+
```
802+
803+
**webpack.config.js**
804+
805+
```js
806+
module.exports = {
807+
plugins: [
808+
new CopyPlugin({
809+
patterns: [
810+
"relative/path/to/file.ext",
811+
{
812+
from: "**/*",
813+
// Terser skip this file for minimization
814+
info: (file) => ({ minimized: true }),
815+
},
816+
],
817+
}),
818+
],
819+
};
820+
```
821+
776822
### Options
777823

778824
| Name | Type | Default | Description |

src/index.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,10 @@ class CopyPlugin {
360360
sourceFilename,
361361
filename,
362362
force: pattern.force,
363+
info:
364+
typeof pattern.info === "function"
365+
? pattern.info(file) || {}
366+
: pattern.info || {},
363367
};
364368

365369
// If this came from a glob or dir, add it to the file dependencies
@@ -731,7 +735,10 @@ class CopyPlugin {
731735
`force updating '${filename}' from '${absoluteFilename}' to compilation assets, because it already exists...`
732736
);
733737

734-
compilation.updateAsset(filename, source, info);
738+
compilation.updateAsset(filename, source, {
739+
...info,
740+
...asset.info,
741+
});
735742

736743
logger.log(
737744
`force updated '${filename}' from '${absoluteFilename}' to compilation assets, because it already exists`
@@ -747,17 +754,20 @@ class CopyPlugin {
747754
return;
748755
}
749756

750-
logger.log(
751-
`writing '${filename}' from '${absoluteFilename}' to compilation assets...`
752-
);
753-
754757
const info = { copied: true, sourceFilename };
755758

756759
if (asset.immutable) {
757760
info.immutable = true;
758761
}
759762

760-
compilation.emitAsset(filename, source, info);
763+
logger.log(
764+
`writing '${filename}' from '${absoluteFilename}' to compilation assets...`
765+
);
766+
767+
compilation.emitAsset(filename, source, {
768+
...info,
769+
...asset.info,
770+
});
761771

762772
logger.log(
763773
`written '${filename}' from '${absoluteFilename}' to compilation assets`

src/options.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@
3333
"force": {
3434
"type": "boolean"
3535
},
36+
"info": {
37+
"anyOf": [
38+
{
39+
"type": "object"
40+
},
41+
{
42+
"instanceof": "Function"
43+
}
44+
]
45+
},
3646
"flatten": {
3747
"type": "boolean"
3848
},

test/__snapshots__/validate-options.test.js.snap

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ exports[`validate options should throw an error on the "options" option with "{"
1414
exports[`validate options should throw an error on the "patterns" option with "" value 1`] = `
1515
"Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
1616
- options.patterns should be an array:
17-
[non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }, ...] (should not have fewer than 1 item)"
17+
[non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, info?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }, ...] (should not have fewer than 1 item)"
1818
`;
1919
2020
exports[`validate options should throw an error on the "patterns" option with "[""]" value 1`] = `
@@ -37,6 +37,32 @@ exports[`validate options should throw an error on the "patterns" option with "[
3737
- options.patterns[0].from should be an non-empty string."
3838
`;
3939
40+
exports[`validate options should throw an error on the "patterns" option with "[{"from":"dir","info":"string"}]" value 1`] = `
41+
"Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
42+
- options.patterns[0] should be one of these:
43+
non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, info?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }
44+
Details:
45+
* options.patterns[0].info should be one of these:
46+
object { … } | function
47+
Details:
48+
* options.patterns[0].info should be an object:
49+
object { … }
50+
* options.patterns[0].info should be an instance of function."
51+
`;
52+
53+
exports[`validate options should throw an error on the "patterns" option with "[{"from":"dir","info":true}]" value 1`] = `
54+
"Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
55+
- options.patterns[0] should be one of these:
56+
non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, info?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }
57+
Details:
58+
* options.patterns[0].info should be one of these:
59+
object { … } | function
60+
Details:
61+
* options.patterns[0].info should be an object:
62+
object { … }
63+
* options.patterns[0].info should be an instance of function."
64+
`;
65+
4066
exports[`validate options should throw an error on the "patterns" option with "[{"from":"test.txt","filter":"test"}]" value 1`] = `
4167
"Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
4268
- options.patterns[0].filter should be an instance of function."
@@ -77,7 +103,7 @@ exports[`validate options should throw an error on the "patterns" option with "[
77103
exports[`validate options should throw an error on the "patterns" option with "[{"from":"test.txt","to":"dir","context":"context"}]" value 1`] = `
78104
"Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
79105
- options.patterns[0] should be one of these:
80-
non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }
106+
non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, info?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }
81107
Details:
82108
* options.patterns[0].cacheTransform should be one of these:
83109
boolean | string | object { directory?, keys? }
@@ -96,7 +122,7 @@ exports[`validate options should throw an error on the "patterns" option with "[
96122
exports[`validate options should throw an error on the "patterns" option with "[{"from":"test.txt","to":true,"context":"context"}]" value 1`] = `
97123
"Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
98124
- options.patterns[0] should be one of these:
99-
non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }
125+
non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, info?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }
100126
Details:
101127
* options.patterns[0].to should be one of these:
102128
string | function
@@ -124,71 +150,71 @@ exports[`validate options should throw an error on the "patterns" option with "[
124150
exports[`validate options should throw an error on the "patterns" option with "{}" value 1`] = `
125151
"Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
126152
- options.patterns should be an array:
127-
[non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }, ...] (should not have fewer than 1 item)"
153+
[non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, info?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }, ...] (should not have fewer than 1 item)"
128154
`;
129155
130156
exports[`validate options should throw an error on the "patterns" option with "true" value 1`] = `
131157
"Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
132158
- options.patterns should be an array:
133-
[non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }, ...] (should not have fewer than 1 item)"
159+
[non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, info?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }, ...] (should not have fewer than 1 item)"
134160
`;
135161
136162
exports[`validate options should throw an error on the "patterns" option with "true" value 2`] = `
137163
"Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
138164
- options.patterns should be an array:
139-
[non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }, ...] (should not have fewer than 1 item)"
165+
[non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, info?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }, ...] (should not have fewer than 1 item)"
140166
`;
141167
142168
exports[`validate options should throw an error on the "patterns" option with "undefined" value 1`] = `
143169
"Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
144170
- options misses the property 'patterns'. Should be:
145-
[non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }, ...] (should not have fewer than 1 item)"
171+
[non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, info?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }, ...] (should not have fewer than 1 item)"
146172
`;
147173
148174
exports[`validate options should throw an error on the "unknown" option with "/test/" value 1`] = `
149175
"Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
150176
- options misses the property 'patterns'. Should be:
151-
[non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }, ...] (should not have fewer than 1 item)"
177+
[non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, info?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }, ...] (should not have fewer than 1 item)"
152178
`;
153179
154180
exports[`validate options should throw an error on the "unknown" option with "[]" value 1`] = `
155181
"Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
156182
- options misses the property 'patterns'. Should be:
157-
[non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }, ...] (should not have fewer than 1 item)"
183+
[non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, info?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }, ...] (should not have fewer than 1 item)"
158184
`;
159185
160186
exports[`validate options should throw an error on the "unknown" option with "{"foo":"bar"}" value 1`] = `
161187
"Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
162188
- options misses the property 'patterns'. Should be:
163-
[non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }, ...] (should not have fewer than 1 item)"
189+
[non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, info?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }, ...] (should not have fewer than 1 item)"
164190
`;
165191
166192
exports[`validate options should throw an error on the "unknown" option with "{}" value 1`] = `
167193
"Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
168194
- options misses the property 'patterns'. Should be:
169-
[non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }, ...] (should not have fewer than 1 item)"
195+
[non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, info?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }, ...] (should not have fewer than 1 item)"
170196
`;
171197
172198
exports[`validate options should throw an error on the "unknown" option with "1" value 1`] = `
173199
"Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
174200
- options misses the property 'patterns'. Should be:
175-
[non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }, ...] (should not have fewer than 1 item)"
201+
[non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, info?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }, ...] (should not have fewer than 1 item)"
176202
`;
177203
178204
exports[`validate options should throw an error on the "unknown" option with "false" value 1`] = `
179205
"Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
180206
- options misses the property 'patterns'. Should be:
181-
[non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }, ...] (should not have fewer than 1 item)"
207+
[non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, info?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }, ...] (should not have fewer than 1 item)"
182208
`;
183209
184210
exports[`validate options should throw an error on the "unknown" option with "test" value 1`] = `
185211
"Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
186212
- options misses the property 'patterns'. Should be:
187-
[non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }, ...] (should not have fewer than 1 item)"
213+
[non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, info?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }, ...] (should not have fewer than 1 item)"
188214
`;
189215
190216
exports[`validate options should throw an error on the "unknown" option with "true" value 1`] = `
191217
"Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
192218
- options misses the property 'patterns'. Should be:
193-
[non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }, ...] (should not have fewer than 1 item)"
219+
[non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, info?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }, ...] (should not have fewer than 1 item)"
194220
`;

test/info-option.test.js

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import { run, runEmit } from "./helpers/run";
2+
3+
describe("info option", () => {
4+
it('should work without "info" option', (done) => {
5+
runEmit({
6+
expectedAssetKeys: ["file.txt"],
7+
patterns: [
8+
{
9+
from: "file.txt",
10+
},
11+
],
12+
})
13+
.then(done)
14+
.catch(done);
15+
});
16+
17+
it('should work when "info" option is a object', (done) => {
18+
run({
19+
expectedAssetKeys: ["file.txt"],
20+
patterns: [
21+
{
22+
from: "file.txt",
23+
info: { test: true },
24+
},
25+
],
26+
})
27+
.then(({ compilation }) => {
28+
expect(compilation.assetsInfo.get("file.txt").test).toBe(true);
29+
})
30+
.then(done)
31+
.catch(done);
32+
});
33+
34+
it('should work when "info" option is a object and "force" option is true', (done) => {
35+
const expectedAssetKeys = ["file.txt"];
36+
37+
run({
38+
preCopy: {
39+
additionalAssets: [
40+
{ name: "file.txt", data: "Content", info: { custom: true } },
41+
],
42+
},
43+
expectedAssetKeys,
44+
patterns: [
45+
{
46+
from: "file.txt",
47+
force: true,
48+
info: { test: true },
49+
},
50+
],
51+
})
52+
.then(({ compilation }) => {
53+
expect(compilation.assetsInfo.get("file.txt").test).toBe(true);
54+
})
55+
.then(done)
56+
.catch(done);
57+
});
58+
59+
it('should work when "info" option is a function', (done) => {
60+
run({
61+
expectedAssetKeys: ["file.txt"],
62+
patterns: [
63+
{
64+
from: "file.txt",
65+
info: (file) => {
66+
expect.assertions(4);
67+
68+
const fileKeys = ["absoluteFilename", "sourceFilename", "filename"];
69+
70+
for (const key of fileKeys) {
71+
expect(key in file).toBe(true);
72+
}
73+
74+
return { test: true };
75+
},
76+
},
77+
],
78+
})
79+
.then(({ compilation }) => {
80+
expect(compilation.assetsInfo.get("file.txt").test).toBe(true);
81+
})
82+
.then(done)
83+
.catch(done);
84+
});
85+
86+
it('should work when "info" option is a function and "force" option is true', (done) => {
87+
const expectedAssetKeys = ["file.txt"];
88+
89+
run({
90+
preCopy: {
91+
additionalAssets: [
92+
{ name: "file.txt", data: "Content", info: { custom: true } },
93+
],
94+
},
95+
expectedAssetKeys,
96+
patterns: [
97+
{
98+
from: "file.txt",
99+
force: true,
100+
info: () => ({ test: true }),
101+
},
102+
],
103+
})
104+
.then(({ compilation }) => {
105+
expect(compilation.assetsInfo.get("file.txt").test).toBe(true);
106+
})
107+
.then(done)
108+
.catch(done);
109+
});
110+
});

0 commit comments

Comments
 (0)