Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix module rebuilds that add new dependencies
- Loading branch information
1 parent
462c469
commit d39cbe2
Showing
6 changed files
with
77 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default "a.js"; |
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,7 @@ | ||
import A from "./a"; | ||
|
||
it("should compile", function (done) { | ||
expect(A).toBe("other-file.js"); | ||
|
||
done(); | ||
}); |
7 changes: 7 additions & 0 deletions
7
test/configCases/rebuild/rebuildWithNewDependencies/loader.js
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,7 @@ | ||
module.exports = function (source) { | ||
if (this.shouldReplace) | ||
return `import otherFile from './other-file.js'; | ||
export default otherFile; | ||
`; | ||
return source; | ||
}; |
1 change: 1 addition & 0 deletions
1
test/configCases/rebuild/rebuildWithNewDependencies/other-file.js
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 @@ | ||
export default "other-file.js"; |
60 changes: 60 additions & 0 deletions
60
test/configCases/rebuild/rebuildWithNewDependencies/webpack.config.js
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,60 @@ | ||
const { resolve, join } = require("path"); | ||
const { NormalModule } = require("../../../../"); | ||
|
||
/** | ||
* @param {import("../../../../").Compiler} compiler the compiler | ||
*/ | ||
var testPlugin = compiler => { | ||
compiler.hooks.compilation.tap("TestPlugin", compilation => { | ||
let shouldReplace = false; | ||
NormalModule.getCompilationHooks(compilation).loader.tap( | ||
"TestPlugin", | ||
loaderContext => { | ||
/** @type {any} */ (loaderContext).shouldReplace = shouldReplace; | ||
} | ||
); | ||
compilation.hooks.finishModules.tapAsync("TestPlugin", function ( | ||
modules, | ||
callback | ||
) { | ||
const src = resolve(join(__dirname, "a.js")); | ||
|
||
/** | ||
* | ||
* @param {any} m test | ||
* @returns {boolean} test | ||
*/ | ||
function matcher(m) { | ||
return m.resource && m.resource === src; | ||
} | ||
|
||
const module = Array.from(modules).find(matcher); | ||
|
||
if (!module) { | ||
throw new Error("something went wrong"); | ||
} | ||
|
||
shouldReplace = true; | ||
compilation.rebuildModule(module, err => { | ||
shouldReplace = false; | ||
callback(err); | ||
}); | ||
}); | ||
}); | ||
}; | ||
|
||
/** @type {import("../../../../").Configuration} */ | ||
module.exports = { | ||
module: { | ||
rules: [ | ||
{ | ||
test: /a.js/, | ||
use: "./loader" | ||
} | ||
] | ||
}, | ||
optimization: { | ||
concatenateModules: false | ||
}, | ||
plugins: [testPlugin] | ||
}; |