copy-webpack-plugin
Advanced tools
Comparing version 8.0.0 to 8.1.0
@@ -5,2 +5,9 @@ # Changelog | ||
## [8.1.0](https://github.com/webpack-contrib/copy-webpack-plugin/compare/v8.0.0...v8.1.0) (2021-03-22) | ||
### Features | ||
* added the `transformAll` option ([#596](https://github.com/webpack-contrib/copy-webpack-plugin/issues/596)) ([dde71f0](https://github.com/webpack-contrib/copy-webpack-plugin/commit/dde71f01417b9291c7029a3876e043d76beb9e8d)) | ||
## [8.0.0](https://github.com/webpack-contrib/copy-webpack-plugin/compare/v7.0.0...v8.0.0) (2021-03-04) | ||
@@ -7,0 +14,0 @@ |
@@ -76,2 +76,23 @@ "use strict"; | ||
static getContentHash(compiler, compilation, source) { | ||
const { | ||
outputOptions | ||
} = compilation; | ||
const { | ||
hashDigest, | ||
hashDigestLength, | ||
hashFunction, | ||
hashSalt | ||
} = outputOptions; | ||
const hash = compiler.webpack.util.createHash(hashFunction); | ||
if (hashSalt) { | ||
hash.update(hashSalt); | ||
} | ||
hash.update(source); | ||
const fullContentHash = hash.digest(hashDigest); | ||
return fullContentHash.slice(0, hashDigestLength); | ||
} | ||
static async runPattern(compiler, compilation, logger, cache, inputPattern, index) { | ||
@@ -375,3 +396,3 @@ const { | ||
logger.log(`transforming content for '${absoluteFilename}'...`); | ||
const buffer = result.source.source(); | ||
const buffer = result.source.buffer(); | ||
@@ -409,21 +430,4 @@ if (transform.cache) { | ||
logger.log(`interpolating template '${filename}' for '${sourceFilename}'...`); | ||
const { | ||
outputOptions | ||
} = compilation; | ||
const { | ||
hashDigest, | ||
hashDigestLength, | ||
hashFunction, | ||
hashSalt | ||
} = outputOptions; | ||
const hash = compiler.webpack.util.createHash(hashFunction); | ||
const contentHash = CopyPlugin.getContentHash(compiler, compilation, result.source.buffer()); | ||
if (hashSalt) { | ||
hash.update(hashSalt); | ||
} | ||
hash.update(result.source.source()); | ||
const fullContentHash = hash.digest(hashDigest); | ||
const contentHash = fullContentHash.slice(0, hashDigestLength); | ||
const ext = _path.default.extname(result.sourceFilename); | ||
@@ -494,2 +498,69 @@ | ||
if (assets && assets.length > 0) { | ||
if (item.transformAll) { | ||
if (typeof item.to === "undefined") { | ||
compilation.errors.push(new Error(`Invalid "pattern.to" for the "pattern.from": "${item.from}" and "pattern.transformAll" function. The "to" option must be specified.`)); | ||
return; | ||
} | ||
assets.sort((a, b) => a.absoluteFilename > b.absoluteFilename ? 1 : a.absoluteFilename < b.absoluteFilename ? -1 : 0); | ||
const mergedEtag = assets.length === 1 ? cache.getLazyHashedEtag(assets[0].source.buffer()) : assets.reduce((accumulator, asset, i) => { | ||
// eslint-disable-next-line no-param-reassign | ||
accumulator = cache.mergeEtags(i === 1 ? cache.getLazyHashedEtag(accumulator.source.buffer()) : accumulator, cache.getLazyHashedEtag(asset.source.buffer())); | ||
return accumulator; | ||
}); | ||
const cacheKeys = `transformAll|${(0, _serializeJavascript.default)({ | ||
version: _package.version, | ||
from: item.from, | ||
to: item.to, | ||
transformAll: item.transformAll | ||
})}`; | ||
const eTag = cache.getLazyHashedEtag(mergedEtag); | ||
const cacheItem = cache.getItemCache(cacheKeys, eTag); | ||
let transformedAsset = await cacheItem.getPromise(); | ||
if (!transformedAsset) { | ||
transformedAsset = { | ||
filename: item.to | ||
}; | ||
try { | ||
transformedAsset.data = await item.transformAll(assets.map(asset => { | ||
return { | ||
data: asset.source.buffer(), | ||
sourceFilename: asset.sourceFilename, | ||
absoluteFilename: asset.absoluteFilename | ||
}; | ||
})); | ||
} catch (error) { | ||
compilation.errors.push(error); | ||
return; | ||
} | ||
if (template.test(item.to)) { | ||
const contentHash = CopyPlugin.getContentHash(compiler, compilation, transformedAsset.data); | ||
const { | ||
path: interpolatedFilename, | ||
info: assetInfo | ||
} = compilation.getPathWithInfo((0, _normalizePath.default)(item.to), { | ||
contentHash, | ||
chunk: { | ||
hash: contentHash, | ||
contentHash | ||
} | ||
}); | ||
transformedAsset.filename = interpolatedFilename; | ||
transformedAsset.info = assetInfo; | ||
} | ||
const { | ||
RawSource | ||
} = compiler.webpack.sources; | ||
transformedAsset.source = new RawSource(transformedAsset.data); | ||
transformedAsset.force = item.force; | ||
await cacheItem.storePromise(transformedAsset); | ||
} | ||
assets = [transformedAsset]; | ||
} | ||
const priority = item.priority || 0; | ||
@@ -496,0 +567,0 @@ |
@@ -30,2 +30,5 @@ { | ||
}, | ||
"transformAll": { | ||
"instanceof": "Function" | ||
}, | ||
"toType": { | ||
@@ -32,0 +35,0 @@ "enum": ["dir", "file", "template"] |
{ | ||
"name": "copy-webpack-plugin", | ||
"version": "8.0.0", | ||
"version": "8.1.0", | ||
"description": "Copy files && directories with webpack", | ||
@@ -33,8 +33,5 @@ "license": "MIT", | ||
"test": "npm run test:coverage", | ||
"prepare": "npm run build", | ||
"prepare": "husky install && npm run build", | ||
"release": "standard-version", | ||
"defaults": "webpack-defaults", | ||
"_postinstall": "husky install", | ||
"prepublishOnly": "pinst --disable", | ||
"postpublish": "pinst --enable" | ||
"defaults": "webpack-defaults" | ||
}, | ||
@@ -50,3 +47,3 @@ "files": [ | ||
"glob-parent": "^5.1.1", | ||
"globby": "^11.0.2", | ||
"globby": "^11.0.3", | ||
"normalize-path": "^3.0.0", | ||
@@ -58,5 +55,5 @@ "p-limit": "^3.1.0", | ||
"devDependencies": { | ||
"@babel/cli": "^7.13.0", | ||
"@babel/core": "^7.13.8", | ||
"@babel/preset-env": "^7.13.8", | ||
"@babel/cli": "^7.13.10", | ||
"@babel/core": "^7.13.10", | ||
"@babel/preset-env": "^7.13.12", | ||
"@commitlint/cli": "^12.0.1", | ||
@@ -69,7 +66,7 @@ "@commitlint/config-conventional": "^12.0.1", | ||
"del-cli": "^3.0.1", | ||
"eslint": "^7.20.0", | ||
"eslint": "^7.22.0", | ||
"eslint-config-prettier": "^8.1.0", | ||
"eslint-plugin-import": "^2.22.1", | ||
"file-loader": "^6.1.1", | ||
"husky": "^5.1.1", | ||
"husky": "^5.2.0", | ||
"is-gzip": "^2.0.0", | ||
@@ -81,6 +78,5 @@ "jest": "^26.6.3", | ||
"npm-run-all": "^4.1.5", | ||
"pinst": "^2.1.6", | ||
"prettier": "^2.1.2", | ||
"standard-version": "^9.1.1", | ||
"webpack": "^5.24.3" | ||
"webpack": "^5.27.2" | ||
}, | ||
@@ -87,0 +83,0 @@ "keywords": [ |
@@ -47,3 +47,3 @@ <div align="center"> | ||
> ℹ️ `webpack-copy-plugin` is not designed to copy files generated from the build process; rather, it is to copy files that already exist in the source tree, as part of the build process. | ||
> ℹ️ `copy-webpack-plugin` is not designed to copy files generated from the build process; rather, it is to copy files that already exist in the source tree, as part of the build process. | ||
@@ -91,2 +91,3 @@ > ℹ️ If you want `webpack-dev-server` to write files to the output directory during development, you can force it with the [`writeToDisk`](https://github.com/webpack/webpack-dev-middleware#writetodisk) option or the [`write-file-webpack-plugin`](https://github.com/gajus/write-file-webpack-plugin). | ||
| [`transform`](#transform) | `{Object}` | `undefined` | Allows to modify the file contents. Enable `transform` caching. You can use `{ transform: {cache: { key: 'my-cache-key' }} }` to invalidate the cache. | | ||
| [`transformAll`](#transformAll) | `{Function}` | `undefined` | Allows you to modify the contents of multiple files and save the result to one file. | | ||
| [`noErrorOnMissing`](#noerroronmissing) | `{Boolean}` | `false` | Doesn't generate an error on missing file(s). | | ||
@@ -337,3 +338,3 @@ | [`info`](#info) | `{Object\|Function}` | `undefined` | Allows to add assets info. | | ||
> ℹ️ To ignore files by path please use the [`globOptions.ignore`]((#globoptions) option. | ||
> ℹ️ To ignore files by path please use the [`globOptions.ignore`](#globoptions) option. | ||
@@ -736,2 +737,41 @@ **webpack.config.js** | ||
#### `transformAll` | ||
Type: `Function` | ||
Default: `undefined` | ||
Allows you to modify the contents of multiple files and save the result to one file. | ||
> ℹ️ The `to` option must be specified and point to a file. It is allowed to use only `[contenthash]` and `[fullhash]` template strings. | ||
**webpack.config.js** | ||
```js | ||
module.exports = { | ||
plugins: [ | ||
new CopyPlugin({ | ||
patterns: [ | ||
{ | ||
from: "src/**/*.txt", | ||
to: "dest/file.txt", | ||
// The `assets` argument is an assets array for the pattern.from ("src/**/*.txt") | ||
transformAll(assets) { | ||
const result = assets.reduce((accumulator, asset) => { | ||
// The asset content can be obtained from `asset.source` using `source` method. | ||
// The asset content is a [`Buffer`](https://nodejs.org/api/buffer.html) object, it could be converted to a `String` to be processed using `content.toString()` | ||
const content = asset.data; | ||
accumulator = `${accumulator}${content}\n`; | ||
return accumulator; | ||
}, ""); | ||
return result; | ||
}, | ||
}, | ||
], | ||
}), | ||
], | ||
}; | ||
``` | ||
### `noErrorOnMissing` | ||
@@ -1109,2 +1149,26 @@ | ||
##### `yarn workspaces` and `monorepos` | ||
When using `yarn workspaces` or` monorepos`, relative copy paths from node_modules can be broken due to the way packages are hoisting. | ||
To avoid this, should explicitly specify where to copy the files from using `require.resolve`. | ||
**webpack.config.js** | ||
```js | ||
module.exports = { | ||
plugins: [ | ||
new CopyPlugin({ | ||
patterns: [ | ||
{ | ||
from: `${path.dirname( | ||
require.resolve(`${moduleName}/package.json`) | ||
)}/target`, | ||
to: "target", | ||
}, | ||
], | ||
}), | ||
], | ||
}; | ||
``` | ||
## Contributing | ||
@@ -1111,0 +1175,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
85617
24
679
1194
Updatedglobby@^11.0.3