Skip to content

Commit

Permalink
feat: added the info option
Browse files Browse the repository at this point in the history
  • Loading branch information
cap-Bernardito committed Dec 7, 2020
1 parent 9bc5416 commit db53937
Show file tree
Hide file tree
Showing 6 changed files with 245 additions and 21 deletions.
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ module.exports = {
| [`cacheTransform`](#cacheTransform) | `{Boolean\|String\|Object}` | `false` | Enable `transform` caching. You can use `{ cache: { key: 'my-cache-key' } }` to invalidate the cache. |
| [`transformPath`](#transformpath) | `{Function}` | `undefined` | Allows to modify the writing path. |
| [`noErrorOnMissing`](#noerroronmissing) | `{Boolean}` | `false` | Doesn't generate an error on missing file(s). |
| [`info`](#info) | `{Object\|Function}` | `undefined` | Allows to add assets info. |

#### `from`

Expand Down Expand Up @@ -773,6 +774,51 @@ module.exports = {
};
```

#### `info`

Type: `Object|Function<Object>`
Default: `undefined`

Allows to add assets info.

**webpack.config.js**

```js
module.exports = {
plugins: [
new CopyPlugin({
patterns: [
"relative/path/to/file.ext",
{
from: "**/*",
// Terser skip this file for minimization
info: { minimized: true },
},
],
}),
],
};
```

**webpack.config.js**

```js
module.exports = {
plugins: [
new CopyPlugin({
patterns: [
"relative/path/to/file.ext",
{
from: "**/*",
// Terser skip this file for minimization
info: (file) => ({ minimized: true }),
},
],
}),
],
};
```

### Options

| Name | Type | Default | Description |
Expand Down
22 changes: 16 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,10 @@ class CopyPlugin {
sourceFilename,
filename,
force: pattern.force,
info:
typeof pattern.info === "function"
? pattern.info(file) || {}
: pattern.info || {},
};

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

compilation.updateAsset(filename, source, info);
compilation.updateAsset(filename, source, {
...info,
...asset.info,
});

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

logger.log(
`writing '${filename}' from '${absoluteFilename}' to compilation assets...`
);

const info = { copied: true, sourceFilename };

if (asset.immutable) {
info.immutable = true;
}

compilation.emitAsset(filename, source, info);
logger.log(
`writing '${filename}' from '${absoluteFilename}' to compilation assets...`
);

compilation.emitAsset(filename, source, {
...info,
...asset.info,
});

logger.log(
`written '${filename}' from '${absoluteFilename}' to compilation assets`
Expand Down
10 changes: 10 additions & 0 deletions src/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@
"force": {
"type": "boolean"
},
"info": {
"anyOf": [
{
"type": "object"
},
{
"instanceof": "Function"
}
]
},
"flatten": {
"type": "boolean"
},
Expand Down
56 changes: 41 additions & 15 deletions test/__snapshots__/validate-options.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ exports[`validate options should throw an error on the "options" option with "{"
exports[`validate options should throw an error on the "patterns" option with "" value 1`] = `
"Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
- options.patterns should be an array:
[non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }, ...] (should not have fewer than 1 item)"
[non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, info?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }, ...] (should not have fewer than 1 item)"
`;
exports[`validate options should throw an error on the "patterns" option with "[""]" value 1`] = `
Expand All @@ -37,6 +37,32 @@ exports[`validate options should throw an error on the "patterns" option with "[
- options.patterns[0].from should be an non-empty string."
`;
exports[`validate options should throw an error on the "patterns" option with "[{"from":"dir","info":"string"}]" value 1`] = `
"Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
- options.patterns[0] should be one of these:
non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, info?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }
Details:
* options.patterns[0].info should be one of these:
object { … } | function
Details:
* options.patterns[0].info should be an object:
object { … }
* options.patterns[0].info should be an instance of function."
`;
exports[`validate options should throw an error on the "patterns" option with "[{"from":"dir","info":true}]" value 1`] = `
"Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
- options.patterns[0] should be one of these:
non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, info?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }
Details:
* options.patterns[0].info should be one of these:
object { … } | function
Details:
* options.patterns[0].info should be an object:
object { … }
* options.patterns[0].info should be an instance of function."
`;
exports[`validate options should throw an error on the "patterns" option with "[{"from":"test.txt","filter":"test"}]" value 1`] = `
"Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
- options.patterns[0].filter should be an instance of function."
Expand Down Expand Up @@ -77,7 +103,7 @@ exports[`validate options should throw an error on the "patterns" option with "[
exports[`validate options should throw an error on the "patterns" option with "[{"from":"test.txt","to":"dir","context":"context"}]" value 1`] = `
"Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
- options.patterns[0] should be one of these:
non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }
non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, info?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }
Details:
* options.patterns[0].cacheTransform should be one of these:
boolean | string | object { directory?, keys? }
Expand All @@ -96,7 +122,7 @@ exports[`validate options should throw an error on the "patterns" option with "[
exports[`validate options should throw an error on the "patterns" option with "[{"from":"test.txt","to":true,"context":"context"}]" value 1`] = `
"Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
- options.patterns[0] should be one of these:
non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }
non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, info?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }
Details:
* options.patterns[0].to should be one of these:
string | function
Expand Down Expand Up @@ -124,71 +150,71 @@ exports[`validate options should throw an error on the "patterns" option with "[
exports[`validate options should throw an error on the "patterns" option with "{}" value 1`] = `
"Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
- options.patterns should be an array:
[non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }, ...] (should not have fewer than 1 item)"
[non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, info?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }, ...] (should not have fewer than 1 item)"
`;
exports[`validate options should throw an error on the "patterns" option with "true" value 1`] = `
"Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
- options.patterns should be an array:
[non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }, ...] (should not have fewer than 1 item)"
[non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, info?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }, ...] (should not have fewer than 1 item)"
`;
exports[`validate options should throw an error on the "patterns" option with "true" value 2`] = `
"Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
- options.patterns should be an array:
[non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }, ...] (should not have fewer than 1 item)"
[non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, info?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }, ...] (should not have fewer than 1 item)"
`;
exports[`validate options should throw an error on the "patterns" option with "undefined" value 1`] = `
"Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
- options misses the property 'patterns'. Should be:
[non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }, ...] (should not have fewer than 1 item)"
[non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, info?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }, ...] (should not have fewer than 1 item)"
`;
exports[`validate options should throw an error on the "unknown" option with "/test/" value 1`] = `
"Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
- options misses the property 'patterns'. Should be:
[non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }, ...] (should not have fewer than 1 item)"
[non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, info?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }, ...] (should not have fewer than 1 item)"
`;
exports[`validate options should throw an error on the "unknown" option with "[]" value 1`] = `
"Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
- options misses the property 'patterns'. Should be:
[non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }, ...] (should not have fewer than 1 item)"
[non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, info?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }, ...] (should not have fewer than 1 item)"
`;
exports[`validate options should throw an error on the "unknown" option with "{"foo":"bar"}" value 1`] = `
"Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
- options misses the property 'patterns'. Should be:
[non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }, ...] (should not have fewer than 1 item)"
[non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, info?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }, ...] (should not have fewer than 1 item)"
`;
exports[`validate options should throw an error on the "unknown" option with "{}" value 1`] = `
"Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
- options misses the property 'patterns'. Should be:
[non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }, ...] (should not have fewer than 1 item)"
[non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, info?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }, ...] (should not have fewer than 1 item)"
`;
exports[`validate options should throw an error on the "unknown" option with "1" value 1`] = `
"Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
- options misses the property 'patterns'. Should be:
[non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }, ...] (should not have fewer than 1 item)"
[non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, info?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }, ...] (should not have fewer than 1 item)"
`;
exports[`validate options should throw an error on the "unknown" option with "false" value 1`] = `
"Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
- options misses the property 'patterns'. Should be:
[non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }, ...] (should not have fewer than 1 item)"
[non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, info?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }, ...] (should not have fewer than 1 item)"
`;
exports[`validate options should throw an error on the "unknown" option with "test" value 1`] = `
"Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
- options misses the property 'patterns'. Should be:
[non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }, ...] (should not have fewer than 1 item)"
[non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, info?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }, ...] (should not have fewer than 1 item)"
`;
exports[`validate options should throw an error on the "unknown" option with "true" value 1`] = `
"Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
- options misses the property 'patterns'. Should be:
[non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }, ...] (should not have fewer than 1 item)"
[non-empty string | object { from, to?, context?, globOptions?, filter?, toType?, force?, info?, flatten?, transform?, cacheTransform?, transformPath?, noErrorOnMissing? }, ...] (should not have fewer than 1 item)"
`;
110 changes: 110 additions & 0 deletions test/info-option.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import { run, runEmit } from "./helpers/run";

describe("info option", () => {
it('should work without "info" option', (done) => {
runEmit({
expectedAssetKeys: ["file.txt"],
patterns: [
{
from: "file.txt",
},
],
})
.then(done)
.catch(done);
});

it('should work when "info" option is a object', (done) => {
run({
expectedAssetKeys: ["file.txt"],
patterns: [
{
from: "file.txt",
info: { test: true },
},
],
})
.then(({ compilation }) => {
expect(compilation.assetsInfo.get("file.txt").test).toBe(true);
})
.then(done)
.catch(done);
});

it('should work when "info" option is a object and "force" option is true', (done) => {
const expectedAssetKeys = ["file.txt"];

run({
preCopy: {
additionalAssets: [
{ name: "file.txt", data: "Content", info: { custom: true } },
],
},
expectedAssetKeys,
patterns: [
{
from: "file.txt",
force: true,
info: { test: true },
},
],
})
.then(({ compilation }) => {
expect(compilation.assetsInfo.get("file.txt").test).toBe(true);
})
.then(done)
.catch(done);
});

it('should work when "info" option is a function', (done) => {
run({
expectedAssetKeys: ["file.txt"],
patterns: [
{
from: "file.txt",
info: (file) => {
expect.assertions(4);

const fileKeys = ["absoluteFilename", "sourceFilename", "filename"];

for (const key of fileKeys) {
expect(key in file).toBe(true);
}

return { test: true };
},
},
],
})
.then(({ compilation }) => {
expect(compilation.assetsInfo.get("file.txt").test).toBe(true);
})
.then(done)
.catch(done);
});

it('should work when "info" option is a function and "force" option is true', (done) => {
const expectedAssetKeys = ["file.txt"];

run({
preCopy: {
additionalAssets: [
{ name: "file.txt", data: "Content", info: { custom: true } },
],
},
expectedAssetKeys,
patterns: [
{
from: "file.txt",
force: true,
info: () => ({ test: true }),
},
],
})
.then(({ compilation }) => {
expect(compilation.assetsInfo.get("file.txt").test).toBe(true);
})
.then(done)
.catch(done);
});
});

0 comments on commit db53937

Please sign in to comment.