Skip to content

Commit

Permalink
Lockfile improvements (#148)
Browse files Browse the repository at this point in the history
* Use absolute file path when locking

* Use md5 of dirname instead of absolute path

* Removed sync versions of lock/unlock

* Updated hook being used to process assets

Use processAssets hook instead of afterProcessAssets
so that async lock/unlock functions can be used.

* Updated min webpack version

* Updated dev dependencies
  • Loading branch information
webdeveric authored Apr 9, 2021
1 parent 4f8042f commit 1da69ba
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 56 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
strategy:
matrix:
node-version: [10.x, 12.x, 14.x]
webpack-version: ['5.1.0', 5]
webpack-version: ['5.2.0', 5]
dev-server-version: ['3.6.0', latest]
css-loader-version: ['3.5.0', latest]
steps:
Expand Down
20 changes: 13 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"tapable": "^2.0"
},
"peerDependencies": {
"webpack": "^5.1.0"
"webpack": "^5.2.0"
},
"devDependencies": {
"@types/lodash.get": "^4.4.6",
Expand All @@ -74,7 +74,7 @@
"compression-webpack-plugin": "^7.1.2",
"copy-webpack-plugin": "^8.1.1",
"cspell": "^5.3.12",
"css-loader": "^5.0.2",
"css-loader": "^5.2.1",
"eslint": "^7.19.0",
"file-loader": "^6.2.0",
"fs-extra": "^9.1.0",
Expand Down
32 changes: 19 additions & 13 deletions src/WebpackAssetsManifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ const {
findMapKeysByValue,
lock,
unlock,
lockSync,
unlockSync,
} = require('./helpers.js');

/** @type {object} */
Expand Down Expand Up @@ -415,17 +413,19 @@ class WebpackAssetsManifest
*
* @param {object} compilation
*/
emitAssetsManifest(compilation)
async emitAssetsManifest(compilation)
{
const outputPath = this.getOutputPath();

const output = this.getManifestPath(
compilation,
this.inDevServer() ?
path.basename( this.options.output ) :
path.relative( compilation.compiler.outputPath, this.getOutputPath() ),
path.relative( compilation.compiler.outputPath, outputPath ),
);

if ( this.options.merge ) {
lockSync( output );
await lock( outputPath );
}

this.maybeMerge();
Expand All @@ -439,7 +439,7 @@ class WebpackAssetsManifest
);

if ( this.options.merge ) {
unlockSync( output );
await unlock( outputPath );
}
}

Expand Down Expand Up @@ -538,7 +538,7 @@ class WebpackAssetsManifest
*
* @param {object} compilation
*/
handleAfterProcessAssets( compilation )
async handleProcessAssetsReport( compilation )
{
// Look in DefaultStatsPresetPlugin.js for options
const stats = compilation.getStats().toJson({
Expand Down Expand Up @@ -630,7 +630,7 @@ class WebpackAssetsManifest
}
}

this.emitAssetsManifest(compilation);
await this.emitAssetsManifest(compilation);
}

/**
Expand Down Expand Up @@ -797,15 +797,21 @@ class WebpackAssetsManifest
handleThisCompilation(compilation)
{
if ( this.options.integrity ) {
compilation.hooks.afterProcessAssets.tap(
PLUGIN_NAME,
compilation.hooks.processAssets.tap(
{
name: PLUGIN_NAME,
stage: Compilation.PROCESS_ASSETS_STAGE_ANALYSE,
},
this.recordSubresourceIntegrity.bind(this, compilation),
);
}

compilation.hooks.afterProcessAssets.tap(
PLUGIN_NAME,
this.handleAfterProcessAssets.bind(this, compilation),
compilation.hooks.processAssets.tapPromise(
{
name: PLUGIN_NAME,
stage: Compilation.PROCESS_ASSETS_STAGE_REPORT,
},
this.handleProcessAssetsReport.bind(this, compilation),
);
}

Expand Down
44 changes: 11 additions & 33 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,16 +161,22 @@ function group( arr, getGroup, mapper = item => item )
);
}

function md5( data )
{
return crypto.createHash('md5').update( data ).digest('hex');
}

/**
* Build a file path to a lock file in the tmp directory
*
* @param {string} filename
*/
function getLockFilename( filename )
{
const name = filename.replace(/[^\w]+/g, '-');
const name = path.basename( filename );
const dirHash = md5( path.dirname( filename ) );

return path.join( os.tmpdir(), `${name}.lock` );
return path.join( os.tmpdir(), `${dirHash}-${name}.lock` );
}

/**
Expand All @@ -183,26 +189,10 @@ async function lock( filename )
await lfLock(
getLockFilename( filename ),
{
wait: 10000,
stale: 20000,
retries: 100,
wait: 6000,
retryWait: 100,
},
);
}

/**
* Create a lockfile
*
* @param {string} filename
*/
function lockSync( filename )
{
return lockfile.lockSync(
getLockFilename( filename ),
{
stale: 20000,
retries: 200,
stale: 5000,
retries: 100,
},
);
}
Expand All @@ -217,16 +207,6 @@ async function unlock( filename )
await lfUnlock( getLockFilename( filename ) );
}

/**
* Remove a lockfile
*
* @param {string} filename
*/
function unlockSync( filename )
{
return lockfile.unlockSync( getLockFilename( filename ) );
}

module.exports = {
maybeArrayWrap,
filterHashes,
Expand All @@ -239,7 +219,5 @@ module.exports = {
group,
getLockFilename,
lock,
lockSync,
unlock,
unlockSync,
};

0 comments on commit 1da69ba

Please sign in to comment.