From c37c94821c1ab78d564f285f293284c58e80b51a Mon Sep 17 00:00:00 2001 From: Etan Reisner Date: Mon, 13 Mar 2017 15:14:11 -0400 Subject: [PATCH 1/7] Add support for copying file permissions on copied files. Add 'copyPermissions: true' to the entry for the files to enable. --- README.md | 1 + src/index.js | 19 +++++++++++++++++++ src/writeFile.js | 13 +++++++++++++ 3 files changed, 33 insertions(+) diff --git a/README.md b/README.md index efb5ade4..ec3cc744 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,7 @@ A pattern looks like: | `ignore` | N | [] | Additional globs to ignore for this pattern | | `transform` | N | function(content, path) {
  return content;
} | Function that modifies file contents before writing to webpack | | `force` | N | false | Overwrites files already in compilation.assets (usually added by other plugins) | +| copyPermissions | N | false | Applies source file permissions to destination files | #### Available options: diff --git a/src/index.js b/src/index.js index 097543f5..3ef3882c 100644 --- a/src/index.js +++ b/src/index.js @@ -2,6 +2,9 @@ import Promise from 'bluebird'; import _ from 'lodash'; import preProcessPattern from './preProcessPattern'; import processPattern from './processPattern'; +import path from 'path'; + +const fs = Promise.promisifyAll(require('fs')); // eslint-disable-line import/no-commonjs function CopyWebpackPlugin(patterns = [], options = {}) { if (!Array.isArray(patterns)) { @@ -121,6 +124,22 @@ 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; + } + + _.forEach(written, function (value) { + if (value.copyPermissions) { + debug(`restoring permissions to ${value.webpackTo}`); + const mask = fs.constants.S_IRWXU | fs.constants.S_IRWXG | fs.constants.S_IRWXO; + fs.chmodSync(path.join(output, value.webpackTo), value.perms & mask); + } + }); + callback(); }); }; diff --git a/src/writeFile.js b/src/writeFile.js index 43c86d6b..01b96ca8 100644 --- a/src/writeFile.js +++ b/src/writeFile.js @@ -80,6 +80,19 @@ 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; + + perms |= stat.mode & fs.constants.S_IRWXU; + perms |= stat.mode & fs.constants.S_IRWXG; + perms |= stat.mode & fs.constants.S_IRWXO; + written[file.absoluteFrom].perms = perms; + } + info(`writing '${file.webpackTo}' to compilation assets from '${file.absoluteFrom}'`); compilation.assets[file.webpackTo] = { size: function() { From 7e04f9cc32038c8b683b090ff15c8d18c64047fb Mon Sep 17 00:00:00 2001 From: Etan Reisner Date: Mon, 26 Jun 2017 08:50:14 -0400 Subject: [PATCH 2/7] Add support for older node versions. --- src/writeFile.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/writeFile.js b/src/writeFile.js index 01b96ca8..0b13479a 100644 --- a/src/writeFile.js +++ b/src/writeFile.js @@ -3,6 +3,7 @@ import loaderUtils from 'loader-utils'; import path from 'path'; const fs = Promise.promisifyAll(require('fs')); // eslint-disable-line import/no-commonjs +const constants = Promise.promisifyAll(require('constants')); // eslint-disable-line import/no-commonjs export default function writeFile(globalRef, pattern, file) { const {info, debug, compilation, fileDependencies, written, copyUnmodified} = globalRef; @@ -87,9 +88,12 @@ export default function writeFile(globalRef, pattern, file) { written[file.absoluteFrom].copyPermissions = pattern.copyPermissions; written[file.absoluteFrom].webpackTo = file.webpackTo; - perms |= stat.mode & fs.constants.S_IRWXU; - perms |= stat.mode & fs.constants.S_IRWXG; - perms |= stat.mode & fs.constants.S_IRWXO; + let constsfrom = fs.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; } From 9f8fd7ad2ba9ec9a15afe1e7b0d389db59c6f216 Mon Sep 17 00:00:00 2001 From: Etan Reisner Date: Mon, 2 Oct 2017 11:24:52 -0400 Subject: [PATCH 3/7] Older node support here too. Just to be safe. --- src/index.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 3ef3882c..4462c3a9 100644 --- a/src/index.js +++ b/src/index.js @@ -5,6 +5,7 @@ import processPattern from './processPattern'; import path from 'path'; const fs = Promise.promisifyAll(require('fs')); // eslint-disable-line import/no-commonjs +const constants = Promise.promisifyAll(require('constants')); // eslint-disable-line import/no-commonjs function CopyWebpackPlugin(patterns = [], options = {}) { if (!Array.isArray(patterns)) { @@ -135,7 +136,10 @@ function CopyWebpackPlugin(patterns = [], options = {}) { _.forEach(written, function (value) { if (value.copyPermissions) { debug(`restoring permissions to ${value.webpackTo}`); - const mask = fs.constants.S_IRWXU | fs.constants.S_IRWXG | fs.constants.S_IRWXO; + + let constsfrom = fs.constants || constants; + + const mask = constsfrom.S_IRWXU | constsfrom.S_IRWXG | constsfrom.S_IRWXO; fs.chmodSync(path.join(output, value.webpackTo), value.perms & mask); } }); From db4c6893695d529a1cd86ae65d0fb0b5928fa4b9 Mon Sep 17 00:00:00 2001 From: benjamin deering Date: Wed, 23 Jan 2019 15:03:59 -0500 Subject: [PATCH 4/7] Fix imports. Import fs.chmodSync as it is not available from inputFileSystem --- src/index.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/index.js b/src/index.js index 1e73acea..765ab646 100644 --- a/src/index.js +++ b/src/index.js @@ -1,10 +1,8 @@ import path from 'path'; import preProcessPattern from './preProcessPattern'; import processPattern from './processPattern'; -import path from 'path'; - -const fs = Promise.promisifyAll(require('fs')); // eslint-disable-line import/no-commonjs -const constants = Promise.promisifyAll(require('constants')); // eslint-disable-line import/no-commonjs +import constants from 'constants'; +import {chmodSync} from 'fs'; function CopyWebpackPlugin(patterns = [], options = {}) { if (!Array.isArray(patterns)) { @@ -171,7 +169,7 @@ function CopyWebpackPlugin(patterns = [], options = {}) { let constsfrom = fs.constants || constants; const mask = constsfrom.S_IRWXU | constsfrom.S_IRWXG | constsfrom.S_IRWXO; - fs.chmodSync(path.join(output, value.webpackTo), value.perms & mask); + chmodSync(path.join(output, value.webpackTo), value.perms & mask); } }); From 382ed0ad141483a7e5d3b5f03951fb0790274ede Mon Sep 17 00:00:00 2001 From: benjamin deering Date: Wed, 23 Jan 2019 15:11:18 -0500 Subject: [PATCH 5/7] replace lodash use --- src/index.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index 765ab646..a7e541e6 100644 --- a/src/index.js +++ b/src/index.js @@ -162,16 +162,17 @@ function CopyWebpackPlugin(patterns = [], options = {}) { output = compiler.options.devServer.outputPath; } - _.forEach(written, function (value) { + for (const key in written) { + let value = written[key]; if (value.copyPermissions) { debug(`restoring permissions to ${value.webpackTo}`); - let constsfrom = fs.constants || constants; + let constsfrom = compiler.inputFileSystem.constants || constants; const mask = constsfrom.S_IRWXU | constsfrom.S_IRWXG | constsfrom.S_IRWXO; chmodSync(path.join(output, value.webpackTo), value.perms & mask); } - }); + } callback(); }; From baba82a282cd9f821c084c7f7d530e53d3edba5b Mon Sep 17 00:00:00 2001 From: benjamin deering Date: Wed, 23 Jan 2019 15:22:55 -0500 Subject: [PATCH 6/7] Fix import of fs constants --- src/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index a7e541e6..fd6c8dac 100644 --- a/src/index.js +++ b/src/index.js @@ -1,8 +1,8 @@ import path from 'path'; import preProcessPattern from './preProcessPattern'; import processPattern from './processPattern'; -import constants from 'constants'; -import {chmodSync} from 'fs'; +import {constants as coreConstants} from 'constants'; +import {chmodSync, constants} from 'fs'; function CopyWebpackPlugin(patterns = [], options = {}) { if (!Array.isArray(patterns)) { @@ -167,7 +167,7 @@ function CopyWebpackPlugin(patterns = [], options = {}) { if (value.copyPermissions) { debug(`restoring permissions to ${value.webpackTo}`); - let constsfrom = compiler.inputFileSystem.constants || constants; + let constsfrom = constants || coreConstants; const mask = constsfrom.S_IRWXU | constsfrom.S_IRWXG | constsfrom.S_IRWXO; chmodSync(path.join(output, value.webpackTo), value.perms & mask); From 899ba2dc392fcc37efa32165eb773cb1fd779654 Mon Sep 17 00:00:00 2001 From: benjamin deering Date: Wed, 23 Jan 2019 15:38:39 -0500 Subject: [PATCH 7/7] Bad syntax fix --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 797151b7..2cf7afda 100644 --- a/README.md +++ b/README.md @@ -69,7 +69,7 @@ Or, in case of just a `from` with the default destination, you can also use a `{ **webpack.config.js** ```js -[` +[ new CopyWebpackPlugin([ 'relative/path/to/file.ext', '/absolute/path/to/file.ext',