You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently the implementation of plugin_bundle and plugin_runtime has a lot of duplication, we need to refactor the implementation of these two plugins to normalize the behavior of how to inject cjs runtime and how to concatenate modules.
Here's the refactor summary(See following api design for details):
Refactor cjs runtime to make sure plugin_bundle and plugin runtime use the same logic
Add a new freeze_module hook called after the module graph is built but before build_end, and perform cjs runtime wrap here
Refactor packages/runtime, turn a huge runtime module class into small isolate function, and imports them in need
Limit the usage of bundling algorithm when library bundle, disable enforce_resources
What does the proposed API look like?
Refactor cjs runtime
Currently plugin_bundle wraps __commonJs(module, exports) {} for cjs module, but plugin_runtime wraps function(module, exports, farmRequire) {}, we need to normalize these two runtimes into the same format as follow:
// input cjs moduleconstb=require('./b)
module.exports=b// output wrapped es moduleimport{__FARM_REGISTER__}from'{FARM_RUNTIME}';exportdefault__FARM_REGISTER__('a.js',function(module,exports,farmRequire){// export a function that can access the exportsconstb=farmRequire('./b');module.exports=b;});
For library bundle and browser bundle, here's the difference when applying above transforms:
For library bundle
Only cjs module and it's dependencies(including require(esm)) would be transformed, example:
// foo.cjsimport{__FARM_REGISTER__}from'{FARM_RUNTIME}';exportdefault__FARM_REGISTER__('foo.cjs',function(module,exports,farmRequire){constb=require('./bar.mjs');console.log(b)});// bar.mjsimport{__FARM_REGISTER__}from'{FARM_RUNTIME}';exportdefault__FARM_REGISTER__('bar.mjs',function(module,exports,farmRequire){module.e();// set exports.__esModule = truemodule.d(()=>'bar');// transformed export default});
the bundled result would like:
// bundle.mjsimport{__FARM_REGISTER__}from'./runtime.mjs';// bar.mjsvarbar_default=__FARM_REGISTER__('bar.mjs',function(module,exports,farmRequire){module.e();// set exports.__esModule = truemodule.d(()=>'bar');// transformed export default});// foo.cjs varfoo_default=__FARM_REGISTER__('foo.cjs',function(module,exports,farmRequire){constb=require('./bar.mjs');console.log(b)});foo_default();// we have to execute foo_default for cjs entry
// foo.mjs// DO NOT transform foo.mjs// bar.cjsimport{__FARM_REGISTER__}from'{FARM_RUNTIME}';exportdefault__FARM_REGISTER__('bar.cjs',function(module,exports,farmRequire){module.exports='bar';});
What problem does this feature solve?
Currently the implementation of plugin_bundle and plugin_runtime has a lot of duplication, we need to refactor the implementation of these two plugins to normalize the behavior of how to inject cjs runtime and how to concatenate modules.
Here's the refactor summary(See following api design for details):
freeze_module
hook called after the module graph is built but beforebuild_end
, and perform cjs runtime wrap hereenforce_resources
What does the proposed API look like?
Refactor cjs runtime
Currently plugin_bundle wraps
__commonJs(module, exports) {}
for cjs module, but plugin_runtime wrapsfunction(module, exports, farmRequire) {}
, we need to normalize these two runtimes into the same format as follow:For library bundle and browser bundle, here's the difference when applying above transforms:
For library bundle
Only cjs module and it's dependencies(including
require(esm)
) would be transformed, example:require(esm)
would be transformed to:
the bundled result would like:
import cjs
would be transformed to:
the bundled result would like:
export from cjs
TBD
For runtime bundle
TBD
Add
freeze_module
hookTBD
Refactor packages/runtime
TBD
Limit usage of bundling algorithm for library
TBD
The text was updated successfully, but these errors were encountered: