forked from webpack-contrib/copy-webpack-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refs webpack-contrib#396
- Loading branch information
Showing
6 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** | ||
* Attempt to get an Utimes function for the compiler's output filesystem. | ||
*/ | ||
function getUtimesFunction(compiler) { | ||
if (compiler.outputFileSystem.utimes) { | ||
// Webpack 5+ on Node will use graceful-fs for outputFileSystem so utimes is always there. | ||
// Other custom outputFileSystems could also have utimes. | ||
return compiler.outputFileSystem.utimes.bind(compiler.outputFileSystem); | ||
} else if ( | ||
compiler.outputFileSystem.constructor && | ||
compiler.outputFileSystem.constructor.name === 'NodeOutputFileSystem' | ||
) { | ||
// Default NodeOutputFileSystem can just use fs.utimes, but we need to late-import it in case | ||
// we're running in a web context and statically importing `fs` might be a bad idea. | ||
// eslint-disable-next-line global-require | ||
return require('fs').utimes; | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* Update the times of disk files for which we have recorded a source time | ||
* @param compiler | ||
* @param compilation | ||
* @param logger | ||
*/ | ||
function updateTimes(compiler, compilation, logger) { | ||
const utimes = getUtimesFunction(compiler); | ||
let nUpdated = 0; | ||
for (const [name, asset] of Object.entries(compilation.assets)) { | ||
// eslint-disable-next-line no-underscore-dangle | ||
const times = asset.copyPluginTimes; | ||
if (times) { | ||
const targetPath = | ||
asset.existsAt || | ||
compiler.outputFileSystem.join(compiler.outputPath, name); | ||
if (!utimes) { | ||
logger.warn( | ||
`unable to update time for ${targetPath} using current file system` | ||
); | ||
} else { | ||
// TODO: process these errors in a better way and/or wait for completion? | ||
utimes(targetPath, times.atime, times.mtime, (err) => { | ||
if (err) { | ||
logger.warn(`${targetPath}: utimes: ${err}`); | ||
} | ||
}); | ||
nUpdated += 1; | ||
} | ||
} | ||
} | ||
if (nUpdated > 0) { | ||
logger.info(`times updated for ${nUpdated} copied files`); | ||
} | ||
} | ||
|
||
export default updateTimes; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters