Skip to content
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

[Feature Request]: Refactor runtime and library bundling #2003

Open
4 tasks
wre232114 opened this issue Dec 17, 2024 · 0 comments
Open
4 tasks

[Feature Request]: Refactor runtime and library bundling #2003

wre232114 opened this issue Dec 17, 2024 · 0 comments
Labels
enhancement: pending triage Untriaged enhancement

Comments

@wre232114
Copy link
Member

wre232114 commented Dec 17, 2024

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):

  • 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 module
const b = require('./b)
module.exports = b

// output wrapped es module
import { __FARM_REGISTER__ } from '{FARM_RUNTIME}';
 
export default __FARM_REGISTER__('a.js', function(module, exports, farmRequire) { // export a function that can access the exports
 const b = 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:

require(esm)

// foo.cjs
const b = require('./bar.mjs');
console.log(b)

// bar.mjs
export default 'bar';

would be transformed to:

// foo.cjs
import { __FARM_REGISTER__ } from '{FARM_RUNTIME}';
 
export default __FARM_REGISTER__('foo.cjs', function(module, exports, farmRequire) {
 const b = require('./bar.mjs');
 console.log(b)
}); 

// bar.mjs
import { __FARM_REGISTER__ } from '{FARM_RUNTIME}';
 
export default __FARM_REGISTER__('bar.mjs', function(module, exports, farmRequire) {
  module.e(); // set exports.__esModule = true
  module.d(() => 'bar'); // transformed export default
}); 

the bundled result would like:

// bundle.mjs
import { __FARM_REGISTER__ } from './runtime.mjs';
// bar.mjs
var bar_default = __FARM_REGISTER__('bar.mjs', function(module, exports, farmRequire) {
  module.e(); // set exports.__esModule = true
  module.d(() => 'bar'); // transformed export default
});
// foo.cjs 
var foo_default = __FARM_REGISTER__('foo.cjs', function(module, exports, farmRequire) {
 const b = require('./bar.mjs');
 console.log(b)
}); 
foo_default(); // we have to execute foo_default for cjs entry

import cjs

// foo.mjs
import b from './bar.cjs';
console.log(b)

// bar.cjs
module.exports = 'bar';

would be transformed to:

// foo.mjs
// DO NOT transform foo.mjs

// bar.cjs
import { __FARM_REGISTER__ } from '{FARM_RUNTIME}';
 
export default __FARM_REGISTER__('bar.cjs', function(module, exports, farmRequire) {
  module.exports = 'bar';
}); 

the bundled result would like:

import { __FARM_REGISTER__, _intropRequireDefault } from './runtime.mjs';

// bar.cjs
var bar_default = __FARM_REGISTER__('bar.cjs', function(module, exports, farmRequire) {
    module.exports = 'bar';
});

// foo.mjs
var b = _intropRequireDefault(bar_default());
console.log(b);

export from cjs

TBD

For runtime bundle

TBD

Add freeze_module hook

TBD

Refactor packages/runtime

TBD

Limit usage of bundling algorithm for library

TBD

@wre232114 wre232114 added the enhancement: pending triage Untriaged enhancement label Dec 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement: pending triage Untriaged enhancement
Projects
None yet
Development

No branches or pull requests

1 participant