copy-webpack-plugin
Advanced tools
Comparing version 6.3.2 to 6.4.0
@@ -5,2 +5,10 @@ # Changelog | ||
## [6.4.0](https://github.com/webpack-contrib/copy-webpack-plugin/compare/v6.3.2...v6.4.0) (2020-12-07) | ||
### Features | ||
* added the `info` option ([db53937](https://github.com/webpack-contrib/copy-webpack-plugin/commit/db53937016b7dbf494bc728f00242cd26541f6a3)) | ||
* added type `Function` for the `to` option ([#563](https://github.com/webpack-contrib/copy-webpack-plugin/issues/563)) ([9bc5416](https://github.com/webpack-contrib/copy-webpack-plugin/commit/9bc541694c0d0975c59586cedfea4a51d11f5278)) | ||
### [6.3.2](https://github.com/webpack-contrib/copy-webpack-plugin/compare/v6.3.1...v6.3.2) (2020-11-19) | ||
@@ -7,0 +15,0 @@ |
@@ -107,26 +107,6 @@ "use strict"; | ||
pattern.from = _path.default.normalize(pattern.from); | ||
pattern.to = _path.default.normalize(typeof pattern.to !== "undefined" ? pattern.to : ""); | ||
pattern.compilerContext = compiler.context; | ||
pattern.context = _path.default.normalize(typeof pattern.context !== "undefined" ? !_path.default.isAbsolute(pattern.context) ? _path.default.join(pattern.compilerContext, pattern.context) : pattern.context : pattern.compilerContext); | ||
logger.log(`starting to process a pattern from '${pattern.from}' using '${pattern.context}' context to '${pattern.to}'...`); | ||
logger.log(`starting to process a pattern from '${pattern.from}' using '${pattern.context}' context`); | ||
const isToDirectory = _path.default.extname(pattern.to) === "" || pattern.to.slice(-1) === _path.default.sep; | ||
switch (true) { | ||
// if toType already exists | ||
case !!pattern.toType: | ||
break; | ||
case template.test(pattern.to): | ||
pattern.toType = "template"; | ||
break; | ||
case isToDirectory: | ||
pattern.toType = "dir"; | ||
break; | ||
default: | ||
pattern.toType = "file"; | ||
} | ||
if (_path.default.isAbsolute(pattern.from)) { | ||
@@ -277,3 +257,3 @@ pattern.absoluteFrom = pattern.from; | ||
const files = filteredPaths.map(item => { | ||
const files = await Promise.all(filteredPaths.map(async item => { | ||
const from = item.path; | ||
@@ -284,2 +264,27 @@ logger.debug(`found '${from}'`); // `globby`/`fast-glob` return the relative path when the path contains special characters on windows | ||
pattern.to = typeof pattern.to !== "function" ? _path.default.normalize(typeof pattern.to !== "undefined" ? pattern.to : "") : await pattern.to({ | ||
context: pattern.context, | ||
absoluteFilename | ||
}); | ||
const isToDirectory = _path.default.extname(pattern.to) === "" || pattern.to.slice(-1) === _path.default.sep; | ||
switch (true) { | ||
// if toType already exists | ||
case !!pattern.toType: | ||
break; | ||
case template.test(pattern.to): | ||
pattern.toType = "template"; | ||
break; | ||
case isToDirectory: | ||
pattern.toType = "dir"; | ||
break; | ||
default: | ||
pattern.toType = "file"; | ||
} | ||
logger.log(`'to' option '${pattern.to}' determinated as '${pattern.toType}'`); | ||
const relativeFrom = pattern.flatten ? _path.default.basename(absoluteFilename) : _path.default.relative(pattern.context, absoluteFilename); | ||
@@ -299,3 +304,3 @@ let filename = pattern.toType === "dir" ? _path.default.join(pattern.to, relativeFrom) : pattern.to; | ||
}; | ||
}); | ||
})); | ||
let assets; | ||
@@ -314,3 +319,4 @@ | ||
filename, | ||
force: pattern.force | ||
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 | ||
@@ -570,3 +576,5 @@ | ||
logger.log(`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`); | ||
@@ -580,3 +588,2 @@ return; | ||
logger.log(`writing '${filename}' from '${absoluteFilename}' to compilation assets...`); | ||
const info = { | ||
@@ -591,3 +598,6 @@ copied: 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`); | ||
@@ -594,0 +604,0 @@ }); |
@@ -12,3 +12,10 @@ { | ||
"to": { | ||
"type": "string" | ||
"anyOf": [ | ||
{ | ||
"type": "string" | ||
}, | ||
{ | ||
"instanceof": "Function" | ||
} | ||
] | ||
}, | ||
@@ -30,2 +37,12 @@ "context": { | ||
}, | ||
"info": { | ||
"anyOf": [ | ||
{ | ||
"type": "object" | ||
}, | ||
{ | ||
"instanceof": "Function" | ||
} | ||
] | ||
}, | ||
"flatten": { | ||
@@ -32,0 +49,0 @@ "type": "boolean" |
{ | ||
"name": "copy-webpack-plugin", | ||
"version": "6.3.2", | ||
"version": "6.4.0", | ||
"description": "Copy files && directories with webpack", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -82,3 +82,3 @@ <div align="center"> | ||
| [`from`](#from) | `{String}` | `undefined` | Glob or path from where we сopy files. | | ||
| [`to`](#to) | `{String}` | `compiler.options.output` | Output path. | | ||
| [`to`](#to) | `{String\|Function}` | `compiler.options.output` | Output path. | | ||
| [`context`](#context) | `{String}` | `options.context \|\| compiler.options.context` | A path that determines how to interpret the `from` path. | | ||
@@ -94,2 +94,3 @@ | [`globOptions`](#globoptions) | `{Object}` | `undefined` | [Options][glob-options] passed to the glob pattern matching library including `ignore` option. | | ||
| [`noErrorOnMissing`](#noerroronmissing) | `{Boolean}` | `false` | Doesn't generate an error on missing file(s). | | ||
| [`info`](#info) | `{Object\|Function}` | `undefined` | Allows to add assets info. | | ||
@@ -179,5 +180,7 @@ #### `from` | ||
Type: `String` | ||
Type: `String|Function` | ||
Default: `compiler.options.output` | ||
##### String | ||
Output path. | ||
@@ -214,2 +217,49 @@ | ||
##### Function | ||
Allows to modify the writing path. | ||
> ⚠️ Don't return directly `\\` in `to` (i.e `path\to\newFile`) option because on UNIX the backslash is a valid character inside a path component, i.e., it's not a separator. | ||
> On Windows, the forward slash and the backward slash are both separators. | ||
> Instead please use `/` or `path` methods. | ||
**webpack.config.js** | ||
```js | ||
module.exports = { | ||
plugins: [ | ||
new CopyPlugin({ | ||
patterns: [ | ||
{ | ||
from: "src/*.png", | ||
to({ context, absoluteFilename }) { | ||
return "dest/newPath"; | ||
}, | ||
}, | ||
], | ||
}), | ||
], | ||
}; | ||
``` | ||
**webpack.config.js** | ||
```js | ||
module.exports = { | ||
plugins: [ | ||
new CopyPlugin({ | ||
patterns: [ | ||
{ | ||
from: "src/*.png", | ||
to: "dest/", | ||
to({ context, absoluteFilename }) { | ||
return Promise.resolve("dest/newPath"); | ||
}, | ||
}, | ||
], | ||
}), | ||
], | ||
}; | ||
``` | ||
#### `context` | ||
@@ -731,2 +781,47 @@ | ||
#### `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 | ||
@@ -733,0 +828,0 @@ |
78288
633
1066