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
Serialize generatedCode info to fix bug in asset module cache restoration #16703
Merged
TheLarkInn
merged 3 commits into
webpack:main
from
ryanwilsonperkin:ryanwilsonperkin/fix-16160
Mar 6, 2023
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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 |
---|---|---|
@@ -0,0 +1,152 @@ | ||
"use strict"; | ||
|
||
require("./helpers/warmup-webpack"); | ||
|
||
const path = require("path"); | ||
const fs = require("graceful-fs"); | ||
const rimraf = require("rimraf"); | ||
|
||
let fixtureCount = 0; | ||
|
||
describe("Compiler (filesystem caching)", () => { | ||
jest.setTimeout(5000); | ||
|
||
const tempFixturePath = path.join( | ||
__dirname, | ||
"fixtures", | ||
"temp-filesystem-cache-fixture" | ||
); | ||
|
||
function compile(entry, onSuccess, onError) { | ||
const webpack = require(".."); | ||
const options = webpack.config.getNormalizedWebpackOptions({}); | ||
options.cache = { | ||
type: "filesystem", | ||
cacheDirectory: path.join(tempFixturePath, "cache") | ||
}; | ||
options.entry = entry; | ||
options.context = path.join(__dirname, "fixtures"); | ||
options.output.path = path.join(tempFixturePath, "dist"); | ||
options.output.filename = "bundle.js"; | ||
options.output.pathinfo = true; | ||
options.module = { | ||
rules: [ | ||
{ | ||
test: /\.svg$/, | ||
type: "asset/resource", | ||
use: { | ||
loader: require.resolve("./fixtures/empty-svg-loader") | ||
} | ||
} | ||
] | ||
}; | ||
|
||
function runCompiler(onSuccess, onError) { | ||
const c = webpack(options); | ||
c.hooks.compilation.tap( | ||
"CompilerCachingTest", | ||
compilation => (compilation.bail = true) | ||
); | ||
c.run((err, stats) => { | ||
if (err) throw err; | ||
expect(typeof stats).toBe("object"); | ||
stats = stats.toJson({ | ||
modules: true, | ||
reasons: true | ||
}); | ||
expect(typeof stats).toBe("object"); | ||
expect(stats).toHaveProperty("errors"); | ||
expect(Array.isArray(stats.errors)).toBe(true); | ||
if (stats.errors.length > 0) { | ||
onError(new Error(JSON.stringify(stats.errors, null, 4))); | ||
} | ||
c.close(() => { | ||
onSuccess(stats); | ||
}); | ||
}); | ||
} | ||
|
||
runCompiler(onSuccess, onError); | ||
|
||
return { | ||
runAgain: runCompiler | ||
}; | ||
} | ||
|
||
function cleanup() { | ||
rimraf.sync(`${tempFixturePath}*`); | ||
} | ||
|
||
beforeAll(cleanup); | ||
afterAll(cleanup); | ||
|
||
function createTempFixture() { | ||
const fixturePath = `${tempFixturePath}-${fixtureCount}`; | ||
const usesAssetFilepath = path.join(fixturePath, "uses-asset.js"); | ||
const svgFilepath = path.join(fixturePath, "file.svg"); | ||
|
||
// Remove previous copy if present | ||
rimraf.sync(fixturePath); | ||
|
||
// Copy over file since we"ll be modifying some of them | ||
fs.mkdirSync(fixturePath); | ||
fs.copyFileSync( | ||
path.join(__dirname, "fixtures", "uses-asset.js"), | ||
usesAssetFilepath | ||
); | ||
fs.copyFileSync(path.join(__dirname, "fixtures", "file.svg"), svgFilepath); | ||
|
||
fixtureCount++; | ||
return { | ||
rootPath: fixturePath, | ||
usesAssetFilepath: usesAssetFilepath, | ||
svgFilepath: svgFilepath | ||
}; | ||
} | ||
|
||
it("should compile again when cached asset has changed but loader output remains the same", done => { | ||
const tempFixture = createTempFixture(); | ||
|
||
const onError = error => done(error); | ||
|
||
const helper = compile( | ||
tempFixture.usesAssetFilepath, | ||
stats => { | ||
// Not cached the first time | ||
expect(stats.assets[0].name).toBe("bundle.js"); | ||
expect(stats.assets[0].emitted).toBe(true); | ||
|
||
expect(stats.assets[1].name).toMatch(/\w+\.svg$/); | ||
expect(stats.assets[0].emitted).toBe(true); | ||
|
||
helper.runAgain(stats => { | ||
// Cached the second run | ||
expect(stats.assets[0].name).toBe("bundle.js"); | ||
expect(stats.assets[0].emitted).toBe(false); | ||
|
||
expect(stats.assets[1].name).toMatch(/\w+\.svg$/); | ||
expect(stats.assets[0].emitted).toBe(false); | ||
|
||
const svgContent = fs | ||
.readFileSync(tempFixture.svgFilepath) | ||
.toString() | ||
.replace("icon-square-small", "icon-square-smaller"); | ||
|
||
fs.writeFileSync(tempFixture.svgFilepath, svgContent); | ||
|
||
helper.runAgain(stats => { | ||
// Still cached after file modification because loader always returns empty | ||
expect(stats.assets[0].name).toBe("bundle.js"); | ||
expect(stats.assets[0].emitted).toBe(false); | ||
|
||
expect(stats.assets[1].name).toMatch(/\w+\.svg$/); | ||
expect(stats.assets[0].emitted).toBe(false); | ||
|
||
done(); | ||
}, onError); | ||
}, onError); | ||
}, | ||
onError | ||
); | ||
}); | ||
}); | ||
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 @@ | ||
module.exports = () => "<svg></svg>"; |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 @@ | ||
import SVG from './file.svg'; |
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.
https://github.com/webpack/webpack/tree/main/test/watchCases/cache/asset-modules, so you don't need to write extra lines and it will be easy to maintance tests in future, thank you
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.
I originally attempted to write a test under asset-modules but could not get it to work, as far as I can see there isn't a mechanism in that test suite to give me the ability to modify an asset between compile steps. I had to heavily tweak the compilation approach in this unit test such that it:
webpack()
instance each time, to avoid relying on shared in-memory stateI don't think these three things are possible under
asset-modules