-
-
Notifications
You must be signed in to change notification settings - Fork 451
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
breaking: use webpack builtin cache #1040
Open
JLHwung
wants to merge
7
commits into
main
Choose a base branch
from
use-webpack-builtin-cache
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
49b724d
breaking: use webpack builtin cache
JLHwung 5e435d0
fix test cases
JLHwung 2221426
remove unused old cache lib
JLHwung c6a894e
fix windows CI error
JLHwung 44c5b15
add a new test case for removing external deps
JLHwung 7717fb3
Add test with custom webpack cache plugin
JLHwung 96c4d92
update loader docs
JLHwung File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,9 +9,6 @@ | |
"engines": { | ||
"node": "^18.20.0 || ^20.10.0 || >=22.0.0" | ||
}, | ||
"dependencies": { | ||
"find-up": "^5.0.0" | ||
}, | ||
"peerDependencies": { | ||
"@babel/core": "^7.12.0", | ||
"webpack": ">=5.61.0" | ||
|
@@ -98,4 +95,4 @@ | |
] | ||
}, | ||
"packageManager": "[email protected]" | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,101 @@ | ||
const serialize = require("./serialize"); | ||
const transform = require("./transform"); | ||
const { promisify } = require("node:util"); | ||
|
||
/** @typedef {import("webpack").Compilation} Compilation */ | ||
/** @typedef {import("webpack").LoaderContext<{}>} LoaderContext */ | ||
/** @typedef {ReturnType<Compilation["getLogger"]>} WebpackLogger */ | ||
/** @typedef {ReturnType<Compilation["getCache"]>} CacheFacade */ | ||
|
||
const addTimestamps = async function (externalDependencies, getFileTimestamp) { | ||
for (const depAndEmptyTimestamp of externalDependencies) { | ||
try { | ||
const [dep] = depAndEmptyTimestamp; | ||
const { timestamp } = await getFileTimestamp(dep); | ||
depAndEmptyTimestamp.push(timestamp); | ||
} catch { | ||
// ignore errors if timestamp is not available | ||
} | ||
} | ||
}; | ||
|
||
const areExternalDependenciesModified = async function ( | ||
externalDepsWithTimestamp, | ||
getFileTimestamp, | ||
) { | ||
for (const depAndTimestamp of externalDepsWithTimestamp) { | ||
const [dep, timestamp] = depAndTimestamp; | ||
let newTimestamp; | ||
try { | ||
newTimestamp = (await getFileTimestamp(dep)).timestamp; | ||
} catch { | ||
return true; | ||
} | ||
if (timestamp !== newTimestamp) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
}; | ||
|
||
/** | ||
* @this {LoaderContext} | ||
* @param {string} filename The input resource path | ||
* @param {string} source The input source | ||
* @param {object} options The Babel transform options | ||
* @param {CacheFacade} cacheFacade The webpack cache facade instance | ||
* @param {string} cacheIdentifier The extra cache identifier | ||
* @param {WebpackLogger} logger | ||
*/ | ||
async function handleCache( | ||
filename, | ||
source, | ||
options = {}, | ||
cacheFacade, | ||
cacheIdentifier, | ||
logger, | ||
) { | ||
const getFileTimestamp = promisify((path, cb) => { | ||
this._compilation.fileSystemInfo.getFileTimestamp(path, cb); | ||
}); | ||
const hash = this.utils.createHash( | ||
this._compilation.outputOptions.hashFunction, | ||
); | ||
const cacheKey = hash | ||
.update(serialize([options, source, cacheIdentifier])) | ||
.digest("hex"); | ||
logger.debug(`getting cache for '${filename}', cachekey '${cacheKey}'`); | ||
|
||
const itemCache = cacheFacade.getItemCache(cacheKey, null); | ||
|
||
let result = await itemCache.getPromise(); | ||
logger.debug( | ||
result ? `found cache for '${filename}'` : `missed cache for '${filename}'`, | ||
); | ||
if (result) { | ||
if ( | ||
await areExternalDependenciesModified( | ||
result.externalDependencies, | ||
getFileTimestamp, | ||
) | ||
) { | ||
logger.debug( | ||
`discarded cache for '${filename}' due to changes in external dependencies`, | ||
); | ||
result = null; | ||
} | ||
} | ||
|
||
if (!result) { | ||
logger.debug("applying Babel transform"); | ||
result = await transform(source, options); | ||
await addTimestamps(result.externalDependencies, getFileTimestamp); | ||
logger.debug(`caching result for '${filename}'`); | ||
await itemCache.storePromise(result); | ||
logger.debug(`cached result for '${filename}'`); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
module.exports = handleCache; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The cache log is updated from "writing result to cache file" to current wordings to reflect the fact that the loader is unaware of the cache implementation: it may be written to the filesystem, it may be written to a database or it is retained in the memory.