feat: add new optimization.entryIife config#18772
feat: add new optimization.entryIife config#18772alexander-akait merged 5 commits intowebpack:mainfrom
Conversation
|
For maintainers only:
|
d149bf4 to
d1e1126
Compare
d1e1126 to
7bc06a3
Compare
|
@alexander-akait Any review suggestions are welcome, thank you! |
011cb2b to
2c37b4f
Compare
test/configCases/output-module/inlined-module/webpack.config.js
Outdated
Show resolved
Hide resolved
|
Also if we can optimize iife better in future, make sense to rename option |
| renamedInlinedModule.get(m) || | ||
| this.renderModule(m, chunkRenderContext, hooks, false); | ||
| const renderedModule = renamedInlinedModule | ||
| ? renamedInlinedModule.get(m) |
There was a problem hiding this comment.
We need a test here, I think it is unnecessary, because we already renamed them before
There was a problem hiding this comment.
Just try
const renderedModule = this.renderModule(
m,
chunkRenderContext,
hooks,
false
);And the output the same
There was a problem hiding this comment.
We need a test here, I think it is unnecessary, because we already renamed them before
It's necessary. The initial value of renamedInlinedModule is false. If avoidEntryIife is enabled, getRenamedInlineModule will check for other reasons to bail out of the IIFE. If any exist, renamedInlinedModule will remain false because the inlined module will still be wrapped in an IIFE for those reasons. Therefore, we skip the optimization here.
There was a problem hiding this comment.
I ask this because of
webpack/lib/javascript/JavascriptModulesPlugin.js
Line 1514 in fe16998
Do we really need source.replace? We need a test here, because our tests don't need renaming, so i thought we could avoid this here
There was a problem hiding this comment.
If we don't need renaming, we can simplify our code quite a bit.
There was a problem hiding this comment.
@fi3ework I still think we have wrong logic, because if I remove
for (const variable of usedInNonInlined) {
const references = getAllReferences(variable);
const allIdentifiers = new Set(
references.map(r => r.identifier).concat(variable.identifiers)
);
const newName = this.findNewName(
variable.name,
usedNames,
m.readableIdentifier(runtimeTemplate.requestShortener)
);
usedNames.add(newName);
for (const identifier of allIdentifiers) {
const r = /** @type {Range} */ (identifier.range);
const path = getPathInAst(ast, identifier);
if (path && path.length > 1) {
const maybeProperty =
path[1].type === "AssignmentPattern" && path[1].left === path[0]
? path[2]
: path[1];
if (maybeProperty.type === "Property" && maybeProperty.shorthand) {
source.insert(r[1], `: ${newName}`);
continue;
}
}
source.replace(r[0], r[1] - 1, newName);
}
}Everything works fine
There was a problem hiding this comment.
As I said before, we already doing renaming before (in concatination modules) and I don't think we need to do it here
There was a problem hiding this comment.
Just put console.log or use debugger for fast debug and we never reach this branches
We already continue here:
if (usedInNonInlined.size === 0) {
renamedInlinedModules.set(m, source);
continue;
}There was a problem hiding this comment.
The case in test/configCases/output-module/iife-entry-module-with-others/index.js It's not working as well as it should. I'm fixing it. I checked the 'test/cases/entry-inline' case, and it is still working.
I made https://stackblitz.com/edit/node-ojwwax, you could check localVar in dist-5.91.0 and dist-5.94.0 to see the difference. In 5.94.0, localVar is renamed to src_localVar by renaming.
There was a problem hiding this comment.
@alexander-akait Updated, please check the new test case now.
The current test cases serve the following purposes, for instance with iife-entry-module-with-others:
- Assert the correct IIFE comment in the bundle.
- Assert the correct runtime behavior of the bundle.
- If the reason branch
it need to be isolated against other entry modules.is deleted, the test will fail
The other test cases are similar, iife-multiple-entry-modules has an extra avoidEntryIife configuration toggle test, which can be seen as a supplement to test/cases/entry-inline.
Will this confuse with https://webpack.js.org/configuration/output/#outputiife ? Using |
2c37b4f to
0811d8f
Compare
0811d8f to
fe16998
Compare
|
@fi3ework I still think we can get this into a plugin, because we can do more optimization in future (like in todo)
Let's skip the name for now, I don't like spending too much time on it, the working code is more important, we need to add tests and check everything above. In fact, we can make more optimizations for |
|
@fi3ework I'll merge it now because the logic works completely, but I still think we should have thought about new hooks to bring this logic into the plugin (we always try to do this), I think something like: const factoryRenderedModule = hooks.optimizeInlinedModule.call(() => this.renderModule(...), m, allModules, ...otherArgs);
const renderedModule = factoryRenderedModule();So, we can create a map What do you think? |
|
Is it possible to get the non-inlined rendered module result from webpack/lib/javascript/JavascriptModulesPlugin.js Lines 766 to 775 in 90ea8b8 () => this.renderModule(...) and re-render that again?
cc @ahabhgk, could you please take a look at the new proposal for the hook as well? Are there any other issues we can address with this hook in Rspack to make it more general? Thanks! 😊 |
|
|
|
Looks good for me too |
What kind of change does this PR introduce?
Fix #18694.
Still need to check the code before ready for review, will check tomorrow.
Did you add tests for your changes?
Yes.
Does this PR introduce a breaking change?
No.
What needs to be documented once your changes are merged?
webpack/webpack.js.org#7402