forked from webpack/webpack
-
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.
Merge pull request webpack#11433 from samcooke98/samc/fix-module-rebuild
- Loading branch information
Showing
8 changed files
with
120 additions
and
15 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,5 @@ | ||
import { doThings, foo } from "./other-file"; | ||
|
||
export { doThings, foo }; | ||
|
||
export const valueFromA = "A"; |
Empty file.
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,20 @@ | ||
import { doThings, foo, valueFromA } from "./a"; | ||
it("should compile", function (done) { | ||
expect(doThings("ok")).toBe("ok"); | ||
|
||
// Should be replaced by the code in the config. | ||
expect(foo.foo).toBe("bar"); | ||
expect(valueFromA).toBe("A"); | ||
|
||
done(); | ||
}); | ||
|
||
it("should not reference the chunk", () => { | ||
expect(__STATS__.chunks.length).toEqual(1); | ||
expect( | ||
__STATS__.modules | ||
.filter(m => m.moduleType !== "runtime") | ||
.map(m => m.name) | ||
.sort() | ||
).toEqual(["./a.js", "./index.js", "./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,5 @@ | ||
module.exports = function (source) { | ||
if (this.shouldReplace) | ||
return "module.exports = { foo: { foo: 'bar' }, doThings: (v) => v}"; | ||
return source; | ||
}; |
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 "foo"; |
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,9 @@ | ||
import foo from "./module"; | ||
|
||
export function doThings(stuff) { | ||
return import("./chunk"); | ||
} | ||
|
||
export const foo = { | ||
foo | ||
}; |
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, "other-file.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: /other-file/, | ||
use: "./loader" | ||
} | ||
] | ||
}, | ||
optimization: { | ||
concatenateModules: false | ||
}, | ||
plugins: [testPlugin] | ||
}; |