-
Notifications
You must be signed in to change notification settings - Fork 122
feat: reapply "feat(webpack): add StartupChunkDependenciesRuntimeModule to fix chunk splitting" #798
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
Merged
Merged
feat: reapply "feat(webpack): add StartupChunkDependenciesRuntimeModule to fix chunk splitting" #798
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
03342a9
feat(webpack): add StartupChunkDependenciesRuntimeModule to fix chunk…
gaoachao e4ea8a4
chore: add changeset
gaoachao 40130d2
Update .changeset/flat-hats-flow.md
gaoachao d1cfcaf
refactor: mark StartupChunkDependenciesPluginOptions as internal type
gaoachao ea9661b
fix: remove useless comments
gaoachao 4a7f781
feat(webpack): add test
gaoachao c3e5bd2
fix(webpack): temporarily handle Promise exports of lazy bundle
gaoachao 2a3c15e
feat: add compilation.chunkGraph.hasChunkEntryDependentChunk
gaoachao 69792d5
Merge branch 'main' into feat/add-startup-chunk-deps-runtime
colinaaa e0e00e0
remove Lynx startup chunk dependencies test
gaoachao 2ec5f65
feat: add StartupEntrypointRuntimeModule
gaoachao d975e37
feat: add StartupEntrypointRuntimeModule
gaoachao 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 hidden or 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 @@ | ||
| --- | ||
| "@lynx-js/chunk-loading-webpack-plugin": patch | ||
| --- | ||
|
|
||
| Add `StartupChunkDependenciesRuntimeModule` to fix `RuntimeGlobals.ensureChunkHandler` not found when using chunk splitting |
This file contains hidden or 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
98 changes: 98 additions & 0 deletions
98
packages/webpack/chunk-loading-webpack-plugin/src/StartupChunkDependenciesPlugin.ts
This file contains hidden or 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,98 @@ | ||
| // Copyright 2024 The Lynx Authors. All rights reserved. | ||
| // Licensed under the Apache License Version 2.0 that can be found in the | ||
| // LICENSE file in the root directory of this source tree. | ||
| import type { Chunk, Compiler } from 'webpack'; | ||
|
|
||
| import { createStartupChunkDependenciesRuntimeModule } from './StartupChunkDependenciesRuntimeModule.js'; | ||
| import { createStartupEntrypointRuntimeModule } from './StartupEntrypointRuntimeModule.js'; | ||
|
|
||
| /** | ||
| * The options for StartupChunkDependenciesPlugin | ||
| */ | ||
| interface StartupChunkDependenciesPluginOptions { | ||
| /** | ||
| * Specifies the chunk loading method | ||
| * @defaultValue 'lynx' | ||
| * @remarks Currently only 'lynx' is supported | ||
| */ | ||
| chunkLoading: string; | ||
|
|
||
| /** | ||
| * Whether to enable async chunk loading | ||
| * @defaultValue true | ||
| * @remarks Currently only async loading mode is supported | ||
| */ | ||
| asyncChunkLoading: boolean; | ||
| } | ||
|
|
||
| const PLUGIN_NAME = 'StartupChunkDependenciesPlugin'; | ||
|
|
||
| export class StartupChunkDependenciesPlugin { | ||
| chunkLoading: string; | ||
| asyncChunkLoading: boolean; | ||
|
|
||
| constructor( | ||
| public options: StartupChunkDependenciesPluginOptions, | ||
| ) { | ||
| this.chunkLoading = options.chunkLoading; | ||
| this.asyncChunkLoading = typeof options.asyncChunkLoading === 'boolean' | ||
| ? options.asyncChunkLoading | ||
| : true; | ||
| } | ||
|
|
||
| apply(compiler: Compiler): void { | ||
| const { RuntimeGlobals } = compiler.webpack; | ||
|
|
||
| const StartupChunkDependenciesRuntimeModule = | ||
| createStartupChunkDependenciesRuntimeModule( | ||
| compiler.webpack, | ||
| ); | ||
|
|
||
| const StartupEntrypointRuntimeModule = createStartupEntrypointRuntimeModule( | ||
| compiler.webpack, | ||
| ); | ||
|
|
||
| compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => { | ||
| const globalChunkLoading = compilation.outputOptions.chunkLoading; | ||
|
|
||
| const isEnabledForChunk = (chunk: Chunk): boolean => { | ||
| const options = chunk.getEntryOptions(); | ||
| const chunkLoading = options && options.chunkLoading !== undefined | ||
| ? options.chunkLoading | ||
| : globalChunkLoading; | ||
| return chunkLoading === this.chunkLoading; | ||
| }; | ||
|
|
||
| compilation.hooks.additionalTreeRuntimeRequirements.tap( | ||
| PLUGIN_NAME, | ||
| (chunk, set) => { | ||
| if (!isEnabledForChunk(chunk)) return; | ||
|
|
||
| if (compilation.chunkGraph.hasChunkEntryDependentChunks(chunk)) { | ||
| set.add(RuntimeGlobals.startup); | ||
| set.add(RuntimeGlobals.ensureChunk); | ||
| set.add(RuntimeGlobals.ensureChunkIncludeEntries); | ||
| compilation.addRuntimeModule( | ||
| chunk, | ||
| new StartupChunkDependenciesRuntimeModule(this.asyncChunkLoading), | ||
| ); | ||
| } | ||
| }, | ||
| ); | ||
|
|
||
| compilation.hooks.runtimeRequirementInTree | ||
| .for(RuntimeGlobals.startupEntrypoint) | ||
| .tap(PLUGIN_NAME, (chunk, set) => { | ||
| if (!isEnabledForChunk(chunk)) return; | ||
|
|
||
| set.add(RuntimeGlobals.require); | ||
| set.add(RuntimeGlobals.ensureChunk); | ||
| set.add(RuntimeGlobals.ensureChunkIncludeEntries); | ||
| compilation.addRuntimeModule( | ||
| chunk, | ||
| new StartupEntrypointRuntimeModule(this.asyncChunkLoading), | ||
| ); | ||
| }); | ||
| }); | ||
| } | ||
| } | ||
78 changes: 78 additions & 0 deletions
78
packages/webpack/chunk-loading-webpack-plugin/src/StartupChunkDependenciesRuntimeModule.ts
This file contains hidden or 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,78 @@ | ||
| // Copyright 2024 The Lynx Authors. All rights reserved. | ||
| // Licensed under the Apache License Version 2.0 that can be found in the | ||
| // LICENSE file in the root directory of this source tree. | ||
|
|
||
| import type { RuntimeModule } from 'webpack'; | ||
|
|
||
| type StartupChunkDependenciesRuntimeModule = new( | ||
| asyncChunkLoading: boolean, | ||
| ) => RuntimeModule; | ||
|
|
||
| const runtimeTemplateBasicFunction = (args: string, body: string[]) => { | ||
| return `(${args}) => {\n${body.join('\n')}\n}`; | ||
| }; | ||
|
|
||
| export function createStartupChunkDependenciesRuntimeModule( | ||
| webpack: typeof import('webpack'), | ||
| ): StartupChunkDependenciesRuntimeModule { | ||
| const { RuntimeGlobals, RuntimeModule, Template } = webpack; | ||
| return class StartupChunkDependenciesRuntimeModule extends RuntimeModule { | ||
| asyncChunkLoading: boolean; | ||
|
|
||
| constructor(asyncChunkLoading: boolean) { | ||
| super('Lynx startup chunk dependencies', RuntimeModule.STAGE_ATTACH); | ||
| this.asyncChunkLoading = asyncChunkLoading; | ||
| } | ||
|
|
||
| override generate(): string { | ||
| const chunkGraph = this.chunkGraph!; | ||
| const chunk = this.chunk!; | ||
| const chunkIds = Array.from( | ||
| chunkGraph.getChunkEntryDependentChunksIterable(chunk), | ||
| ).map(chunk => chunk.id); | ||
| let startupCode: string[]; | ||
|
|
||
| if (this.asyncChunkLoading === false) { | ||
| startupCode = chunkIds | ||
| .map(id => `${RuntimeGlobals.ensureChunk}(${JSON.stringify(id)});`) | ||
| .concat('return next();'); | ||
| // lazy bundle can't exports Promise | ||
| // TODO: handle Promise in lazy bundle exports to support chunk splitting | ||
| } else if (chunkIds.length === 0) { | ||
| startupCode = ['return next();']; | ||
| } else if (chunkIds.length === 1) { | ||
| startupCode = [ | ||
| `return ${RuntimeGlobals.ensureChunk}(${ | ||
| JSON.stringify(chunkIds[0]) | ||
| }).then(next);`, | ||
| ]; | ||
| } else if (chunkIds.length > 2) { | ||
| startupCode = [ | ||
| `return Promise.all(${ | ||
| JSON.stringify(chunkIds) | ||
| }.map(${RuntimeGlobals.ensureChunk}, ${RuntimeGlobals.require})).then(next);`, | ||
| ]; | ||
| } else { | ||
| startupCode = [ | ||
| 'return Promise.all([', | ||
| Template.indent( | ||
| chunkIds.map(id => | ||
| `${RuntimeGlobals.ensureChunk}(${JSON.stringify(id)})` | ||
| ).join(',\n'), | ||
| ), | ||
| ']).then(next);', | ||
| ]; | ||
| } | ||
|
|
||
| return Template.asString([ | ||
| `var next = ${RuntimeGlobals.startup};`, | ||
| `${RuntimeGlobals.startup} = ${ | ||
| runtimeTemplateBasicFunction( | ||
| '', | ||
| startupCode, | ||
| ) | ||
| };`, | ||
| ]); | ||
| } | ||
| }; | ||
| } |
59 changes: 59 additions & 0 deletions
59
packages/webpack/chunk-loading-webpack-plugin/src/StartupEntrypointRuntimeModule.ts
This file contains hidden or 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,59 @@ | ||
| // Copyright 2024 The Lynx Authors. All rights reserved. | ||
| // Licensed under the Apache License Version 2.0 that can be found in the | ||
| // LICENSE file in the root directory of this source tree. | ||
|
|
||
| import type { RuntimeModule } from 'webpack'; | ||
|
|
||
| type StartupEntrypointRuntimeModule = new( | ||
| asyncChunkLoading: boolean, | ||
| ) => RuntimeModule; | ||
|
|
||
| const runtimeTemplateBasicFunction = (args: string, body: string[]) => { | ||
| return `(${args}) => {\n${body.join('\n')}\n}`; | ||
| }; | ||
|
|
||
| const runtimeTemplateReturningFunction = (returnValue: string, args = '') => { | ||
| return `(${args}) => (${returnValue})`; | ||
| }; | ||
|
|
||
| export function createStartupEntrypointRuntimeModule( | ||
| webpack: typeof import('webpack'), | ||
| ): StartupEntrypointRuntimeModule { | ||
| const { RuntimeGlobals, RuntimeModule } = webpack; | ||
| return class StartupEntrypointRuntimeModule extends RuntimeModule { | ||
| asyncChunkLoading: boolean; | ||
|
|
||
| constructor(asyncChunkLoading: boolean) { | ||
| super('Lynx startup entrypoint', RuntimeModule.STAGE_ATTACH); | ||
| this.asyncChunkLoading = asyncChunkLoading; | ||
| } | ||
|
|
||
| override generate(): string { | ||
| return `${RuntimeGlobals.startupEntrypoint} = ${ | ||
| runtimeTemplateBasicFunction('result, chunkIds, fn', [ | ||
| '// arguments: chunkIds, moduleId are deprecated', | ||
| 'var moduleId = chunkIds;', | ||
| `if(!fn) chunkIds = result, fn = ${ | ||
| runtimeTemplateReturningFunction( | ||
| `${RuntimeGlobals.require}(${RuntimeGlobals.entryModuleId} = moduleId)`, | ||
| ) | ||
| };`, | ||
| ...(this.asyncChunkLoading | ||
| ? [ | ||
| `return Promise.all(chunkIds.map(${RuntimeGlobals.ensureChunk}, ${RuntimeGlobals.require})).then(${ | ||
| runtimeTemplateBasicFunction('', [ | ||
| 'var r = fn();', | ||
| 'return r === undefined ? result : r;', | ||
| ]) | ||
| })`, | ||
| ] | ||
| : [ | ||
| `chunkIds.map(${RuntimeGlobals.ensureChunk}, ${RuntimeGlobals.require})`, | ||
| 'var r = fn();', | ||
| 'return r === undefined ? result : r;', | ||
| ]), | ||
| ]) | ||
| }`; | ||
| } | ||
| }; | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.