diff --git a/README.md b/README.md index 0a5b6fff..2cf7afda 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,7 @@ Or, in case of just a `from` with the default destination, you can also use a `{ |[`transformPath`](#transformPath)|`{Function\|Promise}`|`(targetPath, sourcePath) => path`|Function or Promise that modifies file writing path| |[`cache`](#cache)|`{Boolean\|Object}`|`false`|Enable `transform` caching. You can use `{ cache: { key: 'my-cache-key' } }` to invalidate the cache| |[`context`](#context)|`{String}`|`options.context \|\| compiler.options.context`|A path that determines how to interpret the `from` path| +|[`copyPermissions`](#copyPermissions)|`{Boolean}`|`false`|Applies source file permissions to destination files| ### `from` @@ -301,6 +302,17 @@ and so on... ] ``` +### `copyPermissions` + +**webpack.config.js** +```js +[ + new CopyWebpackPlugin([ + { from: 'src/**/*', to: 'dest/', copyPermissions: true } + ], options) +] +``` +

Options

|Name|Type|Default|Description| diff --git a/src/index.js b/src/index.js index 27cf1bea..fd6c8dac 100644 --- a/src/index.js +++ b/src/index.js @@ -1,6 +1,8 @@ import path from 'path'; import preProcessPattern from './preProcessPattern'; import processPattern from './processPattern'; +import {constants as coreConstants} from 'constants'; +import {chmodSync, constants} from 'fs'; function CopyWebpackPlugin(patterns = [], options = {}) { if (!Array.isArray(patterns)) { @@ -152,6 +154,26 @@ function CopyWebpackPlugin(patterns = [], options = {}) { } } + // Copy permissions for files that requested it + let output = compiler.options.output.path; + if (output === '/' && + compiler.options.devServer && + compiler.options.devServer.outputPath) { + output = compiler.options.devServer.outputPath; + } + + for (const key in written) { + let value = written[key]; + if (value.copyPermissions) { + debug(`restoring permissions to ${value.webpackTo}`); + + let constsfrom = constants || coreConstants; + + const mask = constsfrom.S_IRWXU | constsfrom.S_IRWXG | constsfrom.S_IRWXO; + chmodSync(path.join(output, value.webpackTo), value.perms & mask); + } + } + callback(); }; diff --git a/src/writeFile.js b/src/writeFile.js index 0091d98a..c6767be3 100644 --- a/src/writeFile.js +++ b/src/writeFile.js @@ -6,6 +6,7 @@ import { name, version } from '../package.json'; import findCacheDir from 'find-cache-dir'; import { stat, readFile } from './utils/promisify'; import crypto from 'crypto'; +import constants from 'constants'; export default function writeFile(globalRef, pattern, file) { const {info, debug, compilation, fileDependencies, written, inputFileSystem, copyUnmodified} = globalRef; @@ -117,6 +118,22 @@ export default function writeFile(globalRef, pattern, file) { return; } + let perms; + if (pattern.copyPermissions) { + debug(`saving permissions for '${file.absoluteFrom}'`); + + written[file.absoluteFrom].copyPermissions = pattern.copyPermissions; + written[file.absoluteFrom].webpackTo = file.webpackTo; + + let constsfrom = inputFileSystem.constants || constants; + + perms |= stat.mode & constsfrom.S_IRWXU; + perms |= stat.mode & constsfrom.S_IRWXG; + perms |= stat.mode & constsfrom.S_IRWXO; + + written[file.absoluteFrom].perms = perms; + } + info(`writing '${file.webpackTo}' to compilation assets from '${file.absoluteFrom}'`); compilation.assets[file.webpackTo] = { size: function() {