-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: decouple init & module error handling from runtime module (#868)
* refactor: use expect-error intead of ignore * fix: separate responsibilites for runtime modules in TargetPlugin * fix: limit poluting the global scope with runtime global types * fix: use primitives from compiler.webpack * chore: add comments * chore: add ReanimatedPlugin to webpack * chore: add changeset
- Loading branch information
Showing
15 changed files
with
349 additions
and
217 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@callstack/repack": patch | ||
--- | ||
|
||
Decouple init & module error handling from load script runtime module inside RepackTargetPlugin |
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
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
4 changes: 1 addition & 3 deletions
4
packages/repack/src/modules/ScriptManager/getWebpackContext.ts
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 |
---|---|---|
@@ -1,10 +1,8 @@ | ||
import type { WebpackContext } from './types.js'; | ||
|
||
/** | ||
* Get Webpack runtime context form current JavaScript scope. | ||
* | ||
* __You likely don't need to use it.__ | ||
*/ | ||
export function getWebpackContext(): WebpackContext { | ||
export function getWebpackContext(): RepackRuntimeGlobals.WebpackRequire { | ||
return __webpack_require__; | ||
} |
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
34 changes: 34 additions & 0 deletions
34
packages/repack/src/plugins/RepackTargetPlugin/InitRuntimeModule.ts
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,34 @@ | ||
import type { | ||
Compiler, | ||
RuntimeModule as RuntimeModuleType, | ||
} from '@rspack/core'; | ||
|
||
interface InitRuntimeModuleConfig { | ||
globalObject: string; | ||
} | ||
|
||
// runtime module class is generated dynamically based on the compiler instance | ||
// this way it's compatible with both webpack and rspack | ||
export const makeInitRuntimeModule = ( | ||
compiler: Compiler, | ||
moduleConfig: InitRuntimeModuleConfig | ||
): RuntimeModuleType => { | ||
const Template = compiler.webpack.Template; | ||
const RuntimeModule = compiler.webpack.RuntimeModule; | ||
|
||
const InitRuntimeModule = class extends RuntimeModule { | ||
constructor(private config: InitRuntimeModuleConfig) { | ||
super('repack/init', RuntimeModule.STAGE_BASIC); | ||
} | ||
|
||
generate() { | ||
return Template.asString([ | ||
Template.getFunctionContent( | ||
require('./implementation/init.js') | ||
).replaceAll('$globalObject$', this.config.globalObject), | ||
]); | ||
} | ||
}; | ||
|
||
return new InitRuntimeModule(moduleConfig); | ||
}; |
37 changes: 37 additions & 0 deletions
37
packages/repack/src/plugins/RepackTargetPlugin/LoadScriptRuntimeModule.ts
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,37 @@ | ||
import type { | ||
Compiler, | ||
RuntimeModule as RuntimeModuleType, | ||
} from '@rspack/core'; | ||
|
||
interface LoadScriptRuntimeModuleConfig { | ||
chunkId: string | number | undefined; | ||
hmrEnabled: boolean; | ||
} | ||
|
||
// runtime module class is generated dynamically based on the compiler instance | ||
// this way it's compatible with both webpack and rspack | ||
export const makeLoadScriptRuntimeModule = ( | ||
compiler: Compiler, | ||
moduleConfig: LoadScriptRuntimeModuleConfig | ||
): RuntimeModuleType => { | ||
const Template = compiler.webpack.Template; | ||
const RuntimeGlobals = compiler.webpack.RuntimeGlobals; | ||
const RuntimeModule = compiler.webpack.RuntimeModule; | ||
|
||
const LoadScriptRuntimeModule = class extends RuntimeModule { | ||
constructor(private config: LoadScriptRuntimeModuleConfig) { | ||
super('repack/load script', RuntimeModule.STAGE_BASIC); | ||
} | ||
|
||
generate() { | ||
return Template.asString([ | ||
Template.getFunctionContent(require('./implementation/loadScript.js')) | ||
.replaceAll('$caller$', `'${this.config.chunkId?.toString()}'`) | ||
.replaceAll('$hmrEnabled$', `${this.config.hmrEnabled}`) | ||
.replaceAll('$loadScript$', RuntimeGlobals.loadScript), | ||
]); | ||
} | ||
}; | ||
|
||
return new LoadScriptRuntimeModule(moduleConfig); | ||
}; |
40 changes: 40 additions & 0 deletions
40
packages/repack/src/plugins/RepackTargetPlugin/ModuleErrorHandlerRuntimeModule.ts
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,40 @@ | ||
import type { | ||
Compiler, | ||
RuntimeModule as RuntimeModuleType, | ||
} from '@rspack/core'; | ||
|
||
interface ModuleErrorHandlerRuntimeModuleConfig { | ||
globalObject: string; | ||
} | ||
|
||
// runtime module class is generated dynamically based on the compiler instance | ||
// this way it's compatible with both webpack and rspack | ||
export const makeModuleErrorHandlerRuntimeModule = ( | ||
compiler: Compiler, | ||
moduleConfig: ModuleErrorHandlerRuntimeModuleConfig | ||
): RuntimeModuleType => { | ||
const Template = compiler.webpack.Template; | ||
const RuntimeGlobals = compiler.webpack.RuntimeGlobals; | ||
const RuntimeModule = compiler.webpack.RuntimeModule; | ||
|
||
const ModuleErrorHandlerRuntimeModule = class extends RuntimeModule { | ||
constructor(private config: ModuleErrorHandlerRuntimeModuleConfig) { | ||
super('repack/module error handler', RuntimeModule.STAGE_BASIC); | ||
} | ||
|
||
generate() { | ||
return Template.asString([ | ||
Template.getFunctionContent( | ||
require('./implementation/moduleErrorHandler.js') | ||
) | ||
.replaceAll('$globalObject$', this.config.globalObject) | ||
.replaceAll( | ||
'$interceptModuleExecution$', | ||
RuntimeGlobals.interceptModuleExecution | ||
), | ||
]); | ||
} | ||
}; | ||
|
||
return new ModuleErrorHandlerRuntimeModule(moduleConfig); | ||
}; |
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
Oops, something went wrong.